提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<Dialog :title="dialogTitle" v-model="dialogVisible"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
:model="formData" |
|
6 |
:rules="formRules" |
|
7 |
label-width="100px" |
|
8 |
v-loading="formLoading" |
|
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="sex"> |
|
14 |
<el-radio-group v-model="formData.sex"> |
|
15 |
<el-radio |
|
16 |
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)" |
|
17 |
:key="dict.value" |
|
18 |
:label="dict.value" |
|
19 |
> |
|
20 |
{{ dict.label }} |
|
21 |
</el-radio> |
|
22 |
</el-radio-group> |
|
23 |
</el-form-item> |
|
24 |
<el-form-item label="出生日期" prop="birthday"> |
|
25 |
<el-date-picker |
|
26 |
v-model="formData.birthday" |
|
27 |
type="date" |
|
28 |
value-format="x" |
|
29 |
placeholder="选择出生日期" |
|
30 |
/> |
|
31 |
</el-form-item> |
|
32 |
<el-form-item label="简介" prop="description"> |
|
33 |
<Editor v-model="formData.description" height="150px" /> |
|
34 |
</el-form-item> |
|
35 |
</el-form> |
|
36 |
<!-- 子表的表单 --> |
|
37 |
<el-tabs v-model="subTabsName"> |
|
38 |
<el-tab-pane label="学生课程" name="demo03Course"> |
|
39 |
<Demo03CourseForm ref="demo03CourseFormRef" :student-id="formData.id" /> |
|
40 |
</el-tab-pane> |
|
41 |
<el-tab-pane label="学生班级" name="demo03Grade"> |
|
42 |
<Demo03GradeForm ref="demo03GradeFormRef" :student-id="formData.id" /> |
|
43 |
</el-tab-pane> |
|
44 |
</el-tabs> |
|
45 |
<template #footer> |
|
46 |
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> |
|
47 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
48 |
</template> |
|
49 |
</Dialog> |
|
50 |
</template> |
|
51 |
<script setup lang="ts"> |
|
52 |
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict' |
|
53 |
import * as Demo03StudentApi from '@/api/infra/demo/demo03/normal' |
|
54 |
import Demo03CourseForm from './components/Demo03CourseForm.vue' |
|
55 |
import Demo03GradeForm from './components/Demo03GradeForm.vue' |
|
56 |
|
|
57 |
const { t } = useI18n() // 国际化 |
|
58 |
const message = useMessage() // 消息弹窗 |
|
59 |
|
|
60 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
61 |
const dialogTitle = ref('') // 弹窗的标题 |
|
62 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
63 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
64 |
const formData = ref({ |
|
65 |
id: undefined, |
|
66 |
name: undefined, |
|
67 |
sex: undefined, |
|
68 |
birthday: undefined, |
|
69 |
description: undefined |
|
70 |
}) |
|
71 |
const formRules = reactive({ |
|
72 |
name: [{ required: true, message: '名字不能为空', trigger: 'blur' }], |
|
73 |
sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }], |
|
74 |
birthday: [{ required: true, message: '出生日期不能为空', trigger: 'blur' }], |
|
75 |
description: [{ required: true, message: '简介不能为空', trigger: 'blur' }] |
|
76 |
}) |
|
77 |
const formRef = ref() // 表单 Ref |
|
78 |
|
|
79 |
/** 子表的表单 */ |
|
80 |
const subTabsName = ref('demo03Course') |
|
81 |
const demo03CourseFormRef = ref() |
|
82 |
const demo03GradeFormRef = ref() |
|
83 |
|
|
84 |
/** 打开弹窗 */ |
|
85 |
const open = async (type: string, id?: number) => { |
|
86 |
dialogVisible.value = true |
|
87 |
dialogTitle.value = t('action.' + type) |
|
88 |
formType.value = type |
|
89 |
resetForm() |
|
90 |
// 修改时,设置数据 |
|
91 |
if (id) { |
|
92 |
formLoading.value = true |
|
93 |
try { |
|
94 |
formData.value = await Demo03StudentApi.getDemo03Student(id) |
|
95 |
} finally { |
|
96 |
formLoading.value = false |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
101 |
|
|
102 |
/** 提交表单 */ |
|
103 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
104 |
const submitForm = async () => { |
|
105 |
// 校验表单 |
|
106 |
await formRef.value.validate() |
|
107 |
// 校验子表单 |
|
108 |
try { |
|
109 |
await demo03CourseFormRef.value.validate() |
|
110 |
} catch (e) { |
|
111 |
subTabsName.value = 'demo03Course' |
|
112 |
return |
|
113 |
} |
|
114 |
try { |
|
115 |
await demo03GradeFormRef.value.validate() |
|
116 |
} catch (e) { |
|
117 |
subTabsName.value = 'demo03Grade' |
|
118 |
return |
|
119 |
} |
|
120 |
// 提交请求 |
|
121 |
formLoading.value = true |
|
122 |
try { |
|
123 |
const data = formData.value as unknown as Demo03StudentApi.Demo03StudentVO |
|
124 |
// 拼接子表的数据 |
|
125 |
data.demo03Courses = demo03CourseFormRef.value.getData() |
|
126 |
data.demo03Grade = demo03GradeFormRef.value.getData() |
|
127 |
if (formType.value === 'create') { |
|
128 |
await Demo03StudentApi.createDemo03Student(data) |
|
129 |
message.success(t('common.createSuccess')) |
|
130 |
} else { |
|
131 |
await Demo03StudentApi.updateDemo03Student(data) |
|
132 |
message.success(t('common.updateSuccess')) |
|
133 |
} |
|
134 |
dialogVisible.value = false |
|
135 |
// 发送操作成功的事件 |
|
136 |
emit('success') |
|
137 |
} finally { |
|
138 |
formLoading.value = false |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
/** 重置表单 */ |
|
143 |
const resetForm = () => { |
|
144 |
formData.value = { |
|
145 |
id: undefined, |
|
146 |
name: undefined, |
|
147 |
sex: undefined, |
|
148 |
birthday: undefined, |
|
149 |
description: undefined |
|
150 |
} |
|
151 |
formRef.value?.resetFields() |
|
152 |
} |
|
153 |
</script> |