提交 | 用户 | 时间
|
759b1c
|
1 |
<template> |
H |
2 |
<div class="app-container"> |
|
3 |
<!-- 对话框(添加 / 修改) --> |
|
4 |
<el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body> |
|
5 |
<el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px"> |
|
6 |
<el-form-item label="名字" prop="name"> |
|
7 |
<el-input v-model="formData.name" placeholder="请输入名字" /> |
|
8 |
</el-form-item> |
|
9 |
<el-form-item label="父级编号" prop="parentId"> |
|
10 |
<TreeSelect |
|
11 |
v-model="formData.parentId" |
|
12 |
:options="demo02CategoryTree" |
|
13 |
:normalizer="normalizer" |
|
14 |
placeholder="请选择父级编号" |
|
15 |
/> |
|
16 |
</el-form-item> |
|
17 |
</el-form> |
|
18 |
<div slot="footer" class="dialog-footer"> |
|
19 |
<el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button> |
|
20 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
21 |
</div> |
|
22 |
</el-dialog> |
|
23 |
</div> |
|
24 |
</template> |
|
25 |
|
|
26 |
<script> |
|
27 |
import * as Demo02CategoryApi from '@/api/infra/demo02'; |
|
28 |
import TreeSelect from "@riophae/vue-treeselect"; |
|
29 |
import "@riophae/vue-treeselect/dist/vue-treeselect.css"; |
|
30 |
export default { |
|
31 |
name: "Demo02CategoryForm", |
|
32 |
components: { |
|
33 |
TreeSelect, |
|
34 |
}, |
|
35 |
data() { |
|
36 |
return { |
|
37 |
// 弹出层标题 |
|
38 |
dialogTitle: "", |
|
39 |
// 是否显示弹出层 |
|
40 |
dialogVisible: false, |
|
41 |
// 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
42 |
formLoading: false, |
|
43 |
// 表单参数 |
|
44 |
formData: { |
|
45 |
id: undefined, |
|
46 |
name: undefined, |
|
47 |
parentId: undefined, |
|
48 |
}, |
|
49 |
// 表单校验 |
|
50 |
formRules: { |
|
51 |
name: [{ required: true, message: '名字不能为空', trigger: 'blur' }], |
|
52 |
parentId: [{ required: true, message: '父级编号不能为空', trigger: 'blur' }], |
|
53 |
}, |
|
54 |
demo02CategoryTree: [], // 树形结构 |
|
55 |
}; |
|
56 |
}, |
|
57 |
methods: { |
|
58 |
/** 打开弹窗 */ |
|
59 |
async open(id) { |
|
60 |
this.dialogVisible = true; |
|
61 |
this.reset(); |
|
62 |
// 修改时,设置数据 |
|
63 |
if (id) { |
|
64 |
this.formLoading = true; |
|
65 |
try { |
|
66 |
const res = await Demo02CategoryApi.getDemo02Category(id); |
|
67 |
this.formData = res.data; |
|
68 |
this.title = "修改示例分类"; |
|
69 |
} finally { |
|
70 |
this.formLoading = false; |
|
71 |
} |
|
72 |
} |
|
73 |
this.title = "新增示例分类"; |
|
74 |
await this.getDemo02CategoryTree(); |
|
75 |
}, |
|
76 |
/** 提交按钮 */ |
|
77 |
async submitForm() { |
|
78 |
// 校验主表 |
|
79 |
await this.$refs["formRef"].validate(); |
|
80 |
this.formLoading = true; |
|
81 |
try { |
|
82 |
const data = this.formData; |
|
83 |
// 修改的提交 |
|
84 |
if (data.id) { |
|
85 |
await Demo02CategoryApi.updateDemo02Category(data); |
|
86 |
this.$modal.msgSuccess("修改成功"); |
|
87 |
this.dialogVisible = false; |
|
88 |
this.$emit('success'); |
|
89 |
return; |
|
90 |
} |
|
91 |
// 添加的提交 |
|
92 |
await Demo02CategoryApi.createDemo02Category(data); |
|
93 |
this.$modal.msgSuccess("新增成功"); |
|
94 |
this.dialogVisible = false; |
|
95 |
this.$emit('success'); |
|
96 |
} finally { |
|
97 |
this.formLoading = false; |
|
98 |
} |
|
99 |
}, |
|
100 |
/** 获得示例分类树 */ |
|
101 |
async getDemo02CategoryTree() { |
|
102 |
this.demo02CategoryTree = []; |
|
103 |
const res = await Demo02CategoryApi.getDemo02CategoryList(); |
|
104 |
const root = { id: 0, name: '顶级示例分类', children: [] }; |
|
105 |
root.children = this.handleTree(res.data, 'id', 'parentId') |
|
106 |
this.demo02CategoryTree.push(root) |
|
107 |
}, |
|
108 |
/** 转换示例分类数据结构 */ |
|
109 |
normalizer(node) { |
|
110 |
if (node.children && !node.children.length) { |
|
111 |
delete node.children; |
|
112 |
} |
|
113 |
return { |
|
114 |
id: node.id, |
|
115 |
label: node.name, |
|
116 |
children: node.children |
|
117 |
}; |
|
118 |
}, |
|
119 |
/** 表单重置 */ |
|
120 |
reset() { |
|
121 |
this.formData = { |
|
122 |
id: undefined, |
|
123 |
name: undefined, |
|
124 |
parentId: undefined, |
|
125 |
}; |
|
126 |
this.resetForm("formRef"); |
|
127 |
} |
|
128 |
} |
|
129 |
}; |
|
130 |
</script> |