DemoStudentContactForm.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <el-form
  3. ref="formRef"
  4. :model="formData"
  5. :rules="formRules"
  6. label-width="0px"
  7. v-loading="formLoading"
  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="名字" prop="name" width="300">
  13. <template #default="row">
  14. <el-form-item 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="手机号码">
  20. <template #default="{ row, $index }">
  21. <el-form-item :prop="`${$index}.mobile`" :rules="formRules.mobile" class="mb-0px!">
  22. <el-input type="number" placeholder="输入手机号码" v-model="row.mobile" />
  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. const props = defineProps<{
  39. studentId: undefined // 学生编号
  40. }>()
  41. const formLoading = ref(false) // 表单的加载中
  42. const formData = ref([])
  43. const formRules = reactive({
  44. mobile: [required]
  45. })
  46. const formRef = ref() // 表单 Ref
  47. /** 监听主表的关联字段的变化,加载对应的子表数据 */
  48. watch(
  49. () => props.studentId,
  50. (val) => {
  51. if (val) {
  52. formData.value = [
  53. {
  54. name: '芋艿',
  55. mobile: '15601691300'
  56. }
  57. ]
  58. } else {
  59. formData.value = []
  60. }
  61. },
  62. { immediate: true }
  63. )
  64. /** 新增按钮操作 */
  65. const handleAdd = () => {
  66. formData.value.push({
  67. name: '土豆'
  68. })
  69. }
  70. /** 删除按钮操作 */
  71. const handleDelete = (index) => {
  72. formData.value.splice(index, 1)
  73. }
  74. /** 表单校验 */
  75. const validate = () => {
  76. return formRef.value.validate()
  77. }
  78. /** 表单值 **/
  79. const getData = () => {
  80. return formData.value
  81. }
  82. defineExpose({ validate, getData })
  83. </script>