多页签ProTable封装实现
场景
从老系统迁移过来,老系统对于表格存在多页签展示方法,因为数据比较多,新页面延续这种设计
实现方式
proTable
的toolBar
属性支持我们自定义toolBar
,我们可以将menu
配置为tabItem
- 为
tabItem
绑定onChange
事件,并且在点击以后,刷新table
里面的数据,避免出现数据缓存问题 - 如果我们不进行请求的话,那么从
A
页签切换到B
页签,那么A
页签的数据,会默认带到B
页签里,B
页签不会主动自己请求数据,除非你在onChange
里写
代码展示
typescript
interface ITabItem {
btnContent: () => React.ReactNode;
tabItems: {
key: string;
label: string;
}[];
tabContent: {
tab1: {
columns: ProColumns[];
request: (params: any) => Promise<{
data: any;
success: any;
total: any;
}>;
selectedRowKeys: Key[];
setSelectedRowKeys: (params: Key[]) => void;
setSelectedRows: (params: unknown[]) => void;
actionRef: React.MutableRefObject<ActionType | undefined>;
};
tab2: {
columns: ProColumns[];
request: (params: any) => Promise<{
data: any;
success: any;
total: any;
}>;
handleDetail: (record: any) => void; // 双击,弹出弹框,获取数据接口
actionRef: React.MutableRefObject<ActionType | undefined>;
};
};
}
btnContent
:每个proTable
的按钮配置tabItems
:你设置的页签,里面包含了页签的唯一值key
和页签的名字label
,这边我全部设置为了tab1
和tab2
,因为我们目前场景是只有两个tab
页面tabContent
:页签内容columns
:页签的 table 的列配置request
:Protable
请求配置actionRef
:Protable
的action
,这个主要是用于,后续页签改变请求数据用的
typescript
import type { ActionType, ProColumns } from "@ant-design/pro-table";
import ProTable from "@ant-design/pro-table";
import type { Key } from "react";
import React, { useState } from "react";
export default ({ btnContent, tabItems, tabContent }: ITabItem) => {
const [activeKey, setActiveKey] = useState<string>("tab1");
return (
<ProTable
rowKey="id"
dateFormatter="string"
form={{ labelWrap: true }}
actionRef={tabContent[activeKey as keyof typeof tabContent].actionRef}
columns={tabContent[activeKey as keyof typeof tabContent].columns}
request={(params) => {
return tabContent[activeKey as keyof typeof tabContent].request(params);
}}
toolbar={{
menu: {
type: "tab",
activeKey: activeKey,
items: tabItems,
onChange: (key) => {
setActiveKey(key as string);
tabContent[
activeKey as keyof typeof tabContent
].actionRef.current?.reload();
},
},
actions: [btnContent()],
}}
scroll={{ x: 2000 }}
rowSelection={
activeKey === "tab1"
? {
selectedRowKeys: tabContent[activeKey].selectedRowKeys,
onChange: (selectedRowKeys, selectedRows) => {
tabContent[activeKey].setSelectedRowKeys(selectedRowKeys);
tabContent[activeKey].setSelectedRows(selectedRows);
},
}
: undefined
}
onRow={
activeKey === "tab1"
? undefined
: (record) => {
return {
onDoubleClick: () => {
tabContent["tab2"].handleDetail(record);
},
};
}
}
/>
);
};
补充
proTable
使用request
的时候,配置的 columns
的key
不可以有重复的,否则每次切换 tab
都会是request
的输入框个数增加