潘志宝
2024-09-09 ed81b7371e376df35448b81531d30dd9024bd44a
提交 | 用户 | 时间
e7c126 1 <template>
H 2   <div class="app-container">
3       <el-form
4         ref="formRef"
5         :model="formData"
6         :rules="formRules"
7         v-loading="formLoading"
8         label-width="0px"
9         :inline-message="true"
10       >
11         <el-table :data="formData" class="-mt-10px">
12           <el-table-column label="序号" type="index" width="100" />
13                        <el-table-column label="名字" min-width="150">
14                         <template v-slot="{ row, $index }">
15                           <el-form-item :prop="`${$index}.name`" :rules="formRules.name" class="mb-0px!">
16                             <el-input v-model="row.name" placeholder="请输入名字" />
17                           </el-form-item>
18                         </template>
19                       </el-table-column>
20                       <el-table-column label="简介" min-width="200">
21                         <template v-slot="{ row, $index }">
22                           <el-form-item :prop="`${$index}.description`" :rules="formRules.description" class="mb-0px!">
23                             <el-input v-model="row.description" type="textarea" placeholder="请输入简介" />
24                           </el-form-item>
25                         </template>
26                       </el-table-column>
27                       <el-table-column label="出生日期" min-width="150">
28                         <template v-slot="{ row, $index }">
29                           <el-form-item :prop="`${$index}.birthday`" :rules="formRules.birthday" class="mb-0px!">
30                             <el-date-picker clearable v-model="row.birthday" type="date" value-format="timestamp" placeholder="选择出生日期" />
31                           </el-form-item>
32                         </template>
33                       </el-table-column>
34                       <el-table-column label="性别" min-width="150">
35                         <template v-slot="{ row, $index }">
36                           <el-form-item :prop="`${$index}.sex`" :rules="formRules.sex" class="mb-0px!">
37                             <el-select v-model="row.sex" placeholder="请选择性别">
38                                   <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
39                                              :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
40                             </el-select>
41                           </el-form-item>
42                         </template>
43                       </el-table-column>
44                       <el-table-column label="是否有效" min-width="150">
45                         <template v-slot="{ row, $index }">
46                           <el-form-item :prop="`${$index}.enabled`" :rules="formRules.enabled" class="mb-0px!">
47                             <el-radio-group v-model="row.enabled">
48                                   <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
49                                             :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
50                             </el-radio-group>
51                           </el-form-item>
52                         </template>
53                       </el-table-column>
54                       <el-table-column label="头像" min-width="200">
55                         <template v-slot="{ row, $index }">
56                           <el-form-item :prop="`${$index}.avatar`" :rules="formRules.avatar" class="mb-0px!">
57                             <ImageUpload v-model="row.avatar"/>
58                           </el-form-item>
59                         </template>
60                       </el-table-column>
61                       <el-table-column label="附件" min-width="200">
62                         <template v-slot="{ row, $index }">
63                           <el-form-item :prop="`${$index}.video`" :rules="formRules.video" class="mb-0px!">
64                             <FileUpload v-model="row.video"/>
65                           </el-form-item>
66                         </template>
67                       </el-table-column>
68                       <el-table-column label="备注" min-width="400">
69                         <template v-slot="{ row, $index }">
70                           <el-form-item :prop="`${$index}.memo`" :rules="formRules.memo" class="mb-0px!">
71                             <Editor v-model="row.memo" :min-height="192"/>
72                           </el-form-item>
73                         </template>
74                       </el-table-column>
75           <el-table-column align="center" fixed="right" label="操作" width="60">
76             <template v-slot="{ $index }">
77               <el-link @click="handleDelete($index)">—</el-link>
78             </template>
79           </el-table-column>
80         </el-table>
81       </el-form>
82       <el-row justify="center" class="mt-3">
83         <el-button @click="handleAdd" round>+ 添加学生联系人</el-button>
84       </el-row>
85   </div>
86 </template>
87
88 <script>
89   import * as StudentApi from '@/api/infra/demo';
90       import ImageUpload from '@/components/ImageUpload';
91       import FileUpload from '@/components/FileUpload';
92       import Editor from '@/components/Editor';
93   export default {
94     name: "StudentContactForm",
95     components: {
96           ImageUpload,
97           FileUpload,
98           Editor,
99     },
100     props:[
101       'studentId'
102     ],// 学生编号(主表的关联字段)
103     data() {
104       return {
105         // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
106         formLoading: false,
107         // 表单参数
108         formData: [],
109         // 表单校验
110         formRules: {
111                         studentId: [{ required: true, message: "学生编号不能为空", trigger: "blur" }],
112                         name: [{ required: true, message: "名字不能为空", trigger: "blur" }],
113                         description: [{ required: true, message: "简介不能为空", trigger: "blur" }],
114                         birthday: [{ required: true, message: "出生日期不能为空", trigger: "blur" }],
115                         sex: [{ required: true, message: "性别不能为空", trigger: "change" }],
116                         enabled: [{ required: true, message: "是否有效不能为空", trigger: "blur" }],
117                         avatar: [{ required: true, message: "头像不能为空", trigger: "blur" }],
118                         memo: [{ required: true, message: "备注不能为空", trigger: "blur" }],
119         },
120       };
121     },
122     watch:{/** 监听主表的关联字段的变化,加载对应的子表数据 */
123       studentId:{
124         handler(val) {
125           // 1. 重置表单
126               this.formData = []
127           // 2. val 非空,则加载数据
128           if (!val) {
129             return;
130           }
131           try {
132             this.formLoading = true;
133             // 这里还是需要获取一下 this 的不然取不到 formData
134             const that = this;
135             StudentApi.getStudentContactListByStudentId(val).then(function (res){
136               that.formData = res.data;
137             })
138           } finally {
139             this.formLoading = false;
140           }
141         },
142         immediate: true
143       }
144     },
145     methods: {
146           /** 新增按钮操作 */
147           handleAdd() {
148             const row = {
149                                 id: undefined,
150                                 studentId: undefined,
151                                 name: undefined,
152                                 description: undefined,
153                                 birthday: undefined,
154                                 sex: undefined,
155                                 enabled: undefined,
156                                 avatar: undefined,
157                                 video: undefined,
158                                 memo: undefined,
159             }
160             row.studentId = this.studentId;
161             this.formData.push(row);
162           },
163           /** 删除按钮操作 */
164           handleDelete(index) {
165             this.formData.splice(index, 1);
166           },
167       /** 表单校验 */
168       validate(){
169         return this.$refs["formRef"].validate();
170       },
171       /** 表单值 */
172       getData(){
173         return this.formData;
174       }
175     }
176   };
177 </script>