DataTable.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <script lang="ts" setup>
  2. /**
  3. * @Name: DataTable.vue
  4. * @Description:
  5. * @Author: xinyan
  6. */
  7. import { Download, Plus, Refresh, Upload } from '@element-plus/icons-vue';
  8. import { ElMessage } from 'element-plus';
  9. import { usePagination } from '/@/utils/usePagination';
  10. import { useTableData } from '/@/utils/useTableData';
  11. import DataTableSlot from './DataTableSlot.vue';
  12. import { downloadFile } from '/@/utils/service';
  13. import PermissionButton from '/src/components/PermissionButton/index.vue';
  14. import ImportButton from '/src/components/ImportButton/index.vue';
  15. import VerticalDivider from '/src/components/VerticalDivider/index.vue';
  16. import EditDrawer from '/src/views/product-manage/product-list/component/EditDrawer.vue';
  17. import NoticeDialog from '/src/views/product-manage/product-list/component/NoticeDialog.vue';
  18. import * as api from '../api';
  19. import { useResponse } from '/@/utils/useResponse';
  20. import {
  21. CostDetailColumns,
  22. DirectSalesCheckColumns_Regular,
  23. SupplyCheckColumns,
  24. SupplyCheckColumns_Regular,
  25. } from '/@/views/price-approval/Columns';
  26. import router from '/@/router';
  27. interface Parameter {
  28. description: string;
  29. platform: string;
  30. station: string;
  31. }
  32. const queryParameter: Parameter | undefined = inject('query-parameter');
  33. const { tableOptions, handlePageChange } = usePagination(fetchList);
  34. const gridRef = ref();
  35. const gridOptions: any = reactive({
  36. size: 'mini',
  37. border: false,
  38. round: true,
  39. stripe: true,
  40. currentRowHighLight: true,
  41. height: '100%',
  42. toolbarConfig: {
  43. size: 'large',
  44. slots: {
  45. buttons: 'toolbar_buttons',
  46. tools: 'toolbar_tools',
  47. },
  48. },
  49. rowConfig: {
  50. isHover: true,
  51. },
  52. columnConfig: {
  53. resizable: true,
  54. },
  55. pagerConfig: {
  56. total: tableOptions.value.total,
  57. page: tableOptions.value.page,
  58. limit: tableOptions.value.limit,
  59. },
  60. loading: false,
  61. loadingConfig: {
  62. icon: 'vxe-icon-indicator roll',
  63. text: '正在拼命加载中...',
  64. },
  65. columns: '',
  66. data: '',
  67. });
  68. const checkedList = ref<Set<number>>(new Set());
  69. const btnLoading = ref(false);
  70. const editOpen = ref(false);
  71. const rowData = ref({});
  72. const dialogVisible = ref(false);
  73. const templateType = ref('cost');
  74. onBeforeMount(() => {
  75. gridOptions.pagerConfig.limit = 10;
  76. });
  77. onMounted(() => {
  78. fetchList();
  79. });
  80. async function fetchList(isQuery = false) {
  81. if (isQuery) {
  82. gridOptions.pagerConfig.page = 1;
  83. }
  84. gridOptions.data = [];
  85. gridOptions.columns = [];
  86. const query = {
  87. description: queryParameter?.description,
  88. platform: queryParameter?.platform,
  89. station: queryParameter?.station,
  90. };
  91. await useTableData(api.getTableData, query, gridOptions);
  92. await gridRef.value.loadColumn(DirectSalesCheckColumns_Regular);
  93. gridOptions.showHeader = Boolean(gridOptions.data?.length);
  94. }
  95. function handleRefresh() {
  96. fetchList();
  97. }
  98. async function handleDownload() {
  99. gridOptions.loading = true;
  100. try {
  101. const query = {
  102. description: queryParameter?.description,
  103. platform: queryParameter?.platform,
  104. station: queryParameter?.station,
  105. };
  106. const response = await api.exportData(query);
  107. const url = window.URL.createObjectURL(new Blob([response.data]));
  108. const link = document.createElement('a');
  109. link.href = url;
  110. link.setAttribute('download', '商品列表数据.xlsx');
  111. document.body.appendChild(link);
  112. link.click();
  113. ElMessage.success('数据导出成功!');
  114. } catch (error) {
  115. ElMessage.error('数据导出失败,请重试!');
  116. console.error(error);
  117. } finally {
  118. gridOptions.loading = false; // 结束加载状态
  119. }
  120. }
  121. function selectChangeEvent({ checked, row }: any) {
  122. if (checked) {
  123. checkedList.value.add(row.id); // 获取单个数据
  124. } else {
  125. checkedList.value.delete(row.id);
  126. }
  127. }
  128. function selectAllChangeEvent({ checked }: any) {
  129. const $grid = gridRef.value;
  130. if ($grid) {
  131. const records = $grid.getData(); // 获取所有数据
  132. if (checked) {
  133. records.forEach((item: any) => {
  134. checkedList.value.add(item.id);
  135. });
  136. } else {
  137. checkedList.value.clear();
  138. }
  139. }
  140. }
  141. function handleCreate() {
  142. router.push({ name: 'AddPage', query: { type: 'direct' } });
  143. }
  144. function handleEdit(row: any) {
  145. editOpen.value = true;
  146. rowData.value = row;
  147. }
  148. async function singleDelete(row: any) {
  149. const res = await useResponse(api.deleteRow, row);
  150. if (res.code === 2000) {
  151. ElMessage.error({ message: '已删除!', plain: true, icon: 'Delete' });
  152. handleRefresh();
  153. }
  154. }
  155. function downloadTemplate() {
  156. const url = '/api/choice/reviews_monitor/import_data/';
  157. const fileName = '商品监控模板.xlsx';
  158. if (url) {
  159. downloadFile({
  160. url,
  161. method: 'GET',
  162. filename: fileName,
  163. });
  164. } else {
  165. console.error('未知的模板类型:', templateType.value);
  166. }
  167. }
  168. const gridEvents = {
  169. custom({ type }: any) {
  170. if (type == 'confirm') {
  171. fetchList();
  172. }
  173. },
  174. };
  175. function cellStyle(){
  176. return{
  177. fontWeight:500,
  178. }
  179. }
  180. defineExpose({ fetchList });
  181. </script>
  182. <template>
  183. <vxe-grid ref="gridRef" v-bind="gridOptions" v-on="gridEvents" @checkbox-change="selectChangeEvent" @checkbox-all="selectAllChangeEvent" :cell-style="cellStyle">
  184. <!-- 工具栏左侧 -->
  185. <template #toolbar_buttons>
  186. <div class="flex gap-2">
  187. <!--<div>-->
  188. <!-- <el-popconfirm :icon="InfoFilled" icon-color="#626AEF" title="你确定要删除此项吗?" width="220" @confirm="batchDelete">-->
  189. <!-- <template #reference>-->
  190. <!-- <PermissionButton :disabled="!checkedList.size" :icon="Delete" plain round type="danger"> 批量删除 </PermissionButton>-->
  191. <!-- </template>-->
  192. <!-- <template #actions="{ confirm, cancel }">-->
  193. <!-- <el-button size="small" @click="cancel">No!</el-button>-->
  194. <!-- <el-button size="small" type="danger" @click="confirm">Yes?</el-button>-->
  195. <!-- </template>-->
  196. <!-- </el-popconfirm>-->
  197. <!--</div>-->
  198. <div>
  199. <PermissionButton :icon="Plus" plain round type="primary" @click="handleCreate">新 增</PermissionButton>
  200. </div>
  201. <div class="custom-el-input">
  202. <el-select v-model="templateType" style="width: 190px">
  203. <template #prefix>
  204. <div class="flex items-center">
  205. <el-button
  206. size="small"
  207. style="margin-left: -7px; font-size: 14px; border-radius: 29px"
  208. text
  209. type="success"
  210. @click.stop="downloadTemplate"
  211. >
  212. 下载
  213. </el-button>
  214. <VerticalDivider style="margin-left: 7px" />
  215. </div>
  216. </template>
  217. <el-option label="成本查看模板" value="cost" />
  218. </el-select>
  219. </div>
  220. <VerticalDivider class="px-1" style="margin-left: 7px" />
  221. <ImportButton :icon="Upload" :uploadFunction="api.upload" bg text>导 入</ImportButton>
  222. </div>
  223. </template>
  224. <!-- 工具栏右侧 -->
  225. <template #toolbar_tools>
  226. <el-button circle class="toolbar-btn" @click="handleRefresh">
  227. <el-icon>
  228. <Refresh />
  229. </el-icon>
  230. </el-button>
  231. <el-button circle class="toolbar-btn" @click="handleDownload">
  232. <el-icon>
  233. <Download />
  234. </el-icon>
  235. </el-button>
  236. </template>
  237. <template #top>
  238. <div class="mb-2"></div>
  239. </template>
  240. <!-- 分页 -->
  241. <template #pager>
  242. <vxe-pager
  243. v-model:currentPage="gridOptions.pagerConfig.page"
  244. v-model:pageSize="gridOptions.pagerConfig.limit"
  245. :total="gridOptions.pagerConfig.total"
  246. class="mt-1.5"
  247. @page-change="handlePageChange"
  248. />
  249. </template>
  250. <template #empty>
  251. <el-empty description="暂无数据" />
  252. </template>
  253. <!-- 自定义列插槽 -->
  254. <template v-for="col in DirectSalesCheckColumns_Regular" #[`${col.field}`]="{ row }">
  255. <DataTableSlot :field="col.field" :row="row" @edit-row="handleEdit" @handle-delete="singleDelete" />
  256. </template>
  257. </vxe-grid>
  258. <EditDrawer v-if="editOpen" v-model="editOpen" :row-data="rowData" @refresh="handleRefresh" />
  259. <NoticeDialog v-if="dialogVisible" v-model="dialogVisible" :row-data="rowData" />
  260. </template>
  261. <style scoped>
  262. .toolbar-btn {
  263. width: 34px;
  264. height: 34px;
  265. font-size: 18px;
  266. }
  267. :deep(.custom-el-input .el-select__wrapper) {
  268. border-radius: 20px;
  269. }
  270. </style>