From 4335c3bfe2ad1196a6ab4fe726129e0777d9fa06 Mon Sep 17 00:00:00 2001 From: lzy-Jerry <763626908@qq.com> Date: Thu, 16 Nov 2023 19:51:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=AF=B9search=20par?= =?UTF-8?q?ams=E5=8A=A0=E5=B7=A5=E6=89=A9=E5=B1=95=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ProTable/index.vue | 10 +++++++++- src/hooks/useTable.ts | 5 ++++- src/views/proTable/useProTable/index.vue | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/components/ProTable/index.vue b/src/components/ProTable/index.vue index 552d45a5..b3b5fce7 100644 --- a/src/components/ProTable/index.vue +++ b/src/components/ProTable/index.vue @@ -123,6 +123,7 @@ export interface ProTableProps { requestAuto?: boolean; // 是否自动执行请求 api ==> 非必传(默认为true) requestError?: (params: any) => void; // 表格 api 请求错误监听 ==> 非必传 dataCallback?: (data: any) => any; // 返回数据的回调函数,可以对数据进行处理 ==> 非必传 + searchParamsCallBack?: (data: any) => any; // 搜索参数的回调函数,可以对数据进行处理 ==> 非必传 title?: string; // 表格标题 ==> 非必传 pagination?: boolean; // 是否需要分页组件 ==> 非必传(默认为true) initParam?: any; // 初始化请求参数 ==> 非必传(默认为{}) @@ -166,7 +167,14 @@ const { selectionChange, selectedList, selectedListIds, isSelected } = useSelect // 表格操作 Hooks const { tableData, pageable, searchParam, searchInitParam, getTableList, search, reset, handleSizeChange, handleCurrentChange } = - useTable(props.requestApi, props.initParam, props.pagination, props.dataCallback, props.requestError); + useTable( + props.requestApi, + props.initParam, + props.pagination, + props.dataCallback, + props.requestError, + props.searchParamsCallBack + ); // 清空选中数据列表 const clearSelection = () => tableRef.value!.clearSelection(); diff --git a/src/hooks/useTable.ts b/src/hooks/useTable.ts index 4decc65c..aace9d8b 100644 --- a/src/hooks/useTable.ts +++ b/src/hooks/useTable.ts @@ -7,13 +7,15 @@ import { reactive, computed, toRefs } from "vue"; * @param {Object} initParam 获取数据初始化参数 (非必传,默认为{}) * @param {Boolean} isPageable 是否有分页 (非必传,默认为true) * @param {Function} dataCallBack 对后台返回的数据进行处理的方法 (非必传) + * @param {Function} searchParamsCallBack 对搜索参数进行处理的方法 (非必传) * */ export const useTable = ( api?: (params: any) => Promise, initParam: object = {}, isPageable: boolean = true, dataCallBack?: (data: any) => any, - requestError?: (error: any) => void + requestError?: (error: any) => void, + searchParamsCallBack?: (data: any) => any ) => { const state = reactive({ // 表格数据 @@ -87,6 +89,7 @@ export const useTable = ( nowSearchParam[key] = state.searchParam[key]; } } + searchParamsCallBack && (nowSearchParam = searchParamsCallBack(nowSearchParam)); Object.assign(state.totalParam, nowSearchParam, isPageable ? pageParam.value : {}); }; diff --git a/src/views/proTable/useProTable/index.vue b/src/views/proTable/useProTable/index.vue index f9235cfe..59cf0970 100644 --- a/src/views/proTable/useProTable/index.vue +++ b/src/views/proTable/useProTable/index.vue @@ -6,6 +6,7 @@ :request-api="getTableList" :init-param="initParam" :data-callback="dataCallback" + :search-params-call-back="searchParamsCallBack" @darg-sort="sortTable" > @@ -97,6 +98,13 @@ const dataCallback = (data: any) => { }; }; +// searchParamsCallBack 提供了为搜索的参数进行扩展加工处理的方法 +const searchParamsCallBack = (params: any) => { + return { + ...params + }; +}; + // 如果你想在请求之前对当前请求参数做一些操作,可以自定义如下函数:params 为当前所有的请求参数(包括分页),最后返回请求列表接口 // 默认不做操作就直接在 ProTable 组件上绑定 :requestApi="getUserList" const getTableList = (params: any) => {