6
6
:rules =" formRules"
7
7
label-width =" 100px"
8
8
v-loading =" formLoading"
9
- />
10
- <!-- 子表的表单 -->
11
- <el-tabs v-model =" subTabsName" >
12
- <el-tab-pane label =" 学生联系人" name =" demoStudentContact" >
13
- <DemoStudentContactForm ref =" demoStudentContactFormRef" :student-id =" formData.id" />
14
- </el-tab-pane >
15
- <el-tab-pane label =" 学生地址" name =" demoStudentAddress" >
16
- <DemoStudentAddressForm ref =" demoStudentAddressFormRef" :student-id =" formData.id" />
17
- </el-tab-pane >
18
- </el-tabs >
9
+ >
10
+ <el-form-item label =" 名字" prop =" name" >
11
+ <el-input v-model =" formData.name" placeholder =" 请输入名字" />
12
+ </el-form-item >
13
+ <el-form-item label =" 父级编号" prop =" parentId" >
14
+ <el-tree-select
15
+ v-model =" formData.parentId"
16
+ :data =" demo02CategoryTree"
17
+ :props =" defaultProps"
18
+ check-strictly
19
+ default-expand-all
20
+ placeholder =" 请选择父级编号"
21
+ />
22
+ </el-form-item >
23
+ </el-form >
19
24
<template #footer >
20
25
<el-button @click =" submitForm" type =" primary" :disabled =" formLoading" >确 定</el-button >
21
26
<el-button @click =" dialogVisible = false" >取 消</el-button >
22
27
</template >
23
28
</Dialog >
24
29
</template >
25
30
<script setup lang="ts">
26
- import * as DemoStudentApi from ' @/api/infra/demo02'
27
- import DemoStudentContactForm from ' ./DemoStudentContactForm.vue'
28
- import DemoStudentAddressForm from ' ./DemoStudentAddressForm.vue'
31
+ import * as Demo02CategoryApi from ' @/api/infra/demo/demo02'
32
+ import { defaultProps , handleTree } from ' @/utils/tree'
29
33
30
34
const { t } = useI18n () // 国际化
31
35
const message = useMessage () // 消息弹窗
@@ -35,15 +39,16 @@ const dialogTitle = ref('') // 弹窗的标题
35
39
const formLoading = ref (false ) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
36
40
const formType = ref (' ' ) // 表单的类型:create - 新增;update - 修改
37
41
const formData = ref ({
38
- id: undefined
42
+ id: undefined ,
43
+ name: undefined ,
44
+ parentId: undefined
45
+ })
46
+ const formRules = reactive ({
47
+ name: [{ required: true , message: ' 名字不能为空' , trigger: ' blur' }],
48
+ parentId: [{ required: true , message: ' 父级编号不能为空' , trigger: ' blur' }]
39
49
})
40
- const formRules = reactive ({})
41
50
const formRef = ref () // 表单 Ref
42
-
43
- /** 子表的表单 */
44
- const subTabsName = ref (' demoStudentContact' )
45
- const demoStudentContactFormRef = ref ()
46
- const demoStudentAddressFormRef = ref ()
51
+ const demo02CategoryTree = ref () // 树形结构
47
52
48
53
/** 打开弹窗 */
49
54
const open = async (type : string , id ? : number ) => {
@@ -55,11 +60,12 @@ const open = async (type: string, id?: number) => {
55
60
if (id ) {
56
61
formLoading .value = true
57
62
try {
58
- formData .value = await DemoStudentApi . getDemoStudent (id )
63
+ formData .value = await Demo02CategoryApi . getDemo02Category (id )
59
64
} finally {
60
65
formLoading .value = false
61
66
}
62
67
}
68
+ await getDemo02CategoryTree ()
63
69
}
64
70
defineExpose ({ open }) // 提供 open 方法,用于打开弹窗
65
71
@@ -68,31 +74,15 @@ const emit = defineEmits(['success']) // 定义 success 事件,用于操作成
68
74
const submitForm = async () => {
69
75
// 校验表单
70
76
await formRef .value .validate ()
71
- // 校验子表单
72
- try {
73
- await demoStudentContactFormRef .value .validate ()
74
- } catch (e ) {
75
- subTabsName .value = ' demoStudentContact'
76
- return
77
- }
78
- try {
79
- await demoStudentAddressFormRef .value .validate ()
80
- } catch (e ) {
81
- subTabsName .value = ' demoStudentAddress'
82
- return
83
- }
84
77
// 提交请求
85
78
formLoading .value = true
86
79
try {
87
- const data = formData .value as unknown as DemoStudentApi .DemoStudentVO
88
- // 拼接子表的数据
89
- data .demoStudentContacts = demoStudentContactFormRef .value .getData ()
90
- data .demoStudentAddress = demoStudentAddressFormRef .value .getData ()
80
+ const data = formData .value as unknown as Demo02CategoryApi .Demo02CategoryVO
91
81
if (formType .value === ' create' ) {
92
- await DemoStudentApi . createDemoStudent (data )
82
+ await Demo02CategoryApi . createDemo02Category (data )
93
83
message .success (t (' common.createSuccess' ))
94
84
} else {
95
- await DemoStudentApi . updateDemoStudent (data )
85
+ await Demo02CategoryApi . updateDemo02Category (data )
96
86
message .success (t (' common.updateSuccess' ))
97
87
}
98
88
dialogVisible .value = false
@@ -106,8 +96,19 @@ const submitForm = async () => {
106
96
/** 重置表单 */
107
97
const resetForm = () => {
108
98
formData .value = {
109
- id: undefined
99
+ id: undefined ,
100
+ name: undefined ,
101
+ parentId: undefined
110
102
}
111
103
formRef .value ?.resetFields ()
112
104
}
113
- </script >
105
+
106
+ /** 获得示例分类树 */
107
+ const getDemo02CategoryTree = async () => {
108
+ demo02CategoryTree .value = []
109
+ const data = await Demo02CategoryApi .getDemo02CategoryList ()
110
+ const root: Tree = { id: 0 , name: ' 顶级示例分类' , children: [] }
111
+ root .children = handleTree (data , ' id' , ' parentId' )
112
+ demo02CategoryTree .value .push (root )
113
+ }
114
+ </script >
0 commit comments