Demo02CategoryForm.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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="名字" prop="name">
  11. <el-input v-model="formData.name" placeholder="请输入名字" />
  12. </el-form-item>
  13. <el-form-item label="父级编号" prop="parentId">
  14. <el-tree-select
  15. v-model="formData.parentId"
  16. :data="demo02CategoryTree"
  17. :props="defaultProps"
  18. check-strictly
  19. default-expand-all
  20. placeholder="请选择父级编号"
  21. />
  22. </el-form-item>
  23. </el-form>
  24. <template #footer>
  25. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  26. <el-button @click="dialogVisible = false">取 消</el-button>
  27. </template>
  28. </Dialog>
  29. </template>
  30. <script setup lang="ts">
  31. import * as Demo02CategoryApi from '@/api/infra/demo/demo02'
  32. import { defaultProps, handleTree } from '@/utils/tree'
  33. const { t } = useI18n() // 国际化
  34. const message = useMessage() // 消息弹窗
  35. const dialogVisible = ref(false) // 弹窗的是否展示
  36. const dialogTitle = ref('') // 弹窗的标题
  37. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  38. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  39. const formData = ref({
  40. id: undefined,
  41. name: undefined,
  42. parentId: undefined
  43. })
  44. const formRules = reactive({
  45. name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
  46. parentId: [{ required: true, message: '父级编号不能为空', trigger: 'blur' }]
  47. })
  48. const formRef = ref() // 表单 Ref
  49. const demo02CategoryTree = ref() // 树形结构
  50. /** 打开弹窗 */
  51. const open = async (type: string, id?: number) => {
  52. dialogVisible.value = true
  53. dialogTitle.value = t('action.' + type)
  54. formType.value = type
  55. resetForm()
  56. // 修改时,设置数据
  57. if (id) {
  58. formLoading.value = true
  59. try {
  60. formData.value = await Demo02CategoryApi.getDemo02Category(id)
  61. } finally {
  62. formLoading.value = false
  63. }
  64. }
  65. await getDemo02CategoryTree()
  66. }
  67. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  68. /** 提交表单 */
  69. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  70. const submitForm = async () => {
  71. // 校验表单
  72. await formRef.value.validate()
  73. // 提交请求
  74. formLoading.value = true
  75. try {
  76. const data = formData.value as unknown as Demo02CategoryApi.Demo02CategoryVO
  77. if (formType.value === 'create') {
  78. await Demo02CategoryApi.createDemo02Category(data)
  79. message.success(t('common.createSuccess'))
  80. } else {
  81. await Demo02CategoryApi.updateDemo02Category(data)
  82. message.success(t('common.updateSuccess'))
  83. }
  84. dialogVisible.value = false
  85. // 发送操作成功的事件
  86. emit('success')
  87. } finally {
  88. formLoading.value = false
  89. }
  90. }
  91. /** 重置表单 */
  92. const resetForm = () => {
  93. formData.value = {
  94. id: undefined,
  95. name: undefined,
  96. parentId: undefined
  97. }
  98. formRef.value?.resetFields()
  99. }
  100. /** 获得示例分类树 */
  101. const getDemo02CategoryTree = async () => {
  102. demo02CategoryTree.value = []
  103. const data = await Demo02CategoryApi.getDemo02CategoryList()
  104. const root: Tree = { id: 0, name: '顶级示例分类', children: [] }
  105. root.children = handleTree(data, 'id', 'parentId')
  106. demo02CategoryTree.value.push(root)
  107. }
  108. </script>