提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<el-form |
|
3 |
ref="formRef" |
|
4 |
:model="formData" |
|
5 |
:rules="formRules" |
|
6 |
label-width="100px" |
|
7 |
v-loading="formLoading" |
|
8 |
> |
|
9 |
<el-form-item label="名字" prop="name"> |
|
10 |
<el-input v-model="formData.name" placeholder="请输入名字" /> |
|
11 |
</el-form-item> |
|
12 |
<el-form-item label="班主任" prop="teacher"> |
|
13 |
<el-input v-model="formData.teacher" placeholder="请输入班主任" /> |
|
14 |
</el-form-item> |
|
15 |
</el-form> |
|
16 |
</template> |
|
17 |
<script setup lang="ts"> |
|
18 |
import * as Demo03StudentApi from '@/api/infra/demo/demo03/normal' |
|
19 |
|
|
20 |
const props = defineProps<{ |
|
21 |
studentId: undefined // 学生编号(主表的关联字段) |
|
22 |
}>() |
|
23 |
const formLoading = ref(false) // 表单的加载中 |
|
24 |
const formData = ref([]) |
|
25 |
const formRules = reactive({ |
|
26 |
studentId: [{ required: true, message: '学生编号不能为空', trigger: 'blur' }], |
|
27 |
name: [{ required: true, message: '名字不能为空', trigger: 'blur' }], |
|
28 |
teacher: [{ required: true, message: '班主任不能为空', trigger: 'blur' }] |
|
29 |
}) |
|
30 |
const formRef = ref() // 表单 Ref |
|
31 |
|
|
32 |
/** 监听主表的关联字段的变化,加载对应的子表数据 */ |
|
33 |
watch( |
|
34 |
() => props.studentId, |
|
35 |
async (val) => { |
|
36 |
// 1. 重置表单 |
|
37 |
formData.value = { |
|
38 |
id: undefined, |
|
39 |
studentId: undefined, |
|
40 |
name: undefined, |
|
41 |
teacher: undefined |
|
42 |
} |
|
43 |
// 2. val 非空,则加载数据 |
|
44 |
if (!val) { |
|
45 |
return |
|
46 |
} |
|
47 |
try { |
|
48 |
formLoading.value = true |
|
49 |
const data = await Demo03StudentApi.getDemo03GradeByStudentId(val) |
|
50 |
if (!data) { |
|
51 |
return |
|
52 |
} |
|
53 |
formData.value = data |
|
54 |
} finally { |
|
55 |
formLoading.value = false |
|
56 |
} |
|
57 |
}, |
|
58 |
{ immediate: true } |
|
59 |
) |
|
60 |
|
|
61 |
/** 表单校验 */ |
|
62 |
const validate = () => { |
|
63 |
return formRef.value.validate() |
|
64 |
} |
|
65 |
|
|
66 |
/** 表单值 */ |
|
67 |
const getData = () => { |
|
68 |
return formData.value |
|
69 |
} |
|
70 |
|
|
71 |
defineExpose({ validate, getData }) |
|
72 |
</script> |