DemoStudentForm.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  9. />
  10. <!-- 子表的表单 -->
  11. <el-tabs v-model="subTabsName">
  12. <el-tab-pane label="学生联系人" name="demoStudentContact">
  13. <DemoStudentContactForm ref="demoStudentContactFormRef" :student-id="formData.id" />
  14. </el-tab-pane>
  15. <el-tab-pane label="学生地址" name="demoStudentAddress">
  16. <DemoStudentAddressForm ref="demoStudentAddressFormRef" :student-id="formData.id" />
  17. </el-tab-pane>
  18. </el-tabs>
  19. <template #footer>
  20. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  21. <el-button @click="dialogVisible = false">取 消</el-button>
  22. </template>
  23. </Dialog>
  24. </template>
  25. <script setup lang="ts">
  26. import * as DemoStudentApi from '@/api/infra/demo02'
  27. import DemoStudentContactForm from './DemoStudentContactForm.vue'
  28. import DemoStudentAddressForm from './DemoStudentAddressForm.vue'
  29. const { t } = useI18n() // 国际化
  30. const message = useMessage() // 消息弹窗
  31. const dialogVisible = ref(false) // 弹窗的是否展示
  32. const dialogTitle = ref('') // 弹窗的标题
  33. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  34. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  35. const formData = ref({
  36. id: undefined
  37. })
  38. const formRules = reactive({})
  39. const formRef = ref() // 表单 Ref
  40. /** 子表的表单 */
  41. const subTabsName = ref('demoStudentContact')
  42. const demoStudentContactFormRef = ref()
  43. const demoStudentAddressFormRef = ref()
  44. /** 打开弹窗 */
  45. const open = async (type: string, id?: number) => {
  46. dialogVisible.value = true
  47. dialogTitle.value = t('action.' + type)
  48. formType.value = type
  49. resetForm()
  50. // 修改时,设置数据
  51. if (id) {
  52. formLoading.value = true
  53. try {
  54. formData.value = await DemoStudentApi.getDemoStudent(id)
  55. } finally {
  56. formLoading.value = false
  57. }
  58. }
  59. }
  60. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  61. /** 提交表单 */
  62. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  63. const submitForm = async () => {
  64. // 校验表单
  65. await formRef.value.validate()
  66. // 校验子表单
  67. try {
  68. await demoStudentContactFormRef.value.validate()
  69. } catch (e) {
  70. subTabsName.value = 'demoStudentContact'
  71. return
  72. }
  73. try {
  74. await demoStudentAddressFormRef.value.validate()
  75. } catch (e) {
  76. subTabsName.value = 'demoStudentAddress'
  77. return
  78. }
  79. // 提交请求
  80. formLoading.value = true
  81. try {
  82. const data = formData.value as unknown as DemoStudentApi.DemoStudentVO
  83. // 拼接子表的数据
  84. data.demoStudentContacts = demoStudentContactFormRef.value.getData()
  85. data.demoStudentAddress = demoStudentAddressFormRef.value.getData()
  86. if (formType.value === 'create') {
  87. await DemoStudentApi.createDemoStudent(data)
  88. message.success(t('common.createSuccess'))
  89. } else {
  90. await DemoStudentApi.updateDemoStudent(data)
  91. message.success(t('common.updateSuccess'))
  92. }
  93. dialogVisible.value = false
  94. // 发送操作成功的事件
  95. emit('success')
  96. } finally {
  97. formLoading.value = false
  98. }
  99. }
  100. /** 重置表单 */
  101. const resetForm = () => {
  102. formData.value = {
  103. id: undefined
  104. }
  105. formRef.value?.resetFields()
  106. }
  107. </script>