update dialog

This commit is contained in:
lb 2022-08-09 09:41:30 +08:00
parent 32dd90969a
commit 0733660c2f
2 changed files with 93 additions and 3 deletions

View File

@ -2,6 +2,13 @@
<el-dialog :title="isDetail ? title.detail : !dataForm.id ? title.add : title.edit" :visible.sync="visible" @close="handleClose"> <el-dialog :title="isDetail ? title.detail : !dataForm.id ? title.add : title.edit" :visible.sync="visible" @close="handleClose">
<el-form> <el-form>
<!-- 如果需要更精细一点的布局可以根据配置项实现地再复杂一点但此处暂时全部采用一行两列布局 --> <!-- 如果需要更精细一点的布局可以根据配置项实现地再复杂一点但此处暂时全部采用一行两列布局 -->
<!-- extra components , like Markdown or RichEdit -->
<template v-if="extraComponents.length > 0">
<el-form-item v-for="ec in extraComponents" :key="ec.name" :label="ec.label">
<component :is="ec.component" v-model="dataForm[ec.name]"></component>
</el-form-item>
</template>
</el-form> </el-form>
<span slot="footer" class="dialog-footer"> <span slot="footer" class="dialog-footer">
@ -63,7 +70,9 @@ export default {
isEdit: false, isEdit: false,
isDetail: false, isDetail: false,
// cached: false // updated dataForm confirm emit(refreshDataList) // cached: false // updated dataForm confirm emit(refreshDataList)
isUpdated: false isUpdated: false,
dataForm: null,
dataFormRules: {}
} }
}, },
computed: { computed: {
@ -72,6 +81,71 @@ export default {
return Math.ceil(this.configs.fields.length / COLUMN_PER_ROW) return Math.ceil(this.configs.fields.length / COLUMN_PER_ROW)
} }
}, },
mounted() {
this.$nextTick(() => {
/** 动态设置dataForm字段 */
this.configs.fields.forEach(item => {
if (typeof item === 'string') {
this.$set(this.dataForm, [item], '')
} else if (typeof item === 'object') {
if (item.api) {
/** 自动请求并填充 */
this.$set(this.dataForm, [item.name], '')
this.$http({
url: this.$http.adornUrl(item.api),
methods: 'get'
}).then(({ data: res }) => {
if (data & (data.code === 0)) {
this.dataFrom[item.name] = res.data // <===
}
})
} // end if (item.api)
if (item.required) {
const requiredRule = {
required: true,
message: '请输入必填项',
trigger: 'change'
}
/** 检查是否已经存在该字段的规则 */
const exists = this.dataFormRules[item.name] || null
/** 设置验证规则 */
if (exists) {
const unset = true
for (const rule of exists) {
if (rule.required) unset = false
}
if (unset) {
exists.push(requiredRule)
}
} else {
/** 不存在已有规则 */
this.$set(this.dataFormRules, [item.name], [requiredRule])
}
} // end if (item.required)
if (item.rules) {
const exists = this.dataFormRules[item.name] || null
if (exists) {
//
exists.push(...item.rules)
} else {
this.$set(this.dataFormRules, [item.name], [...item.rules])
}
} // end if (item.rules)
}
})
/** 检查是否需要额外的组件 */
this.configs.extraComponents.forEach(item => {
this.$set(this.dataForm, [item.name], '')
})
// TODOdelete next lines
console.log('dataform: ', this.dataForm)
console.log('rules: ', this.dataFormRules)
})
},
updated() { updated() {
this.isUpdated = true // emit(refreshDataList) this.isUpdated = true // emit(refreshDataList)
}, },
@ -99,6 +173,7 @@ export default {
break break
case 'reset': case 'reset':
break break
// add more..
} }
}, },
handleClose() { handleClose() {

View File

@ -103,7 +103,15 @@ const addOrUpdateConfigs = {
name: 'processTime', name: 'processTime',
type: 'number', // type: number(input+number) | default(input) | textarea | select(options) | datetime type: 'number', // type: number(input+number) | default(input) | textarea | select(options) | datetime
required: true, required: true,
validator: false // rules: [
// required
{
type: 'number',
trigger: 'blur',
transform: val => Number(val),
message: '必须输入数字'
}
]
}, },
'remark', 'remark',
'specifications', 'specifications',
@ -130,7 +138,14 @@ const addOrUpdateConfigs = {
{ name: 'save', url: 'api/product/add' }, { name: 'save', url: 'api/product/add' },
{ name: 'update', url: 'api/product/update' }, { name: 'update', url: 'api/product/update' },
{ name: 'reset', url: true } { name: 'reset', url: true }
] ],
// extraComponents: [
// {
// name: 'CompName',
// label: 'markdown',
// component: () => import('xx.vue')
// }
// ]
} }
export default { export default {