mt-yd-ui/src/components/base-dialog/addOrUpdate/index.vue

250 lines
7.1 KiB
Vue
Raw Normal View History

2022-08-08 11:01:15 +08:00
<template>
2022-08-08 16:51:49 +08:00
<el-dialog :title="isDetail ? title.detail : !dataForm.id ? title.add : title.edit" :visible.sync="visible" @close="handleClose">
2022-08-09 10:39:33 +08:00
<el-form :model="dataForm" :rules="dataFormRules">
2022-08-08 16:51:49 +08:00
<!-- 如果需要更精细一点的布局可以根据配置项实现地再复杂一点但此处暂时全部采用一行两列布局 -->
2022-08-09 10:39:33 +08:00
<el-row v-for="n in rows" :key="n" :gutter="20">
<el-col v-for="c in COLUMN_PER_ROW" :key="`${n}+'col'+${c}`" :span="24 / COLUMN_PER_ROW">
<!-- <el-form-item
v-for="field in configs.fields"
:key="field.name || field"
:prop="field.name || field"
:label="field.label || field.name | nameFilter || field | nameFilter"
> -->
<!-- <el-form-item
:prop="
typeof configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)] === 'string'
? configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)]
: configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)].type
"
:key="`${n}-col-${c}-item`"
:label="
`
${configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)].label ||
(configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)].name && defaultNames[configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)].name]) ||
defaultNames[configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)]]}
`
"
> -->
<el-form-item :prop="`${n}-test-${c}`" :key="`${n}-col-${c}-item`" :label="'f'">
{{ n - c - JSON.stringify(configs.fields[(n - 1) * COLUMN_PER_ROW + (c - 1)]) }}
<!-- 暂时先不实现部分输入方式 -->
<!-- <el-input></el-input>
<el-radio></el-radio>
<el-checkbox></el-checkbox>
<el-select></el-select>
<el-switch></el-switch>
<el-cascader></el-cascader>
<el-time-select></el-time-select>
<el-date-picker></el-date-picker> -->
</el-form-item>
</el-col>
</el-row>
2022-08-09 09:41:30 +08:00
<!-- extra components , like Markdown or RichEdit -->
2022-08-09 10:39:33 +08:00
<template v-if="configs.extraComponents && configs.extraComponents.length > 0">
<el-form-item v-for="ec in configs.extraComponents" :key="ec.name" :label="ec.label">
2022-08-09 09:41:30 +08:00
<component :is="ec.component" v-model="dataForm[ec.name]"></component>
</el-form-item>
</template>
2022-08-08 16:51:49 +08:00
</el-form>
<span slot="footer" class="dialog-footer">
<el-button v-for="(operate, index) in configs.operations" :key="`operate-${index}`" :type="btnType[operate.name]" @click="handleClick(operate)">
<!-- {{ operate.name | btnNameFilter }} -->
{{ btnName[operate.name] }}
</el-button>
</span>
</el-dialog>
2022-08-08 16:22:04 +08:00
</template>
<script>
2022-08-08 16:51:49 +08:00
// 标题 for i18n
const title = {
detail: '详情',
add: '新增',
edit: '编辑'
}
// 或者也可以改造成自定义颜色:
const btnType = {
save: 'success',
update: 'primary',
reset: 'text'
// add more...
}
const btnName = {
// for i18n
save: '保存',
update: '更新',
reset: '重置'
// add more...
}
2022-08-08 16:22:58 +08:00
2022-08-08 16:51:49 +08:00
// 每行的列数
const COLUMN_PER_ROW = 2
2022-08-08 16:22:58 +08:00
2022-08-08 16:22:04 +08:00
export default {
name: 'AddOrUpdateDialog',
2022-08-08 16:51:49 +08:00
props: {
configs: {
/**
* type: 'dialog' | 'drawer' | 'page'
* fields: Array<string|object>
* - fields.object: { name, type: 'number'|'textarea'|'select'|'date'|.., required: boolean, validator: boolean(是否需要验证), [options]: any[], api: string(自动获取数据的接口一般为getcode接口)}
* operations: Array[object], 操作名和对应的接口地址
*/
type: Object,
default: () => ({}) // 此处省去类型检查,使用者自行注意就好
}
},
2022-08-09 10:39:33 +08:00
filters: {
nameFilter: function(name) {
if (!name) return null
// for i18n
const defaultNames = {
name: '名称',
code: '编码',
remark: '备注',
specifications: '规格'
// add more...
}
return defaultNames[name]
}
},
2022-08-08 16:22:04 +08:00
data() {
return {
2022-08-09 10:39:33 +08:00
COLUMN_PER_ROW,
2022-08-08 16:51:49 +08:00
title,
btnName,
btnType,
2022-08-08 16:22:04 +08:00
visible: false,
isEdit: false,
isDetail: false,
// cached: false // 不采用缓存比较的方案了,采用 updated 方案: 如果更新了dataForm就在 confirm 时 emit(refreshDataList)
2022-08-09 09:41:30 +08:00
isUpdated: false,
2022-08-09 10:39:33 +08:00
dataForm: {},
dataFormRules: {},
defaultNames: {
name: '名称',
code: '编码',
remark: '备注',
specifications: '规格'
// add more...
}
2022-08-08 16:22:04 +08:00
}
},
2022-08-08 16:51:49 +08:00
computed: {
rows() {
// 本组件只实现了'一行两列'的表单布局
return Math.ceil(this.configs.fields.length / COLUMN_PER_ROW)
}
},
2022-08-09 09:41:30 +08:00
mounted() {
this.$nextTick(() => {
/** 动态设置dataForm字段 */
this.configs.fields.forEach(item => {
if (typeof item === 'string') {
this.$set(this.dataForm, [item], '')
} else if (typeof item === 'object') {
2022-08-09 10:39:33 +08:00
this.$set(this.dataForm, [item.name], '')
2022-08-09 09:41:30 +08:00
if (item.api) {
/** 自动请求并填充 */
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)
}
})
/** 检查是否需要额外的组件 */
2022-08-09 10:39:33 +08:00
this.configs.extraComponents &&
this.configs.extraComponents.forEach(item => {
this.$set(this.dataForm, [item.name], '')
})
/** 单独设置 id */
this.$set(this.dataForm, 'id', null)
2022-08-09 09:41:30 +08:00
// TODOdelete next lines
console.log('dataform: ', this.dataForm)
console.log('rules: ', this.dataFormRules)
})
},
2022-08-08 16:22:04 +08:00
updated() {
this.isUpdated = true // 此时如果点击保存就会 emit(refreshDataList)
},
// beforeDestroy() {
// 缓存比较方案:
// 在组件快要销毁时比较localStorage和dataForm里的值
// 如果有改变则 emit
// 否则直接销毁
// 清除localStorage里的缓存
// if (cached && compareCache(this.dataForm, localStorage...) || !isEdit) {
// // 如果是编辑页面并且已经更新了内容或者是新增页面就emit刷新列表
// clearCache()
// this.$emit('refreshDataList')
// }
// },
methods: {
init() {
this.visible = true
},
2022-08-08 16:51:49 +08:00
handleClick(btn) {
switch (btn.name) {
case 'save':
break
case 'update':
break
case 'reset':
break
2022-08-09 09:41:30 +08:00
// add more..
2022-08-08 16:51:49 +08:00
}
},
2022-08-08 16:22:04 +08:00
handleClose() {
if (this.isAdd || this.isUpdated) this.$emit('refreshDataList')
this.visible = false
}
}
}
</script>