houzhongjian
2024-07-11 759b1c71011abd6b58c37d2566f3f3c208c2f1b2
提交 | 用户 | 时间
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="sex">
10           <el-radio-group v-model="formData.sex">
11             <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
12                       :key="dict.value" :label="parseInt(dict.value)"
13             >{{dict.label}}</el-radio>
14           </el-radio-group>
15         </el-form-item>
16         <el-form-item label="出生日期" prop="birthday">
17           <el-date-picker clearable v-model="formData.birthday" type="date" value-format="timestamp" placeholder="选择出生日期" />
18         </el-form-item>
19         <el-form-item label="简介">
20           <Editor v-model="formData.description" :min-height="192"/>
21         </el-form-item>
22       </el-form>
23       <!-- 子表的表单 -->
24       <el-tabs v-model="subTabsName">
25         <el-tab-pane label="学生课程" name="demo03Course">
26           <Demo03CourseForm ref="demo03CourseFormRef" :student-id="formData.id" />
27         </el-tab-pane>
28         <el-tab-pane label="学生班级" name="demo03Grade">
29           <Demo03GradeForm ref="demo03GradeFormRef" :student-id="formData.id" />
30         </el-tab-pane>
31       </el-tabs>
32       <div slot="footer" class="dialog-footer">
33         <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
34         <el-button @click="dialogVisible = false">取 消</el-button>
35       </div>
36     </el-dialog>
37   </div>
38 </template>
39
40 <script>
41 import * as Demo03StudentApi from '@/api/infra/demo03-inner';
42 import Editor from '@/components/Editor';
43 import Demo03CourseForm from './components/Demo03CourseForm.vue';
44 import Demo03GradeForm from './components/Demo03GradeForm.vue';
45 export default {
46   name: "Demo03StudentForm",
47   components: {
48     Editor,
49     Demo03CourseForm,
50     Demo03GradeForm,
51   },
52   data() {
53     return {
54       // 弹出层标题
55       dialogTitle: "",
56       // 是否显示弹出层
57       dialogVisible: false,
58       // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
59       formLoading: false,
60       // 表单参数
61       formData: {
62         id: undefined,
63         name: undefined,
64         sex: undefined,
65         birthday: undefined,
66         description: undefined,
67       },
68       // 表单校验
69       formRules: {
70         name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
71         sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
72         birthday: [{ required: true, message: '出生日期不能为空', trigger: 'blur' }],
73         description: [{ required: true, message: '简介不能为空', trigger: 'blur' }],
74       },
75       /** 子表的表单 */
76       subTabsName: 'demo03Course'
77     };
78   },
79   methods: {
80     /** 打开弹窗 */
81     async open(id) {
82       this.dialogVisible = true;
83       this.reset();
84       // 修改时,设置数据
85       if (id) {
86         this.formLoading = true;
87         try {
88           const res = await Demo03StudentApi.getDemo03Student(id);
89           this.formData = res.data;
90           this.title = "修改学生";
91         } finally {
92           this.formLoading = false;
93         }
94       }
95       this.title = "新增学生";
96     },
97     /** 提交按钮 */
98     async submitForm() {
99       // 校验主表
100       await this.$refs["formRef"].validate();
101       // 校验子表
102       try {
103         await this.$refs['demo03CourseFormRef'].validate();
104       } catch (e) {
105         this.subTabsName = 'demo03Course';
106         return;
107       }
108       try {
109         await this.$refs['demo03GradeFormRef'].validate();
110       } catch (e) {
111         this.subTabsName = 'demo03Grade';
112         return;
113       }
114       this.formLoading = true;
115       try {
116         const data = this.formData;
117         // 拼接子表的数据
118         data.demo03Courses = this.$refs['demo03CourseFormRef'].getData();
119         data.demo03Grade = this.$refs['demo03GradeFormRef'].getData();
120         // 修改的提交
121         if (data.id) {
122           await Demo03StudentApi.updateDemo03Student(data);
123           this.$modal.msgSuccess("修改成功");
124           this.dialogVisible = false;
125           this.$emit('success');
126           return;
127         }
128         // 添加的提交
129         await Demo03StudentApi.createDemo03Student(data);
130         this.$modal.msgSuccess("新增成功");
131         this.dialogVisible = false;
132         this.$emit('success');
133       } finally {
134         this.formLoading = false;
135       }
136     },
137     /** 表单重置 */
138     reset() {
139       this.formData = {
140         id: undefined,
141         name: undefined,
142         sex: undefined,
143         birthday: undefined,
144         description: undefined,
145       };
146       this.resetForm("formRef");
147     }
148   }
149 };
150 </script>