We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vben Admin V5
参照V2的版本实现的格式化时间范围字段方法
//添加的文件位置: packages/@core/ui-kit/form-ui/src/types.ts export type FieldMapToTime = [ string, [string, string], ([string, string] | string)?, ][];
//添加的文件位置: packages/@core/ui-kit/form-ui/src/components/form-actions.vue async function handleSubmit(e: Event) { e?.preventDefault(); e?.stopPropagation(); const { valid } = await form.validate(); if (!valid) { return; } const values = handleRangeTimeValue(toRaw(form.values)); await unref(rootProps).handleSubmit?.(values); } async function handleReset(e: Event) { e?.preventDefault(); e?.stopPropagation(); const props = unref(rootProps); const values = toRaw(form.values); // 清理时间字段 props.fieldMapToTime && props.fieldMapToTime.forEach(([_, [startTimeKey, endTimeKey]]) => { delete values[startTimeKey]; delete values[endTimeKey]; }); if (isFunction(props.handleReset)) { await props.handleReset?.(values); } else { form.resetForm(); } } /** * @description: Processing time interval parameters */ function handleRangeTimeValue(values: Record<string, any>) { const fieldMapToTime = unref(rootProps).fieldMapToTime; if (!fieldMapToTime) return values; if (!Array.isArray(fieldMapToTime)) { return values; } fieldMapToTime.forEach( ([field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD']) => { if (!values[field]) { delete values[field]; return; } const [startTime, endTime] = values[field]; const [startTimeFormat, endTimeFormat] = Array.isArray(format) ? format : [format, format]; values[startTimeKey] = startTime ? formatTime(startTime, startTimeFormat) : undefined; values[endTimeKey] = endTime ? formatTime(endTime, endTimeFormat) : undefined; delete values[field]; }, ); return values; } function formatTime(time: string, format: string): number | string { const date = new Date(time); const timestamp = (date: Date) => Math.floor(date.getTime() / 1000); if (format === 'timestamp') return timestamp(date); if (format === 'timestampStartDay') return timestamp( new Date(date.getFullYear(), date.getMonth(), date.getDate()), ); const padZero = (num: number) => num.toString().padStart(2, '0'); const replacements: Record<string, string> = { DD: padZero(date.getDate()), HH: padZero(date.getHours()), MM: padZero(date.getMonth() + 1), mm: padZero(date.getMinutes()), ss: padZero(date.getSeconds()), YYYY: date.getFullYear().toString(), }; return format.replaceAll( /YYYY|MM|DD|HH|mm|ss/g, (match) => replacements[match] || match, ); }
使用方法
const [Grid, gridApi] = useVbenVxeGrid({ formOptions: { schema: searchFormSchema, showCollapseButton: searchFormSchema.length > 3, // 格式化日期 fieldMapToTime: [ ['createTime', ['createTimeBegin', 'createTimeEnd'], 'YYYY-MM-DD'], ], }, gridOptions, });
希望官方大大能实现这个小功能
No response
The text was updated successfully, but these errors were encountered:
你都写好了,怎么不直接提pr
Sorry, something went wrong.
Successfully merging a pull request may close this issue.
Version
Vben Admin V5
Description
参照V2的版本实现的格式化时间范围字段方法
使用方法
Proposed Solution
希望官方大大能实现这个小功能
Alternatives Considered
No response
Additional Context
No response
Validations
The text was updated successfully, but these errors were encountered: