index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import BsTable from './Table.vue';
  2. import { reactive, onMounted, onBeforeMount, toRaw, nextTick } from 'vue';
  3. import { set, get, isEmpty, isUndefined, forEach, has } from 'lodash';
  4. import { getTableDataApi } from '/@/api/system/table-api.js';
  5. import { PAGE_NUM, PAGE_SIZE_OPTIONS } from '/@/components/BsUi/constant.js';
  6. import { PAGE_SIZE } from '/@/constants/common-const.js';
  7. export const useBsTable = (options, tableRef) => {
  8. let tableOptions = reactive(options.tableOptions);
  9. let gridRef = null;
  10. const initGridOptions = () => {
  11. const optCol = tableOptions?.gridOptions.columns.find((v) => v?.cellRender && v.cellRender.name === 'CellOption');
  12. // 操作初始化
  13. if (!isEmpty(optCol)) {
  14. optCol.fixed = 'right';
  15. }
  16. ['seq', 'checkbox', 'radio'].forEach((v) => {
  17. const itemCol = tableOptions?.gridOptions.columns.find((vv) => vv.type === v);
  18. // 序号初始化
  19. if (!isEmpty(itemCol)) {
  20. itemCol.align = 'center';
  21. itemCol.headerAlign = 'center';
  22. itemCol.width = '60px';
  23. itemCol.fixed = 'left';
  24. }
  25. });
  26. // 初始化列
  27. tableOptions?.gridOptions.columns.forEach((col) => {
  28. if (!has(col, 'headerAlign')) {
  29. col.headerAlign = 'center';
  30. }
  31. });
  32. set(tableOptions, 'gridOptions', {
  33. border: 'full',
  34. showHeaderOverflow: true,
  35. showOverflow: true,
  36. stripe: false,
  37. round: true,
  38. cellConfig: {
  39. height: '40px',
  40. ...tableOptions.gridOptions?.cellConfig,
  41. },
  42. customConfig: {
  43. mode: 'drawer',
  44. ...tableOptions.gridOptions?.customConfig,
  45. },
  46. headerCellConfig: {
  47. height: '40px',
  48. ...tableOptions.gridOptions?.headerCellConfig,
  49. },
  50. height: '100%',
  51. ...tableOptions?.gridOptions,
  52. });
  53. set(tableOptions, 'getGridRef', (value) => {
  54. gridRef = value;
  55. });
  56. };
  57. const initTableData = () => {
  58. const tableData = get(tableOptions.gridOptions, 'data', []);
  59. setTableData(tableData);
  60. };
  61. const handleSearchFields = () => {
  62. const fields = get(tableOptions, 'searchConfig.fields', []);
  63. const newFields = fields.map((el, index) => {
  64. return {
  65. ...el,
  66. visible: index < 1,
  67. };
  68. });
  69. set(tableOptions, 'searchConfig.fields', newFields);
  70. set(tableOptions, 'searchConfig.onSearch', async () => {
  71. setPageNum(1);
  72. await getTableData();
  73. });
  74. set(tableOptions, 'searchConfig.onReset', async () => {
  75. const searchFields = get(tableOptions, 'searchConfig.fields').map((el) => el.field);
  76. const searchParams = getSearchParams();
  77. forEach(searchParams, (value, key) => {
  78. if (searchFields.includes(key)) {
  79. searchParams[key] = undefined;
  80. }
  81. });
  82. setSearchParams(searchParams);
  83. setPageNum(1);
  84. await getTableData();
  85. });
  86. };
  87. const toggleSearchFields = () => {
  88. const fields = get(tableOptions, 'searchConfig.fields', []);
  89. if (!isEmpty(fields) && fields.length > 1) {
  90. fields.forEach((item, index) => {
  91. if (index > 0) {
  92. item.visible = !item.visible;
  93. }
  94. });
  95. }
  96. };
  97. const getGridRef = () => {
  98. return gridRef;
  99. };
  100. const getSearchParams = () => {
  101. return get(tableOptions, 'searchConfig.data', {});
  102. };
  103. const setSearchParams = (searchParams) => {
  104. set(tableOptions, 'searchConfig.data', searchParams);
  105. };
  106. const setLoading = (loading) => {
  107. set(tableOptions, 'gridOptions.loading', loading);
  108. };
  109. const setPageNum = (value) => {
  110. set(tableOptions, 'pagerConfig.pageNum', value);
  111. };
  112. const setPageSize = (value) => {
  113. set(tableOptions, 'pagerConfig.pageSize', value);
  114. };
  115. const setPageTotal = (value) => {
  116. set(tableOptions, 'pagerConfig.total', value);
  117. };
  118. const setPageSizeOptions = (value) => {
  119. set(tableOptions, 'pagerConfig.pageSizeOptions', value);
  120. };
  121. const setTableData = (data) => {
  122. set(tableOptions, 'gridOptions.data', data);
  123. };
  124. const setTableOptions = (options) => {
  125. tableOptions = options;
  126. };
  127. const getTableData = (customParams) => {
  128. tableOptions.tableSearchBeforeBiz && tableOptions.tableSearchBeforeBiz();
  129. const otherParams = !isEmpty(customParams) ? { ...customParams } : {};
  130. const oldSearchParams = getSearchParams();
  131. setSearchParams({ ...oldSearchParams, ...otherParams });
  132. const pageNum = tableOptions?.pagerConfig.pageNum;
  133. const pageSize = tableOptions?.pagerConfig.pageSize;
  134. const newSearchParams = getSearchParams();
  135. setLoading(true);
  136. return new Promise(async (resolve, reject) => {
  137. if (tableOptions?.request) {
  138. const { list, total } = await tableOptions?.request({ pageNum, pageSize, ...newSearchParams });
  139. setLoading(false);
  140. setTableData(list);
  141. setPageTotal(total);
  142. nextTick(() => {
  143. tableOptions?.tableSearchAfterBiz && tableOptions.tableSearchAfterBiz({ gridRef });
  144. });
  145. resolve(list);
  146. } else {
  147. if (isEmpty(tableOptions?.url)) {
  148. setLoading(false);
  149. setPageTotal(tableOptions.gridOptions.data.length);
  150. setTableData(tableOptions.gridOptions.data);
  151. nextTick(() => {
  152. tableOptions?.tableSearchAfterBiz && tableOptions.tableSearchAfterBiz({ gridRef });
  153. });
  154. resolve(tableOptions.gridOptions.data);
  155. } else {
  156. const resData = await getTableDataApi(tableOptions?.url, { pageNum, pageSize, ...newSearchParams });
  157. const { data } = resData;
  158. setLoading(false);
  159. setTableData(data.list);
  160. setPageTotal(data.total);
  161. nextTick(() => {
  162. tableOptions?.tableSearchAfterBiz && tableOptions.tableSearchAfterBiz({ gridRef });
  163. });
  164. resolve(data);
  165. }
  166. }
  167. });
  168. };
  169. const setTablePropsValue = (path, value) => {
  170. set(tableOptions, path, value);
  171. };
  172. const getTablePropsValue = (path) => {
  173. return toRaw(get(tableOptions, path, undefined));
  174. };
  175. const initPageEvent = () => {
  176. set(tableOptions, 'pagerConfig.onChange', async () => {
  177. await getTableData();
  178. });
  179. };
  180. const initSearchConfig = () => {
  181. handleSearchFields();
  182. isUndefined(tableOptions?.searchConfig?.data) && set(tableOptions, 'searchConfig.data', {});
  183. set(tableOptions, 'searchConfig.onToggle', toggleSearchFields);
  184. const visible = get(tableOptions, 'searchConfig.enable', true);
  185. setSearchVisible(visible);
  186. };
  187. const initPagerConfig = () => {
  188. setPageNum(tableOptions?.pagerConfig?.pageNum || PAGE_NUM);
  189. setPageSize(tableOptions?.pagerConfig?.pageSize || PAGE_SIZE);
  190. setPageSizeOptions(tableOptions?.pagerConfig?.pageSizeOptions || PAGE_SIZE_OPTIONS);
  191. setPagerVisible(tableOptions?.pagerConfig?.enable || true);
  192. initPageEvent();
  193. };
  194. const initToolbarConfig = () => {
  195. const visible = get(tableOptions, 'toolbarConfig.enable', true);
  196. setToolBarVisible(visible);
  197. set(tableOptions, 'toolbarConfig.onRefresh', async () => {
  198. setPageNum(1);
  199. await getTableData();
  200. });
  201. };
  202. onMounted(async () => {
  203. initTableData();
  204. initGridOptions();
  205. initToolbarConfig();
  206. initPagerConfig();
  207. initSearchConfig();
  208. tableOptions.beforeMount && tableOptions.beforeMount();
  209. if (get(tableOptions, 'isLoadRequest', true)) {
  210. await getTableData();
  211. }
  212. });
  213. const setToolBarVisible = (visible) => {
  214. set(tableOptions, 'toolbarConfig.enable', visible);
  215. };
  216. const setSearchVisible = (visible) => {
  217. set(tableOptions, 'searchConfig.enable', visible);
  218. };
  219. const setPagerVisible = (visible) => {
  220. set(tableOptions, 'pagerConfig.enable', visible);
  221. };
  222. const refreshTable = async () => {
  223. setPageNum(tableOptions.pagerConfig?.pageNum || PAGE_NUM);
  224. setPageSize(tableOptions.pagerConfig?.pageSize || PAGE_SIZE);
  225. await getTableData();
  226. };
  227. const fetchTableData = async (searchParams) => {
  228. setPageNum(searchParams?.pageNum || PAGE_NUM);
  229. setPageSize(searchParams?.pageSize || PAGE_SIZE);
  230. await getTableData(searchParams);
  231. };
  232. const getSelectedTableData = () => {
  233. return [];
  234. };
  235. return {
  236. tableOptions,
  237. setTablePropsValue,
  238. getTablePropsValue,
  239. setTableOptions,
  240. getGridRef,
  241. refreshTable,
  242. fetchTableData,
  243. getSelectedTableData,
  244. };
  245. };
  246. export default BsTable;