DemoStudentForm.vue 4.0 KB

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