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