index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <script setup lang="ts">
  2. import { onMounted, ref } from 'vue'
  3. import type { Category, CategorySearchFilter } from '@/model/category'
  4. import RegularPage from '@/components/RegularPage.vue'
  5. import { searchCategoryRequest, deleteCategoryRequest } from '@/api/category'
  6. import type { BaseTableColumns } from 'tdesign-vue-next'
  7. import { useSearchable } from '@/composables/useSearchable'
  8. import CategoryDialog from '@/pages/category/components/CategoryDialog.vue'
  9. import { MessagePlugin } from 'tdesign-vue-next'
  10. const { data, loading, pagination, onPageChange, fetchData } = useSearchable<
  11. CategorySearchFilter,
  12. Category
  13. >(searchCategoryRequest)
  14. const columns: BaseTableColumns = [
  15. {
  16. title: '分类名称',
  17. colKey: 'name',
  18. width: 100
  19. },
  20. {
  21. title: '分类编号',
  22. colKey: 'code',
  23. width: 100
  24. },
  25. {
  26. title: '排序',
  27. colKey: 'order',
  28. width: 100
  29. },
  30. {
  31. title: '操作',
  32. colKey: 'operation',
  33. width: 100
  34. }
  35. ]
  36. const categoryDialogVisible = ref(false)
  37. const isEdit = ref(false)
  38. const currentTableData = ref<Category | {}>({})
  39. onMounted(fetchData)
  40. const editClick = (category: Category) => {
  41. isEdit.value = true
  42. currentTableData.value = category
  43. categoryDialogVisible.value = true
  44. }
  45. const handleCategoryDialogSuccess = () => {
  46. fetchData()
  47. categoryDialogVisible.value = false
  48. }
  49. const handleCategoryDialogClosed = () => {
  50. isEdit.value = false
  51. currentTableData.value = {}
  52. }
  53. const deleteCategoryClick = async (id: string) => {
  54. await deleteCategoryRequest(id)
  55. fetchData()
  56. MessagePlugin.success('删除分类成功')
  57. }
  58. </script>
  59. <template>
  60. <RegularPage title="分类管理">
  61. <template #right-area>
  62. <TButton @click="categoryDialogVisible = true">创建分类</TButton>
  63. </template>
  64. <TTable
  65. class="w-full h-full"
  66. row-key="id"
  67. height="92%"
  68. table-layout="auto"
  69. :data="data"
  70. :loading="loading"
  71. :columns="columns"
  72. cell-empty-content="-"
  73. :paginationAffixedBottom="true"
  74. :pagination="{
  75. total: pagination.total,
  76. current: pagination.page,
  77. pageSize: pagination.size,
  78. onChange: onPageChange
  79. }"
  80. >
  81. <template #operation="{ row }">
  82. <TSpace :size="1">
  83. <TButton variant="text" size="small" theme="primary" @click="editClick(row)"
  84. >编辑</TButton
  85. >
  86. <t-popconfirm
  87. theme="default"
  88. content="确认删除此项分类吗"
  89. @confirm="deleteCategoryClick(row.id)"
  90. >
  91. <TButton variant="text" size="small" theme="danger">删除</TButton>
  92. </t-popconfirm>
  93. </TSpace>
  94. </template>
  95. </TTable>
  96. <CategoryDialog
  97. headerTitle="分类"
  98. :isEdit="isEdit"
  99. :category="currentTableData"
  100. v-model:visible="categoryDialogVisible"
  101. @closed="handleCategoryDialogClosed"
  102. @success="handleCategoryDialogSuccess"
  103. ></CategoryDialog>
  104. </RegularPage>
  105. </template>
  106. <style scoped>
  107. :deep(.t-card__body) {
  108. flex: 1;
  109. min-height: 0;
  110. }
  111. </style>