dengzedong
2024-12-19 85b2001c0ec2f1adc598db3bf47ad457dcca7074
提交 | 用户 | 时间
e7c126 1 <template>
H 2   <div class="app-container">
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="description">
14                       <el-input v-model="formData.description" type="textarea" placeholder="请输入简介" />
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="性别" prop="sex">
20                       <el-select v-model="formData.sex" placeholder="请选择性别">
21                             <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
22                                        :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
23                       </el-select>
24                     </el-form-item>
25                     <el-form-item label="是否有效" prop="enabled">
26                       <el-radio-group v-model="formData.enabled">
27                             <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
28                                       :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
29                       </el-radio-group>
30                     </el-form-item>
31                     <el-form-item label="头像">
32                       <ImageUpload v-model="formData.avatar"/>
33                     </el-form-item>
34                     <el-form-item label="附件">
35                       <FileUpload v-model="formData.video"/>
36                     </el-form-item>
37                     <el-form-item label="备注">
38                       <Editor v-model="formData.memo" :min-height="192"/>
39                     </el-form-item>
40       </el-form>
41   </div>
42 </template>
43
44 <script>
45   import * as StudentApi from '@/api/infra/demo';
46       import ImageUpload from '@/components/ImageUpload';
47       import FileUpload from '@/components/FileUpload';
48       import Editor from '@/components/Editor';
49   export default {
50     name: "StudentTeacherForm",
51     components: {
52           ImageUpload,
53           FileUpload,
54           Editor,
55     },
56     props:[
57       'studentId'
58     ],// 学生编号(主表的关联字段)
59     data() {
60       return {
61         // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
62         formLoading: false,
63         // 表单参数
64         formData: [],
65         // 表单校验
66         formRules: {
67                         studentId: [{ required: true, message: "学生编号不能为空", trigger: "blur" }],
68                         name: [{ required: true, message: "名字不能为空", trigger: "blur" }],
69                         description: [{ required: true, message: "简介不能为空", trigger: "blur" }],
70                         birthday: [{ required: true, message: "出生日期不能为空", trigger: "blur" }],
71                         sex: [{ required: true, message: "性别不能为空", trigger: "change" }],
72                         enabled: [{ required: true, message: "是否有效不能为空", trigger: "blur" }],
73                         avatar: [{ required: true, message: "头像不能为空", trigger: "blur" }],
74                         memo: [{ required: true, message: "备注不能为空", trigger: "blur" }],
75         },
76       };
77     },
78     watch:{/** 监听主表的关联字段的变化,加载对应的子表数据 */
79       studentId:{
80         handler(val) {
81           // 1. 重置表单
82               this.formData = {
83                                   id: undefined,
84                                   studentId: undefined,
85                                   name: undefined,
86                                   description: undefined,
87                                   birthday: undefined,
88                                   sex: undefined,
89                                   enabled: undefined,
90                                   avatar: undefined,
91                                   video: undefined,
92                                   memo: undefined,
93               }
94           // 2. val 非空,则加载数据
95           if (!val) {
96             return;
97           }
98           try {
99             this.formLoading = true;
100             // 这里还是需要获取一下 this 的不然取不到 formData
101             const that = this;
102             StudentApi.getStudentTeacherByStudentId(val).then(function (res){
103               const data = res.data;
104               if (!data) {
105                 return
106               }
107               that.formData = data;
108             })
109           } finally {
110             this.formLoading = false;
111           }
112         },
113         immediate: true
114       }
115     },
116     methods: {
117       /** 表单校验 */
118       validate(){
119         return this.$refs["formRef"].validate();
120       },
121       /** 表单值 */
122       getData(){
123         return this.formData;
124       }
125     }
126   };
127 </script>