DataTable.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <script lang="ts" setup>
  2. /**
  3. * @Name: Table.vue
  4. * @Description: 市场店铺表格
  5. * @Author: Cheney
  6. */
  7. import { Download, InfoFilled, Refresh } from '@element-plus/icons-vue';
  8. import { usePagination } from '/@/utils/usePagination';
  9. import { useTableData } from '/@/utils/useTableData';
  10. import { OnlineMerchandiseColumns } from '/@/views/store-manage/Columns';
  11. import DataTableSlot from './DataTableSlot.vue';
  12. import * as api from '../api';
  13. import { ElMessage } from 'element-plus';
  14. interface Parameter {
  15. country: string,
  16. shop: string,
  17. region: string,
  18. delivery: string,
  19. status: string,
  20. asin: string,
  21. sku: string
  22. }
  23. const queryParameter: Parameter | undefined = inject('query-parameter');
  24. const { tableOptions, handlePageChange } = usePagination(fetchList);
  25. const gridRef = ref();
  26. const gridOptions: any = reactive({
  27. id: 'online-merchandise-table',
  28. keepSource: true,
  29. size: 'small',
  30. border: false,
  31. round: true,
  32. stripe: true,
  33. currentRowHighLight: true,
  34. height: '100%',
  35. customConfig: {
  36. storage: {
  37. visible: true
  38. }
  39. },
  40. toolbarConfig: {
  41. size: 'large',
  42. custom: true,
  43. slots: {
  44. tools: 'toolbar_tools'
  45. }
  46. },
  47. rowConfig: {
  48. isHover: true
  49. },
  50. columnConfig: {
  51. resizable: true
  52. },
  53. pagerConfig: {
  54. total: tableOptions.value.total,
  55. page: tableOptions.value.page,
  56. limit: tableOptions.value.limit
  57. },
  58. loading: false,
  59. loadingConfig: {
  60. icon: 'vxe-icon-indicator roll',
  61. text: '正在拼命加载中...'
  62. },
  63. columns: '',
  64. data: ''
  65. });
  66. onMounted(() => {
  67. fetchList();
  68. });
  69. async function fetchList() {
  70. gridOptions.data = [];
  71. gridOptions.columns = [];
  72. const query = {
  73. asin: queryParameter?.asin,
  74. sku: queryParameter?.sku,
  75. country_code: queryParameter?.country,
  76. shop_id: queryParameter?.shop,
  77. region: queryParameter?.region,
  78. delivery: queryParameter?.delivery,
  79. status: queryParameter?.status
  80. };
  81. await useTableData(api.getTableData, query, gridOptions);
  82. await gridRef.value.loadColumn(OnlineMerchandiseColumns);
  83. gridOptions.showHeader = Boolean(gridOptions.data?.length);
  84. }
  85. function handleRefresh() {
  86. fetchList();
  87. }
  88. async function handleDownload() {
  89. gridOptions.loading = true;
  90. try {
  91. const query = {
  92. asin: queryParameter?.asin,
  93. sku__startswith: queryParameter?.sku,
  94. country_code: queryParameter?.country,
  95. shop_id: queryParameter?.shop,
  96. shop__region: queryParameter?.region,
  97. fulfillment_channel: queryParameter?.delivery,
  98. status: queryParameter?.status
  99. };
  100. const response = await api.exportData(query);
  101. const url = window.URL.createObjectURL(new Blob([ response.data ]));
  102. const link = document.createElement('a');
  103. link.href = url;
  104. link.setAttribute('download', '在线商品数据.xlsx');
  105. document.body.appendChild(link);
  106. link.click();
  107. ElMessage.success('数据导出成功!');
  108. } catch (error) {
  109. ElMessage.error('数据导出失败,请重试!');
  110. console.error(error);
  111. } finally {
  112. gridOptions.loading = false;
  113. }
  114. }
  115. defineExpose({ fetchList });
  116. </script>
  117. <template>
  118. <vxe-grid ref="gridRef" v-bind="gridOptions">
  119. <!-- 工具栏右侧 -->
  120. <template #toolbar_tools>
  121. <el-button circle class="toolbar-btn" @click="handleRefresh">
  122. <el-icon>
  123. <Refresh />
  124. </el-icon>
  125. </el-button>
  126. <el-popconfirm
  127. :icon="InfoFilled"
  128. icon-color="#626AEF"
  129. title="是否确认导出当前时间内所有数据项?"
  130. width="220"
  131. @confirm="handleDownload"
  132. >
  133. <template #reference>
  134. <el-button circle class="mr-3 toolbar-btn">
  135. <el-icon>
  136. <Download />
  137. </el-icon>
  138. </el-button>
  139. </template>
  140. <template #actions="{ confirm, cancel }">
  141. <el-button size="small" @click="cancel">No!</el-button>
  142. <el-button
  143. size="small"
  144. type="danger"
  145. @click="confirm"
  146. >
  147. Yes?
  148. </el-button>
  149. </template>
  150. </el-popconfirm>
  151. </template>
  152. <template #pager>
  153. <vxe-pager
  154. v-model:currentPage="gridOptions.pagerConfig.page"
  155. v-model:pageSize="gridOptions.pagerConfig.limit"
  156. :total="gridOptions.pagerConfig.total"
  157. class="mt-1.5"
  158. @page-change="handlePageChange"
  159. />
  160. </template>
  161. <template #empty>
  162. <el-empty description="暂无数据" />
  163. </template>
  164. <!-- 自定义列插槽 -->
  165. <template v-for="col in OnlineMerchandiseColumns" #[`${col.field}`]="{ row }">
  166. <DataTableSlot :key="row.id" :field="col.field" :row="row" />
  167. </template>
  168. </vxe-grid>
  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>