index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <script lang="ts" setup>
  2. import type { PropType } from 'vue';
  3. import type { OnActionClickParams, VxeTableGridOptions } from '#/adapter';
  4. import { watch } from 'vue';
  5. import { alert, useVbenModal } from '@vben/common-ui';
  6. import { Button, message } from 'ant-design-vue';
  7. import { useTableGridOptions, useVbenVxeGrid } from '#/adapter';
  8. import { DictionaryApi } from '#/api';
  9. import { SystemItemDelete } from '#/service/apis/System/DictionaryController';
  10. import { useColumns, useSearchSchema } from './data.config';
  11. import FormEdit from './edit.vue';
  12. defineOptions({
  13. name: 'DictionaryItem',
  14. });
  15. const props = defineProps({
  16. type: {
  17. type: String as PropType<string>,
  18. default: '',
  19. },
  20. });
  21. const [FormEditModal, formEditApi] = useVbenModal({
  22. connectedComponent: FormEdit,
  23. });
  24. const handelSuccess = () => {
  25. reload();
  26. };
  27. const handleDelete = async (id: number) => {
  28. await SystemItemDelete({ id });
  29. message.success('数据删除成功');
  30. reload();
  31. };
  32. const handleEdit = (record: any, isUpdate: boolean) => {
  33. if (!isUpdate && !props.type) {
  34. alert({ content: '请先选择字典类型', icon: 'warning' });
  35. return;
  36. }
  37. formEditApi
  38. .setData({
  39. isUpdate,
  40. baseData: { ...record, dictionaryCode: props.type },
  41. })
  42. .open();
  43. };
  44. const handleActionClick = async ({
  45. code,
  46. row,
  47. }: OnActionClickParams<DictionaryApi.RecordItem>) => {
  48. switch (code) {
  49. case 'append': {
  50. handleEdit({ parentId: row.id }, false);
  51. break;
  52. }
  53. case 'delete': {
  54. await handleDelete(row.id);
  55. break;
  56. }
  57. case 'edit': {
  58. handleEdit({ id: row.id }, true);
  59. break;
  60. }
  61. }
  62. };
  63. const [Grid, { reload }] = useVbenVxeGrid(
  64. useTableGridOptions({
  65. formOptions: {
  66. schema: useSearchSchema(),
  67. },
  68. gridOptions: {
  69. pagerConfig: {
  70. enabled: false,
  71. },
  72. treeConfig: {
  73. rowField: 'id',
  74. childrenField: 'children',
  75. },
  76. columns: useColumns(handleActionClick),
  77. proxyConfig: {
  78. autoLoad: false,
  79. ajax: {
  80. query: async (_: any, formValues: any) => {
  81. const postData = { ...formValues };
  82. postData.type = props.type;
  83. return await DictionaryApi.getItemTree({
  84. ...postData,
  85. });
  86. },
  87. },
  88. },
  89. } as VxeTableGridOptions,
  90. }),
  91. );
  92. watch(
  93. () => props.type,
  94. () => {
  95. reload();
  96. },
  97. { deep: true },
  98. );
  99. </script>
  100. <template>
  101. <div class="h-full">
  102. <FormEditModal @success="handelSuccess" />
  103. <Grid>
  104. <template #table-title>
  105. <span class="border-l-primary border-l-8 border-solid pl-2">
  106. 字典项列表
  107. </span>
  108. </template>
  109. <template #toolbar-tools>
  110. <Button
  111. class="mr-2"
  112. type="primary"
  113. v-access:code="'dict-detail:add'"
  114. @click="() => handleEdit({}, false)"
  115. >
  116. 新增字典项
  117. </Button>
  118. </template>
  119. </Grid>
  120. </div>
  121. </template>