style:维修项目-新增/编辑-设备类型切换时重置设备下拉框内容

main
黄伟杰 2 weeks ago
parent 9e3e82f577
commit e4b82b4b26

@ -95,6 +95,9 @@ const formLoading = ref(false)
const formType = ref('') const formType = ref('')
const formRef = ref() const formRef = ref()
const isInitializing = ref(false)
const isSwitchingDeviceType = ref(false)
const formData = ref({ const formData = ref({
id: undefined as number | undefined, id: undefined as number | undefined,
subjectCode: undefined as string | undefined, subjectCode: undefined as string | undefined,
@ -163,15 +166,16 @@ const loadComponentOptionsByDeviceId = async (deviceId: number) => {
watch( watch(
() => formData.value.deviceType, () => formData.value.deviceType,
(val) => { async () => {
if (val === 1) { if (isInitializing.value) return
formData.value.componentId = undefined isSwitchingDeviceType.value = true
componentOptions.value = []
} else if (val !== 2) {
formData.value.deviceId = undefined formData.value.deviceId = undefined
deviceOptions.value = []
formData.value.componentId = undefined formData.value.componentId = undefined
componentOptions.value = [] componentOptions.value = []
} await nextTick()
formRef.value?.clearValidate?.(['deviceId', 'componentId'])
isSwitchingDeviceType.value = false
} }
) )
@ -187,6 +191,10 @@ watch(
) )
const validateDeviceId = (_: any, value: any, callback: any) => { const validateDeviceId = (_: any, value: any, callback: any) => {
if (isSwitchingDeviceType.value) {
callback()
return
}
const dt = formData.value.deviceType const dt = formData.value.deviceType
if ((dt === 1 || dt === 2) && (value === undefined || value === null || value === '')) { if ((dt === 1 || dt === 2) && (value === undefined || value === null || value === '')) {
callback(new Error('设备不能为空')) callback(new Error('设备不能为空'))
@ -196,6 +204,10 @@ const validateDeviceId = (_: any, value: any, callback: any) => {
} }
const validateComponentId = (_: any, value: any, callback: any) => { const validateComponentId = (_: any, value: any, callback: any) => {
if (isSwitchingDeviceType.value) {
callback()
return
}
const dt = formData.value.deviceType const dt = formData.value.deviceType
if (dt === 2 && (value === undefined || value === null || value === '')) { if (dt === 2 && (value === undefined || value === null || value === '')) {
callback(new Error('关键件不能为空')) callback(new Error('关键件不能为空'))
@ -205,18 +217,19 @@ const validateComponentId = (_: any, value: any, callback: any) => {
} }
const formRules = reactive({ const formRules = reactive({
subjectCode: [{ required: true, message: '项目编码不能为空', trigger: 'blur' }], subjectCode: [{ required: true, message: '项目编码不能为空' }],
subjectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }], subjectName: [{ required: true, message: '项目名称不能为空' }],
deviceType: [{ required: true, message: '设备类型不能为空', trigger: 'change' }], deviceType: [{ required: true, message: '设备类型不能为空' }],
deviceId: [{ validator: validateDeviceId, trigger: 'change' }], deviceId: [{ validator: validateDeviceId }],
componentId: [{ validator: validateComponentId, trigger: 'change' }], componentId: [{ validator: validateComponentId }],
isEnable: [{ required: true, message: '是否启用不能为空', trigger: 'change' }] isEnable: [{ required: true, message: '是否启用不能为空' }]
}) })
const open = async (type: string, row?: any) => { const open = async (type: string, row?: any) => {
dialogVisible.value = true dialogVisible.value = true
dialogTitle.value = t('action.' + type) dialogTitle.value = t('action.' + type)
formType.value = type formType.value = type
isInitializing.value = true
resetForm() resetForm()
if (!dictReady.value) { if (!dictReady.value) {
await dictStore.setDictMap() await dictStore.setDictMap()
@ -249,6 +262,9 @@ const open = async (type: string, row?: any) => {
} }
} }
} }
await nextTick()
formRef.value?.clearValidate?.()
isInitializing.value = false
} }
defineExpose({ open }) defineExpose({ open })

Loading…
Cancel
Save