index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import BsTable from './Table.vue';
  2. import { reactive, onMounted, onBeforeMount, toRaw } from 'vue';
  3. import { set, get, isEmpty, isUndefined } from 'lodash';
  4. import {getTableDataApi} from "/@/api/system/table-api.js";
  5. export const useBsTable = (options, tableRef) => {
  6. let tableOptions = reactive(options.tableOptions);
  7. let gridRef = null;
  8. const initGridOptions = () => {
  9. set(tableOptions, 'gridOptions', {
  10. border: 'none',
  11. size: 'mini',
  12. showHeaderOverflow: true,
  13. showOverflow: true,
  14. stripe: true,
  15. round: true,
  16. ...tableOptions?.gridOptions,
  17. });
  18. set(tableOptions, 'getGridRef', (value) => {
  19. gridRef = value;
  20. });
  21. };
  22. const initTableData = () => {
  23. const tableData = get(tableOptions.gridOptions, 'data', []);
  24. setTableData(tableData);
  25. };
  26. const handleSearchFields = () => {
  27. const fields = get(tableOptions, 'searchConfig.fields', []);
  28. const newFields = fields.map((el, index) => {
  29. return {
  30. ...el,
  31. visible: index < 1,
  32. };
  33. });
  34. set(tableOptions, 'searchConfig.fields', newFields);
  35. set(tableOptions, 'searchConfig.onSearch', async () => {
  36. setPageNum(1);
  37. await getTableData();
  38. });
  39. set(tableOptions, 'searchConfig.onReset', async () => {
  40. setSearchParams({});
  41. setPageNum(1);
  42. await getTableData();
  43. });
  44. };
  45. const toggleSearchFields = () => {
  46. const fields = get(tableOptions, 'searchConfig.fields', []);
  47. if (!isEmpty(fields) && fields.length > 1) {
  48. fields.forEach((item, index) => {
  49. if (index > 0) {
  50. item.visible = !item.visible;
  51. }
  52. });
  53. }
  54. };
  55. const getGridRef = () => {
  56. return gridRef;
  57. };
  58. const getSearchParams = () => {
  59. return get(tableOptions, 'searchConfig.data', {});
  60. };
  61. const setSearchParams = (searchParams) => {
  62. set(tableOptions, 'searchConfig.data', searchParams);
  63. };
  64. const setLoading = (loading) => {
  65. set(tableOptions, 'gridOptions.loading', loading);
  66. };
  67. const setPageNum = (value) => {
  68. set(tableOptions, 'pagerConfig.pageNum', value);
  69. };
  70. const setPageSize = (value) => {
  71. set(tableOptions, 'pagerConfig.pageSize', value);
  72. };
  73. const setPageTotal = (value) => {
  74. set(tableOptions, 'pagerConfig.total', value);
  75. };
  76. const setTableData = (data) => {
  77. set(tableOptions, 'gridOptions.data', data);
  78. };
  79. const setTableOptions = (options) => {
  80. tableOptions = options;
  81. }
  82. const getTableData = () => {
  83. tableOptions.tableSearchBeforeBiz && tableOptions.tableSearchBeforeBiz();
  84. setLoading(true);
  85. return new Promise(async (resolve, reject) => {
  86. const pageNum = tableOptions?.pagerConfig.pageNum;
  87. const pageSize = tableOptions?.pagerConfig.pageSize;
  88. const searchParams = getSearchParams();
  89. if (isEmpty(tableOptions?.url)) {
  90. setTimeout(() => {
  91. setLoading(false);
  92. setPageTotal(tableOptions.gridOptions.data.length);
  93. setPagerVisible(false);
  94. resolve(tableOptions.gridOptions.data);
  95. }, 500);
  96. } else {
  97. console.log("入参为 =====>", { pageNum, pageSize, ...searchParams })
  98. const resData = await getTableDataApi(tableOptions?.url,{ pageNum, pageSize, ...searchParams });
  99. const { data } = resData;
  100. setLoading(false);
  101. setTableData(data.list);
  102. setPageTotal(data.total);
  103. resolve(data);
  104. // setTimeout(() => {
  105. // setLoading(false);
  106. // setTableData([{name: "12334"}]);
  107. // setPageTotal(1000);
  108. // resolve();
  109. // }, 1000);
  110. }
  111. });
  112. };
  113. const setTablePropsValue = (path, value) => {
  114. set(tableOptions, path, value);
  115. };
  116. const getTablePropsValue = (path) => {
  117. return toRaw(get(tableOptions, path, undefined));
  118. };
  119. const initPageEvent = () => {
  120. const visible = get(tableOptions, 'pagerConfig.enable', true);
  121. setPagerVisible(visible);
  122. set(tableOptions, 'pagerConfig.onChange', async () => {
  123. await getTableData();
  124. });
  125. };
  126. const initSearchConfig = () => {
  127. handleSearchFields();
  128. isUndefined(tableOptions?.searchConfig?.data) && set(tableOptions, 'searchConfig.data', {});
  129. set(tableOptions, 'searchConfig.onToggle', toggleSearchFields);
  130. const visible = get(tableOptions, 'searchConfig.enable', true);
  131. setSearchVisible(visible);
  132. };
  133. const initPagerConfig = () => {
  134. initPageEvent();
  135. };
  136. const initToolbarConfig = () => {
  137. const visible = get(tableOptions, 'toolbarConfig.enable', true);
  138. setToolBarVisible(visible);
  139. set(tableOptions, 'toolbarConfig.onRefresh', async () => {
  140. setPageNum(1);
  141. await getTableData();
  142. });
  143. };
  144. const onTableBeforeMount = (callback) => {
  145. onBeforeMount(() => {
  146. if (typeof callback === 'function') {
  147. callback();
  148. }
  149. });
  150. };
  151. const onTableMounted = (callback) => {
  152. onMounted(() => {
  153. if (typeof callback === 'function') {
  154. const gridRef = getGridRef();
  155. callback(gridRef);
  156. }
  157. });
  158. };
  159. onMounted(async () => {
  160. initTableData();
  161. initGridOptions();
  162. initToolbarConfig();
  163. initPagerConfig();
  164. initSearchConfig();
  165. onTableBeforeMount();
  166. options.beforeMount && options.beforeMount();
  167. await getTableData();
  168. onTableMounted();
  169. options.mounted && options.mounted();
  170. });
  171. const setToolBarVisible = (visible) => {
  172. set(tableOptions, 'toolbarConfig.enable', visible);
  173. };
  174. const setSearchVisible = (visible) => {
  175. set(tableOptions, 'searchConfig.enable', visible);
  176. };
  177. const setPagerVisible = (visible) => {
  178. set(tableOptions, 'pagerConfig.enable', visible);
  179. };
  180. return {
  181. tableOptions,
  182. onTableMounted,
  183. onTableBeforeMount,
  184. setTablePropsValue,
  185. getTablePropsValue,
  186. setTableOptions,
  187. getGridRef,
  188. };
  189. };
  190. export default BsTable;