优化字典管理弹窗逻辑

This commit is contained in:
sc 2021-07-12 15:48:04 +08:00
parent 72ffa8dce9
commit 424d9e1199
3 changed files with 150 additions and 121 deletions

View File

@ -1,24 +1,35 @@
<template> <template>
<el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="left"> <el-dialog :title="titleMap[mode]" v-model="visible" :width="330" destroy-on-close @closed="$emit('closed')">
<el-form-item label="编码" prop="code"> <el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="left">
<el-input v-model="form.code" clearable></el-input> <el-form-item label="编码" prop="code">
</el-form-item> <el-input v-model="form.code" clearable placeholder="字典编码"></el-input>
<el-form-item label="字典名称" prop="name"> </el-form-item>
<el-input v-model="form.name" clearable></el-input> <el-form-item label="字典名称" prop="name">
</el-form-item> <el-input v-model="form.name" clearable placeholder="字典显示名称"></el-input>
<el-form-item label="父路径" prop="parentId"> </el-form-item>
<el-cascader v-model="form.parentId" :options="dic" :props="dicProps" :show-all-levels="false" clearable></el-cascader> <el-form-item label="父路径" prop="parentId">
</el-form-item> <el-cascader v-model="form.parentId" :options="dic" :props="dicProps" :show-all-levels="false" clearable></el-cascader>
</el-form> </el-form-item>
</el-form>
<template #footer>
<el-button @click="visible=false" > </el-button>
<el-button type="primary" :loading="isSaveing" @click="submit()"> </el-button>
</template>
</el-dialog>
</template> </template>
<script> <script>
export default { export default {
props: { emits: ['success', 'closed'],
mode: { type: String, default: "add" }
},
data() { data() {
return { return {
mode: "add",
titleMap: {
add: '新增字典',
edit: '编辑字典'
},
visible: false,
isSaveing: false,
form: { form: {
id:"", id:"",
name: "", name: "",
@ -45,18 +56,31 @@
this.getDic() this.getDic()
}, },
methods: { methods: {
//
open(mode='add'){
this.mode = mode;
this.visible = true;
return this;
},
// //
async getDic(){ async getDic(){
var res = await this.$API.dic.list.get(); var res = await this.$API.dic.list.get();
this.dic = res.data; this.dic = res.data;
}, },
// //
submit(callback){ submit(){
this.$refs.dialogForm.validate((valid) => { this.$refs.dialogForm.validate(async (valid) => {
if (valid) { if (valid) {
callback(this.form) this.isSaveing = true;
}else{ var res = await this.$API.user.save.post(this.form);
return false; this.isSaveing = false;
if(res.code == 200){
this.$emit('success', this.form, this.mode)
this.visible = false;
this.$message.success("操作成功")
}else{
this.$alert(res.message, "提示", {type: 'error'})
}
} }
}) })
}, },

View File

@ -61,21 +61,9 @@
</el-container> </el-container>
</el-container> </el-container>
<el-dialog :title="titleMap[dicMode]" v-model="dicDialogVisible" :width="330" destroy-on-close> <dic-dialog v-if="dialog.dic" ref="dicDialog" @success="handleDicSuccess" @closed="dialog.dic=false"></dic-dialog>
<dic-dialog ref="dicDialog" :mode="dicMode"></dic-dialog>
<template #footer>
<el-button @click="dicDialogVisible=false" > </el-button>
<el-button type="primary" @click="saveDic()" :loading="isDicSaveing"> </el-button>
</template>
</el-dialog>
<el-dialog :title="titleMap[listMode]" v-model="listDialogVisible" :width="330" destroy-on-close> <list-dialog v-if="dialog.list" ref="listDialog" @success="handleListSuccess" @closed="dialog.list=false"></list-dialog>
<list-dialog ref="listDialog" :mode="listMode" :params="listDialogParams"></list-dialog>
<template #footer>
<el-button @click="listDialogVisible=false" > </el-button>
<el-button type="primary" @click="saveList()" :loading="isListSaveing"> </el-button>
</template>
</el-dialog>
</template> </template>
@ -92,15 +80,11 @@
}, },
data() { data() {
return { return {
showDicloading: true, dialog: {
dicDialogVisible: false, dic: false,
isDicSaveing: false, info: false
dicMode: 'add',
titleMap: {
add: "新增",
edit: "编辑",
show: "查看"
}, },
showDicloading: true,
dicList: [], dicList: [],
dicFilterText: '', dicFilterText: '',
dicProps: { dicProps: {
@ -108,10 +92,6 @@
}, },
listApi: null, listApi: null,
listApiParams: {}, listApiParams: {},
listDialogVisible: false,
listDialogParams: {},
isListSaveing: false,
listMode: 'add',
selection: [] selection: []
} }
}, },
@ -150,59 +130,19 @@
}, },
// //
addDic(){ addDic(){
this.dicMode = 'add'; this.dialog.dic = true
this.dicDialogVisible = true; this.$nextTick(() => {
this.$refs.dicDialog.open()
})
}, },
// //
dicEdit(data){ dicEdit(data){
this.dicMode = 'edit'; this.dialog.dic = true
this.dicDialogVisible = true;
this.$nextTick(() => { this.$nextTick(() => {
var editNode = this.$refs.dic.getNode(data.id); var editNode = this.$refs.dic.getNode(data.id);
var editNodeParentId = editNode.level==1?undefined:editNode.parent.data.id var editNodeParentId = editNode.level==1?undefined:editNode.parent.data.id
data.parentId = editNodeParentId data.parentId = editNodeParentId
this.$refs.dicDialog.setData(data) this.$refs.dicDialog.open('edit').setData(data)
})
},
//
saveDic(){
this.$refs.dicDialog.submit(async (formData) => {
this.isDicSaveing = true;
var res = await this.$API.user.save.post(formData);
this.isDicSaveing = false;
if(res.code == 200){
// OR /
if(this.dicMode == 'add'){
formData.id = (Math.random() * (500 - 400 + 1) | 0) + 400;
if(this.dicList.length > 0){
this.$refs.table.upData({
code: formData.code
})
}else{
this.listApiParams = {
code: formData.code
}
this.listApi = this.$API.dic.info;
}
this.$refs.dic.append(formData, formData.parentId[0])
this.$refs.dic.setCurrentKey(formData.id)
}
if(this.dicMode == 'edit'){
var editNode = this.$refs.dic.getNode(formData.id);
//
var editNodeParentId = editNode.level==1?undefined:editNode.parent.data.id
if(editNodeParentId != formData.parentId){
var obj = editNode.data;
this.$refs.dic.remove(formData.id)
this.$refs.dic.append(obj, formData.parentId[0])
}
Object.assign(editNode.data, formData)
}
this.dicDialogVisible = false;
this.$message.success("操作成功")
}else{
this.$alert(res.message, "提示", {type: 'error'})
}
}) })
}, },
// //
@ -258,17 +198,20 @@
}, },
// //
addInfo(){ addInfo(){
var dicCurrentKey = this.$refs.dic.getCurrentKey(); this.dialog.list = true
this.listDialogParams = {code: dicCurrentKey}; this.$nextTick(() => {
this.listMode = 'add'; var dicCurrentKey = this.$refs.dic.getCurrentKey();
this.listDialogVisible = true; const data = {
dic: dicCurrentKey
}
this.$refs.listDialog.open().setData(data)
})
}, },
// //
table_edit(row){ table_edit(row){
this.listMode = 'edit'; this.dialog.list = true
this.listDialogVisible = true;
this.$nextTick(() => { this.$nextTick(() => {
this.$refs.listDialog.setData(row) this.$refs.listDialog.open('edit').setData(row)
}) })
}, },
// //
@ -332,6 +275,45 @@
row.yx = val; row.yx = val;
this.$message.success(`操作成功id:${row.id} val:${val}`) this.$message.success(`操作成功id:${row.id} val:${val}`)
}, 500) }, 500)
},
//
handleDicSuccess(data, mode){
if(mode=='add'){
data.id = new Date().getTime()
if(this.dicList.length > 0){
this.$refs.table.upData({
code: data.code
})
}else{
this.listApiParams = {
code: data.code
}
this.listApi = this.$API.dic.info;
}
this.$refs.dic.append(data, data.parentId[0])
this.$refs.dic.setCurrentKey(data.id)
}else if(mode=='edit'){
var editNode = this.$refs.dic.getNode(data.id);
//
var editNodeParentId = editNode.level==1?undefined:editNode.parent.data.id
if(editNodeParentId != data.parentId){
var obj = editNode.data;
this.$refs.dic.remove(data.id)
this.$refs.dic.append(obj, data.parentId[0])
}
Object.assign(editNode.data, data)
}
},
//
handleListSuccess(data, mode){
if(mode=='add'){
data.id = new Date().getTime()
this.$refs.table.tableData.push(data)
}else if(mode=='edit'){
this.$refs.table.tableData.filter(item => item.id===data.id ).forEach(item => {
Object.assign(item, data)
})
}
} }
} }
} }

