edit.stub 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div class="edit-popup">
  3. <popup
  4. ref="popupRef"
  5. :title="popupTitle"
  6. :async="true"
  7. width="550px"
  8. @confirm="handleSubmit"
  9. @close="handleClose"
  10. >
  11. <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
  12. {FORM_VIEW}
  13. </el-form>
  14. </popup>
  15. </div>
  16. </template>
  17. <script lang="ts" setup name="{SETUP_NAME}Edit">
  18. import type { FormInstance } from 'element-plus'
  19. import Popup from '@/components/popup/index.vue'
  20. import {{IMPORT_LISTS} api{UPPER_CAMEL_NAME}Add, api{UPPER_CAMEL_NAME}Edit, api{UPPER_CAMEL_NAME}Detail } from '@/api/{API_DIR}'
  21. import { timeFormat } from '@/utils/util'
  22. import type { PropType } from 'vue'
  23. defineProps({
  24. dictData: {
  25. type: Object as PropType<Record<string, any[]>>,
  26. default: () => ({})
  27. }
  28. })
  29. const emit = defineEmits(['success', 'close'])
  30. const formRef = shallowRef<FormInstance>()
  31. const popupRef = shallowRef<InstanceType<typeof Popup>>()
  32. const mode = ref('add')
  33. {TREE_CONST}
  34. // 弹窗标题
  35. const popupTitle = computed(() => {
  36. return mode.value == 'edit' ? '编辑{TABLE_COMMENT}' : '新增{TABLE_COMMENT}'
  37. })
  38. // 表单数据
  39. const formData = reactive({
  40. {PK}: '',
  41. {FORM_DATA}
  42. })
  43. // 表单验证
  44. const formRules = reactive<any>({
  45. {FORM_VALIDATE}
  46. })
  47. // 获取详情
  48. const setFormData = async (data: Record<any, any>) => {
  49. for (const key in formData) {
  50. if (data[key] != null && data[key] != undefined) {
  51. //@ts-ignore
  52. formData[key] = data[key]
  53. }
  54. }
  55. {CHECKBOX_SPLIT}
  56. {FORM_DATE}
  57. }
  58. const getDetail = async (row: Record<string, any>) => {
  59. const data = await api{UPPER_CAMEL_NAME}Detail({
  60. {PK}: row.{PK}
  61. })
  62. setFormData(data)
  63. }
  64. // 提交按钮
  65. const handleSubmit = async () => {
  66. await formRef.value?.validate()
  67. const data = { ...formData, {CHECKBOX_JOIN} }
  68. mode.value == 'edit'
  69. ? await api{UPPER_CAMEL_NAME}Edit(data)
  70. : await api{UPPER_CAMEL_NAME}Add(data)
  71. popupRef.value?.close()
  72. emit('success')
  73. }
  74. //打开弹窗
  75. const open = (type = 'add') => {
  76. mode.value = type
  77. popupRef.value?.open()
  78. }
  79. // 关闭回调
  80. const handleClose = () => {
  81. emit('close')
  82. }
  83. {GET_TREE_LISTS}
  84. defineExpose({
  85. open,
  86. setFormData,
  87. getDetail
  88. })
  89. </script>