|
|
@@ -123,7 +123,29 @@ const props = defineProps({
|
|
|
})
|
|
|
|
|
|
const emit = defineEmits(['update:modelValue', 'search', 'reset'])
|
|
|
+// 获取日期字段的默认值(当前月范围或当天)
|
|
|
+const getDefaultDateValue = (field) => {
|
|
|
+ const now = new Date()
|
|
|
+ const year = now.getFullYear()
|
|
|
+ const month = now.getMonth()
|
|
|
+ const firstDay = new Date(year, month, 1)
|
|
|
+ const lastDay = new Date(year, month + 1, 0)
|
|
|
+
|
|
|
+ const formatDate = (date) => {
|
|
|
+ const y = date.getFullYear()
|
|
|
+ const m = String(date.getMonth() + 1).padStart(2, '0')
|
|
|
+ const d = String(date.getDate()).padStart(2, '0')
|
|
|
+ return `${y}-${m}-${d}`
|
|
|
+ }
|
|
|
|
|
|
+ if (field.type === 'daterange') {
|
|
|
+ return [formatDate(firstDay), formatDate(lastDay)]
|
|
|
+ } else if (field.type === 'date') {
|
|
|
+ // 单日期默认设为今天(也可设为第一天,根据需求调整)
|
|
|
+ return formatDate(now)
|
|
|
+ }
|
|
|
+ return ''
|
|
|
+}
|
|
|
// 表单数据(PC端)
|
|
|
const formData = ref({})
|
|
|
// 移动端临时数据
|
|
|
@@ -149,15 +171,15 @@ const selectChip = (key, value) => {
|
|
|
const initFormData = () => {
|
|
|
const initial = {}
|
|
|
props.fields.forEach(field => {
|
|
|
- // 1. 优先使用外部传入的 modelValue
|
|
|
- if (props.modelValue && props.modelValue[field.key] !== undefined) {
|
|
|
+ // 1. 优先使用外部传入的 modelValue(如果值不为 null 或 undefined)
|
|
|
+ if (props.modelValue && props.modelValue[field.key] != null) {
|
|
|
initial[field.key] = props.modelValue[field.key]
|
|
|
}
|
|
|
// 2. 其次使用字段配置的 defaultValue
|
|
|
else if (field.defaultValue !== undefined) {
|
|
|
initial[field.key] = field.defaultValue
|
|
|
}
|
|
|
- // 3. 日期字段特殊处理:默认当前月
|
|
|
+ // 3. 日期字段特殊处理:默认当前月(即使 modelValue 中存在但为 null/undefined)
|
|
|
else if (field.type === 'date' || field.type === 'daterange') {
|
|
|
initial[field.key] = getDefaultDateValue(field)
|
|
|
}
|
|
|
@@ -210,8 +232,11 @@ const nonDateField = computed(() => {
|
|
|
watch(() => props.modelValue, (newVal) => {
|
|
|
if (newVal) {
|
|
|
props.fields.forEach(field => {
|
|
|
- if (newVal[field.key] !== undefined) {
|
|
|
+ if (newVal[field.key] != null) {
|
|
|
formData.value[field.key] = newVal[field.key]
|
|
|
+ } else if (field.type === 'date' || field.type === 'daterange') {
|
|
|
+ // 当日期字段为 null 或 undefined 时,使用默认值
|
|
|
+ formData.value[field.key] = getDefaultDateValue(field)
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
@@ -266,29 +291,7 @@ const resetTempForm = () => {
|
|
|
})
|
|
|
tempFormData.value = empty
|
|
|
}
|
|
|
-// 获取日期字段的默认值(当前月范围或当天)
|
|
|
-const getDefaultDateValue = (field) => {
|
|
|
- const now = new Date()
|
|
|
- const year = now.getFullYear()
|
|
|
- const month = now.getMonth()
|
|
|
- const firstDay = new Date(year, month, 1)
|
|
|
- const lastDay = new Date(year, month + 1, 0)
|
|
|
-
|
|
|
- const formatDate = (date) => {
|
|
|
- const y = date.getFullYear()
|
|
|
- const m = String(date.getMonth() + 1).padStart(2, '0')
|
|
|
- const d = String(date.getDate()).padStart(2, '0')
|
|
|
- return `${y}-${m}-${d}`
|
|
|
- }
|
|
|
|
|
|
- if (field.type === 'daterange') {
|
|
|
- return [formatDate(firstDay), formatDate(lastDay)]
|
|
|
- } else if (field.type === 'date') {
|
|
|
- // 单日期默认设为今天(也可设为第一天,根据需求调整)
|
|
|
- return formatDate(now)
|
|
|
- }
|
|
|
- return ''
|
|
|
-}
|
|
|
const applyFilter = () => {
|
|
|
// 将临时数据同步到正式表单
|
|
|
formData.value = JSON.parse(JSON.stringify(tempFormData.value))
|
|
|
@@ -324,6 +327,11 @@ onMounted(() => {
|
|
|
flex: none;
|
|
|
}
|
|
|
|
|
|
+ .uni-date {
|
|
|
+ width: px2rpx(250) !important;
|
|
|
+ flex: none;
|
|
|
+ }
|
|
|
+
|
|
|
.form-actions {
|
|
|
display: flex;
|
|
|
justify-content: flex-end;
|