View File

@ -1,28 +1,38 @@
<template> <template>
<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left"> <el-dialog :title="titleMap[mode]" v-model="visible" :width="330" destroy-on-close @closed="$emit('closed')">
<el-form-item label="所属字典" prop="dic"> <el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
<el-cascader v-model="form.dic" :options="dic" :props="dicProps" :show-all-levels="false" clearable></el-cascader> <el-form-item label="所属字典" prop="dic">
</el-form-item> <el-cascader v-model="form.dic" :options="dic" :props="dicProps" :show-all-levels="false" clearable></el-cascader>
<el-form-item label="项名称" prop="name"> </el-form-item>
<el-input v-model="form.name" clearable></el-input> <el-form-item label="项名称" prop="name">
</el-form-item> <el-input v-model="form.name" clearable></el-input>
<el-form-item label="键值" prop="key"> </el-form-item>
<el-input v-model="form.key" clearable></el-input> <el-form-item label="键值" prop="key">
</el-form-item> <el-input v-model="form.key" clearable></el-input>
<el-form-item label="是否有效" prop="yx"> </el-form-item>
<el-switch v-model="form.yx" active-value="1" inactive-value="0"></el-switch> <el-form-item label="是否有效" prop="yx">
</el-form-item> <el-switch v-model="form.yx" active-value="1" inactive-value="0"></el-switch>
</el-form> </el-form-item>
</el-form>
<template #footer>
<el-button @click="visible=false" > </el-button>
<el-button type="primary" :loading="isSaveing" @click="submit()"> </el-button>
</template>
</el-dialog>
</template> </template>
<script> <script>
export default { export default {
props: { emits: ['success', 'closed'],
mode: { type: String, default: "add" },
params: { type: Object, default: () => {} }
},
data() { data() {
return { return {
mode: "add",
titleMap: {
add: '新增项',
edit: '编辑项'
},
visible: false,
isSaveing: false,
form: { form: {
id: "", id: "",
dic: "", dic: "",
@ -56,18 +66,31 @@
this.getDic() this.getDic()
}, },
methods: { methods: {
//
open(mode='add'){
this.mode = mode;
this.visible = true;
return this;
},
// //
async getDic(){ async getDic(){
var res = await this.$API.dic.list.get(); var res = await this.$API.dic.list.get();
this.dic = res.data; this.dic = res.data;
}, },
// //
submit(callback){ submit(){
this.$refs.dialogForm.validate((valid) => { this.$refs.dialogForm.validate(async (valid) => {
if (valid) { if (valid) {
callback(this.form) this.isSaveing = true;
}else{ var res = await this.$API.user.save.post(this.form);
return false; this.isSaveing = false;
if(res.code == 200){
this.$emit('success', this.form, this.mode)
this.visible = false;
this.$message.success("操作成功")
}else{
this.$alert(res.message, "提示", {type: 'error'})
}
} }
}) })
}, },