提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<el-form |
|
3 |
ref="formRef" |
|
4 |
:model="formData" |
|
5 |
:rules="formRules" |
|
6 |
v-loading="formLoading" |
|
7 |
label-width="0px" |
|
8 |
:inline-message="true" |
|
9 |
> |
|
10 |
<el-table :data="formData" class="-mt-10px"> |
|
11 |
<el-table-column label="序号" type="index" width="100" /> |
|
12 |
<el-table-column label="名字" min-width="150"> |
|
13 |
<template #default="{ row, $index }"> |
|
14 |
<el-form-item :prop="`${$index}.name`" :rules="formRules.name" class="mb-0px!"> |
|
15 |
<el-input v-model="row.name" placeholder="请输入名字" /> |
|
16 |
</el-form-item> |
|
17 |
</template> |
|
18 |
</el-table-column> |
|
19 |
<el-table-column label="分数" min-width="150"> |
|
20 |
<template #default="{ row, $index }"> |
|
21 |
<el-form-item :prop="`${$index}.score`" :rules="formRules.score" class="mb-0px!"> |
|
22 |
<el-input v-model="row.score" placeholder="请输入分数" /> |
|
23 |
</el-form-item> |
|
24 |
</template> |
|
25 |
</el-table-column> |
|
26 |
<el-table-column align="center" fixed="right" label="操作" width="60"> |
|
27 |
<template #default="{ $index }"> |
|
28 |
<el-button @click="handleDelete($index)" link>—</el-button> |
|
29 |
</template> |
|
30 |
</el-table-column> |
|
31 |
</el-table> |
|
32 |
</el-form> |
|
33 |
<el-row justify="center" class="mt-3"> |
|
34 |
<el-button @click="handleAdd" round>+ 添加学生课程</el-button> |
|
35 |
</el-row> |
|
36 |
</template> |
|
37 |
<script setup lang="ts"> |
|
38 |
import * as Demo03StudentApi from '@/api/infra/demo/demo03/normal' |
|
39 |
|
|
40 |
const props = defineProps<{ |
|
41 |
studentId: undefined // 学生编号(主表的关联字段) |
|
42 |
}>() |
|
43 |
const formLoading = ref(false) // 表单的加载中 |
|
44 |
const formData = ref([]) |
|
45 |
const formRules = reactive({ |
|
46 |
studentId: [{ required: true, message: '学生编号不能为空', trigger: 'blur' }], |
|
47 |
name: [{ required: true, message: '名字不能为空', trigger: 'blur' }], |
|
48 |
score: [{ required: true, message: '分数不能为空', trigger: 'blur' }] |
|
49 |
}) |
|
50 |
const formRef = ref() // 表单 Ref |
|
51 |
|
|
52 |
/** 监听主表的关联字段的变化,加载对应的子表数据 */ |
|
53 |
watch( |
|
54 |
() => props.studentId, |
|
55 |
async (val) => { |
|
56 |
// 1. 重置表单 |
|
57 |
formData.value = [] |
|
58 |
// 2. val 非空,则加载数据 |
|
59 |
if (!val) { |
|
60 |
return |
|
61 |
} |
|
62 |
try { |
|
63 |
formLoading.value = true |
|
64 |
formData.value = await Demo03StudentApi.getDemo03CourseListByStudentId(val) |
|
65 |
} finally { |
|
66 |
formLoading.value = false |
|
67 |
} |
|
68 |
}, |
|
69 |
{ immediate: true } |
|
70 |
) |
|
71 |
|
|
72 |
/** 新增按钮操作 */ |
|
73 |
const handleAdd = () => { |
|
74 |
const row = { |
|
75 |
id: undefined, |
|
76 |
studentId: undefined, |
|
77 |
name: undefined, |
|
78 |
score: undefined |
|
79 |
} |
|
80 |
row.studentId = props.studentId |
|
81 |
formData.value.push(row) |
|
82 |
} |
|
83 |
|
|
84 |
/** 删除按钮操作 */ |
|
85 |
const handleDelete = (index) => { |
|
86 |
formData.value.splice(index, 1) |
|
87 |
} |
|
88 |
|
|
89 |
/** 表单校验 */ |
|
90 |
const validate = () => { |
|
91 |
return formRef.value.validate() |
|
92 |
} |
|
93 |
|
|
94 |
/** 表单值 */ |
|
95 |
const getData = () => { |
|
96 |
return formData.value |
|
97 |
} |
|
98 |
|
|
99 |
defineExpose({ validate, getData }) |
|
100 |
</script> |