Преглед на файлове

fix: BsUi-表格逻辑修改

hanxiaohui преди 5 месеца
родител
ревизия
b68133ad93
променени са 1 файла, в които са добавени 34 реда и са изтрити 21 реда
  1. 34 21
      src/components/BsUi/Table/index.js

+ 34 - 21
src/components/BsUi/Table/index.js

@@ -1,8 +1,7 @@
 import BsTable from './Table.vue';
 import { reactive, onMounted, onBeforeMount, toRaw } from 'vue';
-import { set, get, isEmpty, isUndefined } from 'lodash';
-import {getTableDataApi} from "/@/api/system/table-api.js";
-
+import { set, get, isEmpty, isUndefined, forEach } from 'lodash';
+import { getTableDataApi } from '/@/api/system/table-api.js';
 
 export const useBsTable = (options, tableRef) => {
   let tableOptions = reactive(options.tableOptions);
@@ -42,13 +41,20 @@ export const useBsTable = (options, tableRef) => {
 
     set(tableOptions, 'searchConfig.fields', newFields);
 
-    set(tableOptions, 'searchConfig.onSearch',  async () => {
+    set(tableOptions, 'searchConfig.onSearch', async () => {
       setPageNum(1);
       await getTableData();
     });
 
-    set(tableOptions, 'searchConfig.onReset',  async () => {
-      setSearchParams({});
+    set(tableOptions, 'searchConfig.onReset', async () => {
+      const searchFields = get(tableOptions, 'searchConfig.fields').map((el) => el.field);
+      const searchParams = getSearchParams();
+      forEach(searchParams, (value, key) => {
+        if (searchFields.includes(key)) {
+          searchParams[key] = undefined;
+        }
+      });
+      setSearchParams(searchParams);
       setPageNum(1);
       await getTableData();
     });
@@ -98,27 +104,35 @@ export const useBsTable = (options, tableRef) => {
     set(tableOptions, 'gridOptions.data', data);
   };
 
-  const setTableOptions  = (options) => {
+  const setTableOptions = (options) => {
     tableOptions = options;
-  }
+  };
+
 
   const getTableData = (customParams) => {
     tableOptions.tableSearchBeforeBiz && tableOptions.tableSearchBeforeBiz();
+    const otherParams = !isEmpty(customParams) ? { ...customParams } : {};
+    const oldSearchParams = getSearchParams();
+    setSearchParams({...oldSearchParams, ...otherParams })
+    const pageNum = tableOptions?.pagerConfig.pageNum;
+    const pageSize = tableOptions?.pagerConfig.pageSize;
+    const newSearchParams = getSearchParams();
     setLoading(true);
     return new Promise(async (resolve, reject) => {
-      const pageNum = tableOptions?.pagerConfig.pageNum;
-      const pageSize = tableOptions?.pagerConfig.pageSize;
-      const searchParams = getSearchParams();
-
       if (isEmpty(tableOptions?.url)) {
         setLoading(false);
         setPageTotal(tableOptions.gridOptions.data.length);
         setPagerVisible(false);
         resolve(tableOptions.gridOptions.data);
       } else {
-        const otherParams = !isEmpty(customParams) ? { ...customParams } : {};
-        console.log("入参为 =====>", { pageNum, pageSize, ...searchParams, ...otherParams })
-        const resData = await getTableDataApi(tableOptions?.url,{ pageNum, pageSize, ...searchParams, ...otherParams });
+        // console.log('入参为 =====>', { pageNum, pageSize, ...newSearchParams });
+        const resData = await getTableDataApi(tableOptions?.url, { pageNum, pageSize, ...newSearchParams });
+        // const resData = {
+        //   data:  {
+        //     list: [{ name: "韩晓辉" }],
+        //     total: 1
+        //   }
+        // }
         const { data } = resData;
         setLoading(false);
         setTableData(data.list);
@@ -140,7 +154,7 @@ export const useBsTable = (options, tableRef) => {
     const visible = get(tableOptions, 'pagerConfig.enable', true);
     setPagerVisible(visible);
 
-    set(tableOptions, 'pagerConfig.onChange',  async () => {
+    set(tableOptions, 'pagerConfig.onChange', async () => {
       await getTableData();
     });
   };
@@ -161,11 +175,10 @@ export const useBsTable = (options, tableRef) => {
     const visible = get(tableOptions, 'toolbarConfig.enable', true);
     setToolBarVisible(visible);
 
-    set(tableOptions, 'toolbarConfig.onRefresh',  async () => {
+    set(tableOptions, 'toolbarConfig.onRefresh', async () => {
       setPageNum(1);
       await getTableData();
     });
-
   };
 
   const onTableBeforeMount = (callback) => {
@@ -213,12 +226,12 @@ export const useBsTable = (options, tableRef) => {
   const refreshTable = async () => {
     setPageNum(1);
     await getTableData();
-  }
+  };
 
   const fetchTableData = async (searchParams) => {
     setPageNum(1);
     await getTableData(searchParams);
-  }
+  };
 
   return {
     tableOptions,
@@ -229,7 +242,7 @@ export const useBsTable = (options, tableRef) => {
     setTableOptions,
     getGridRef,
     refreshTable,
-    fetchTableData
+    fetchTableData,
   };
 };
 export default BsTable;