提交 | 用户 | 时间
|
759b1c
|
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="150"> |
|
21 |
<template v-slot="{ row, $index }"> |
|
22 |
<el-form-item :prop="`${$index}.score`" :rules="formRules.score" class="mb-0px!"> |
|
23 |
<el-input v-model="row.score" placeholder="请输入分数" /> |
|
24 |
</el-form-item> |
|
25 |
</template> |
|
26 |
</el-table-column> |
|
27 |
<el-table-column align="center" fixed="right" label="操作" width="60"> |
|
28 |
<template v-slot="{ $index }"> |
|
29 |
<el-link @click="handleDelete($index)">—</el-link> |
|
30 |
</template> |
|
31 |
</el-table-column> |
|
32 |
</el-table> |
|
33 |
</el-form> |
|
34 |
<el-row justify="center" class="mt-3"> |
|
35 |
<el-button @click="handleAdd" round>+ 添加学生课程</el-button> |
|
36 |
</el-row> |
|
37 |
</div> |
|
38 |
</template> |
|
39 |
|
|
40 |
<script> |
|
41 |
import * as Demo03StudentApi from '@/api/infra/demo03-inner'; |
|
42 |
export default { |
|
43 |
name: "Demo03CourseForm", |
|
44 |
components: { |
|
45 |
}, |
|
46 |
props:[ |
|
47 |
'studentId' |
|
48 |
],// 学生编号(主表的关联字段) |
|
49 |
data() { |
|
50 |
return { |
|
51 |
// 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
52 |
formLoading: false, |
|
53 |
// 表单参数 |
|
54 |
formData: [], |
|
55 |
// 表单校验 |
|
56 |
formRules: { |
|
57 |
studentId: [{ required: true, message: "学生编号不能为空", trigger: "blur" }], |
|
58 |
name: [{ required: true, message: "名字不能为空", trigger: "blur" }], |
|
59 |
score: [{ required: true, message: "分数不能为空", trigger: "blur" }], |
|
60 |
}, |
|
61 |
}; |
|
62 |
}, |
|
63 |
watch:{/** 监听主表的关联字段的变化,加载对应的子表数据 */ |
|
64 |
studentId:{ |
|
65 |
handler(val) { |
|
66 |
// 1. 重置表单 |
|
67 |
this.formData = [] |
|
68 |
// 2. val 非空,则加载数据 |
|
69 |
if (!val) { |
|
70 |
return; |
|
71 |
} |
|
72 |
try { |
|
73 |
this.formLoading = true; |
|
74 |
// 这里还是需要获取一下 this 的不然取不到 formData |
|
75 |
const that = this; |
|
76 |
Demo03StudentApi.getDemo03CourseListByStudentId(val).then(function (res){ |
|
77 |
that.formData = res.data; |
|
78 |
}) |
|
79 |
} finally { |
|
80 |
this.formLoading = false; |
|
81 |
} |
|
82 |
}, |
|
83 |
immediate: true |
|
84 |
} |
|
85 |
}, |
|
86 |
methods: { |
|
87 |
/** 新增按钮操作 */ |
|
88 |
handleAdd() { |
|
89 |
const row = { |
|
90 |
id: undefined, |
|
91 |
studentId: undefined, |
|
92 |
name: undefined, |
|
93 |
score: undefined, |
|
94 |
} |
|
95 |
row.studentId = this.studentId; |
|
96 |
this.formData.push(row); |
|
97 |
}, |
|
98 |
/** 删除按钮操作 */ |
|
99 |
handleDelete(index) { |
|
100 |
this.formData.splice(index, 1); |
|
101 |
}, |
|
102 |
/** 表单校验 */ |
|
103 |
validate(){ |
|
104 |
return this.$refs["formRef"].validate(); |
|
105 |
}, |
|
106 |
/** 表单值 */ |
|
107 |
getData(){ |
|
108 |
return this.formData; |
|
109 |
} |
|
110 |
} |
|
111 |
}; |
|
112 |
</script> |