DataTable.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <script lang="ts" setup>
  2. /**
  3. * @Name: Table.vue
  4. * @Description: 商品列表表格
  5. * @Author: Cheney
  6. */
  7. import { Delete, Download, Operation, Plus, Refresh, Upload } from '@element-plus/icons-vue';
  8. import * as api from '../api';
  9. import PermissionButton from '/src/components/PermissionButton/index.vue';
  10. import EditDrawer from './EditDrawer.vue';
  11. import ImportButton from '/src/components/ImportButton/index.vue';
  12. import VerticalDivider from '/src/components/VerticalDivider/index.vue';
  13. import DataTableSlot from './DataTableSlot.vue';
  14. import { productColumns } from '../ColumnsTsx';
  15. const { tableOptions, handlePageChange } = usePagination(fetchList);
  16. const gridRef = ref();
  17. const gridOptions: any = reactive({
  18. border: false,
  19. round: true,
  20. stripe: true,
  21. currentRowHighLight: true,
  22. height: '100%',
  23. toolbarConfig: {
  24. custom: true,
  25. slots: {
  26. buttons: 'toolbar_buttons',
  27. tools: 'toolbar_tools'
  28. }
  29. },
  30. rowConfig: {
  31. isHover: true
  32. },
  33. columnConfig: {
  34. resizable: true
  35. },
  36. pagerConfig: {
  37. total: tableOptions.value.total,
  38. page: tableOptions.value.page,
  39. limit: tableOptions.value.limit
  40. },
  41. loading: false,
  42. loadingConfig: {
  43. icon: 'vxe-icon-indicator roll',
  44. text: '正在拼命加载中...'
  45. },
  46. columns: productColumns,
  47. data: ''
  48. });
  49. const checkedList = ref<Set<number>>(new Set());
  50. const editOpen = ref(false);
  51. const rowData = ref({});
  52. const dialogVisible = ref(false);
  53. const templateType = ref();
  54. onBeforeMount(() => {
  55. fetchList();
  56. });
  57. async function fetchList() {
  58. const query = {
  59. page: gridOptions.pagerConfig.page,
  60. limit: gridOptions.pagerConfig.limit
  61. };
  62. await useTableData(api.getTableData, query, gridOptions);
  63. }
  64. function handleRefresh() {
  65. fetchList();
  66. }
  67. async function batchOpen() {
  68. const ids = Array.from(checkedList.value);
  69. await useResponse(api.updateShopDetail, { ids, status: 1 });
  70. checkedList.value.clear();
  71. await fetchList();
  72. }
  73. function selectChangeEvent({ checked, row }: any) {
  74. if (checked) {
  75. checkedList.value.add(row.id); // 获取单个数据
  76. } else {
  77. checkedList.value.delete(row.id);
  78. }
  79. }
  80. function selectAllChangeEvent({ checked }: any) {
  81. const $grid = gridRef.value;
  82. if ($grid) {
  83. const records = $grid.getData(); // 获取所有数据
  84. if (checked) {
  85. records.forEach((item: any) => {
  86. checkedList.value.add(item.id);
  87. });
  88. } else {
  89. checkedList.value.clear();
  90. }
  91. }
  92. }
  93. function handleEdit(row: any) {
  94. editOpen.value = true;
  95. rowData.value = row;
  96. console.log('(DataTable.vue: 111)=> rowData.value', rowData.value);
  97. }
  98. function singleDelete(row: any) {
  99. // dialogVisible.value = true;
  100. rowData.value = row;
  101. console.log('(DataTable.vue: 116)=> rowData.value', rowData.value);
  102. }
  103. function downloadTemplate() {
  104. // console.log('111=> ');
  105. }
  106. </script>
  107. <template>
  108. <vxe-grid ref="gridRef" v-bind="gridOptions"
  109. @checkbox-change="selectChangeEvent"
  110. @checkbox-all="selectAllChangeEvent">
  111. <template #toolbar_buttons>
  112. <div class="flex gap-2">
  113. <PermissionButton :disabled="!checkedList.size" :icon="Delete" plain round type="danger" @click="batchOpen">
  114. 批量删除
  115. </PermissionButton>
  116. <PermissionButton :icon="Plus" plain round type="primary" @click="batchOpen">
  117. 新 增
  118. </PermissionButton>
  119. <div class="custom-el-input">
  120. <el-select v-model="templateType" placeholder="Select" style="width: 190px">
  121. <template #prefix>
  122. <div class="flex items-center">
  123. <el-button bg size="small" style="margin-left: -7px; font-size: 14px; border-radius: 29px;" text
  124. type="success"
  125. @click.stop="downloadTemplate">下载
  126. </el-button>
  127. <VerticalDivider style="margin-left: 7px" />
  128. </div>
  129. </template>
  130. <el-option label="商品通知模板" value="item1" />
  131. <el-option label="商品模板" value="item2" />
  132. <el-option label="指导价格模板" value="item3" />
  133. </el-select>
  134. </div>
  135. <VerticalDivider class="px-1" style="margin-left: 7px;" />
  136. <ImportButton :icon="Upload" bg text>导 入</ImportButton>
  137. </div>
  138. </template>
  139. <template #toolbar_tools>
  140. <el-button circle class="toolbar-btn" @click="handleRefresh">
  141. <el-icon>
  142. <Refresh />
  143. </el-icon>
  144. </el-button>
  145. <el-button circle class="mr-3 toolbar-btn">
  146. <el-icon>
  147. <Download />
  148. </el-icon>
  149. </el-button>
  150. </template>
  151. <template #top>
  152. <div class="mb-2"></div>
  153. </template>
  154. <template #pager>
  155. <vxe-pager
  156. v-model:currentPage="gridOptions.pagerConfig.page"
  157. v-model:pageSize="gridOptions.pagerConfig.limit"
  158. :total="gridOptions.pagerConfig.total"
  159. class="mt-1.5"
  160. @page-change="handlePageChange"
  161. />
  162. </template>
  163. <!-- 自定义列插槽 -->
  164. <template v-for="col in productColumns" :key="col.field" #[`${col.field}`]="{ row }">
  165. <DataTableSlot :field="col.field" :row="row" @edit-row="handleEdit" @single-delete="singleDelete" />
  166. </template>
  167. </vxe-grid>
  168. <EditDrawer v-if="editOpen" v-model="editOpen" :row-data="rowData" />
  169. </template>
  170. <style scoped>
  171. .toolbar-btn {
  172. width: 34px;
  173. height: 34px;
  174. font-size: 18px
  175. }
  176. :deep(.custom-el-input .el-select__wrapper) {
  177. border-radius: 20px;
  178. }
  179. </style>