CommonTable.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <ele-card :body-style="{ paddingTop: '8px', ...bodyStyle }" :flex-table="flexTable">
  3. <!-- 表格 -->
  4. <ele-pro-table ref="tableRef" :row-key="pageConfig.rowKey || 'id'" :columns="columns"
  5. :datasource="finalDatasource" :show-overflow-tooltip="true" v-model:selections="selections"
  6. highlight-current-row :export-config="{ fileName: pageConfig.fileName }" :cache-key="pageConfig.cacheKey"
  7. border v-bind="$attrs">
  8. <template v-for="(val, key) in slotArray" #[key]="{ row }">
  9. <slot :name="key" :row="row"></slot>
  10. </template>
  11. </ele-pro-table>
  12. </ele-card>
  13. </template>
  14. <script setup>
  15. import { ref, getCurrentInstance, useSlots } from "vue";
  16. import { ElMessageBox } from "element-plus/es";
  17. import { EleMessage } from "ele-admin-plus/es";
  18. import { useDictData } from "@/utils/use-dict-data";
  19. import { download, toFormData, checkDownloadRes } from "@/utils/common";
  20. const slotArray = useSlots();
  21. let props = defineProps({
  22. pageConfig: {
  23. type: Object,
  24. default: () => ({
  25. cacheKey: "recycleOrderTable",
  26. fileName: "回收订单查询",
  27. rowKey: "id",
  28. }),
  29. },
  30. pageUrl: { type: String, default: "" },
  31. columns: { type: Array, default: () => [] },
  32. bodyStyle: { type: Object, default: () => ({}) },
  33. flexTable: { type: Boolean, default: true },
  34. sortFunction: { type: Function },
  35. datasource: { type: Function },
  36. });
  37. let { proxy } = getCurrentInstance();
  38. /** 表格实例 */
  39. const tableRef = ref(null);
  40. /** 表格选中数据 */
  41. const selections = ref([]);
  42. async function queryPage(params) {
  43. let url = props.pageConfig.pageUrl || props.pageUrl;
  44. if (!url) {
  45. return Promise.reject(new Error("请配置页面URL"));
  46. }
  47. const res = await proxy.$http.get(url, { params });
  48. if (res.data.code === 200) {
  49. return res.data;
  50. }
  51. return Promise.reject(new Error(res.data.msg));
  52. }
  53. /** 表格数据源 */
  54. const whereCache = ref({});
  55. const defaultDatasource = (table) => {
  56. let { pages, where, orders, sorter } = table;
  57. let initKeys = props.pageConfig.params || {};
  58. whereCache.value = where;
  59. let tempOrders = {};
  60. console.log('orders', orders);
  61. if (orders && orders.orderByColumn) {
  62. tempOrders.orderType = sorter.column.columnKey;
  63. tempOrders.orderWay = orders.isAsc === "ascending" ? "asc" : "desc";
  64. }
  65. // 兼容激活码排序
  66. if (props.sortFunction) {
  67. tempOrders = props.sortFunction(orders);
  68. }
  69. return queryPage({ ...initKeys, ...where, ...tempOrders, ...pages });
  70. };
  71. const finalDatasource = (table) => {
  72. if (props.datasource) {
  73. return props.datasource(table);
  74. }
  75. return defaultDatasource(table);
  76. };
  77. /** 搜索 */
  78. const reload = (where = {}) => {
  79. let data = where && where.search ? { page: 1, where } : { where: { ...whereCache.value, ...where } };
  80. delete data.where?.search;
  81. delete data.where?.isReset;
  82. tableRef.value?.reload?.(data);
  83. };
  84. /** 批量操作 */
  85. const operatBatch = ({ method, row, url, title, data = {} }) => {
  86. const rows = row == null ? selections.value : [row];
  87. if (!rows.length) {
  88. EleMessage.error("请至少选择一条数据");
  89. return;
  90. }
  91. title = title || "是否确认当前操作?";
  92. ElMessageBox.confirm(title, "提示", {
  93. type: "warning",
  94. draggable: true,
  95. })
  96. .then(() => {
  97. const loading = EleMessage.loading({
  98. message: "请求中..",
  99. plain: true,
  100. });
  101. proxy.$http[method](url, data)
  102. .then((res) => {
  103. if (res.data.code === 200) {
  104. EleMessage.success("操作成功");
  105. reload();
  106. } else {
  107. EleMessage.error(res.data.msg);
  108. }
  109. loading.close();
  110. })
  111. .catch((e) => {
  112. loading.close();
  113. EleMessage.error(e.message);
  114. });
  115. })
  116. .catch(() => { });
  117. };
  118. /// 导出数据
  119. async function exportPage(params, name, method = "POST") {
  120. const res = await proxy.$http({
  121. url: props.pageConfig.exportUrl,
  122. method,
  123. data: toFormData(params),
  124. responseType: "blob",
  125. });
  126. await checkDownloadRes(res);
  127. download(
  128. res.data,
  129. name ? `${name}_${Date.now()}.xlsx` : `post_${Date.now()}.xlsx`
  130. );
  131. }
  132. //导出数据 进导出记录
  133. function exportRecord(params, method = "POST") {
  134. let data = method === "POST" ? { data: params } : { params };
  135. return proxy.$http({
  136. url: props.pageConfig.exportUrl,
  137. method,
  138. ...data,
  139. }); // 导出记录
  140. }
  141. /** 导出数据 */
  142. const exportData = (name, method = "POST") => {
  143. const loading = EleMessage.loading({
  144. message: "请求中..",
  145. plain: true,
  146. });
  147. tableRef.value?.fetch?.(({ where, orders }) => {
  148. let initKeys = props.pageConfig.params || {};
  149. console.log('where', where);
  150. console.log('orders', orders);
  151. console.log('initKeys', initKeys);
  152. exportRecord({ ...where, ...orders, ...initKeys }, method)
  153. .then((res) => {
  154. if (res.data.code === 200) {
  155. EleMessage.success("操作成功,请前往导出记录下载");
  156. } else {
  157. EleMessage.error(res.data.msg);
  158. }
  159. loading.close();
  160. })
  161. .catch((e) => {
  162. loading.close();
  163. EleMessage.error(e.message);
  164. });
  165. });
  166. };
  167. function messageBoxConfirm({ message, fetch }) {
  168. ElMessageBox.confirm(message, "提示", {
  169. confirmButtonText: "确定",
  170. cancelButtonText: "关闭",
  171. type: "warning",
  172. }).then(() => {
  173. fetch().then((res) => {
  174. if (res.data.code === 200) {
  175. EleMessage.success("操作成功");
  176. reload();
  177. } else {
  178. EleMessage.error(res.data.msg);
  179. }
  180. });
  181. });
  182. }
  183. function getSelections() {
  184. console.log(selections.value, "selections.value");
  185. return selections.value;
  186. }
  187. function setSelectedRows(rows) {
  188. selections.value = rows;
  189. // Also try to toggle if table instance is available and supports it,
  190. // ensuring visual sync if v-model isn't enough for pre-load
  191. if (tableRef.value?.toggleRowSelection) {
  192. rows.forEach(row => {
  193. tableRef.value.toggleRowSelection(row, true);
  194. });
  195. }
  196. }
  197. function clearSelection() {
  198. selections.value = [];
  199. tableRef.value?.clearSelection?.();
  200. }
  201. defineExpose({
  202. reload,
  203. exportData,
  204. operatBatch,
  205. getSelections,
  206. setSelectedRows,
  207. clearSelection,
  208. messageBoxConfirm,
  209. });
  210. </script>