|
|
1 ماه پیش | |
|---|---|---|
| .. | ||
| ProTable.tsx | 1 ماه پیش | |
| ProTable.types.ts | 1 ماه پیش | |
| ProTableTest.tsx | 1 ماه پیش | |
| README.md | 1 ماه پیش | |
| usage-example.tsx | 1 ماه پیش | |
自定义实现的 ProTable 组件,用于替代 @ant-design/pro-components 的 ProTable。
✅ 完整支持你当前代码中使用的所有功能:
reload() 和 reset() 方法title - 列标题dataIndex - 数据字段名key - 唯一标识width - 列宽search - 搜索配置hideInTable - 隐藏列tooltip - 提示信息ellipsis - 文本省略valueType - 值类型(date, dateTime, option 等)valueEnum - 枚举值(自动转换为过滤器)render - 自定义渲染函数filters - 过滤配置onFilter - 过滤函数sorter - 排序配置toolBarRender - 自定义工具栏按钮toolbar.menu - 标签页切换(如 my/collaboration/community)options.search - 关键词搜索功能将 ProTable.tsx 复制到你的项目中。
// 原来
import { ActionType, ProTable } from "@ant-design/pro-components";
// 现在
import ProTable, { ActionType } from "./components/ProTable";
import { useRef } from "react";
import ProTable, { ActionType } from "./components/ProTable";
const MyComponent = () => {
const ref = useRef<ActionType | null>(null);
return (
<ProTable
actionRef={ref}
columns={[
{
title: "序号",
dataIndex: "id",
key: "id",
width: 50,
search: false,
},
{
title: "标题",
dataIndex: "title",
key: "title",
ellipsis: true,
render: (text, row, index, action) => {
return <Button onClick={() => action.reload()}>{text}</Button>;
},
},
{
title: "类型",
dataIndex: "type",
key: "type",
filters: true,
onFilter: true,
valueEnum: {
all: { text: "全部" },
translation: { text: "翻译" },
original: { text: "原创" },
},
},
{
title: "创建时间",
dataIndex: "created_at",
key: "created_at",
valueType: "date",
sorter: true,
},
]}
request={async (params, sorter, filter) => {
// 构建 API URL
const url = `/api/data?page=${params.current}&size=${params.pageSize}`;
// 发起请求
const res = await fetch(url);
const json = await res.json();
return {
data: json.rows,
total: json.count,
success: true,
};
}}
rowKey="id"
bordered
pagination={{
showQuickJumper: true,
showSizeChanger: true,
}}
options={{
search: true,
}}
toolBarRender={() => [
<Button key="create" type="primary">
新建
</Button>,
]}
toolbar={{
menu: {
activeKey: "my",
items: [
{ key: "my", label: "我的" },
{ key: "all", label: "全部" },
],
onChange: (key) => {
console.log("切换到", key);
},
},
}}
/>
);
};
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| columns | 列配置 | ProColumns[] |
- |
| request | 数据请求函数 | (params, sorter, filter) => Promise<RequestData> |
- |
| actionRef | 表格操作引用 | React.MutableRefObject<ActionType> |
- |
| rowKey | 行唯一标识 | string \| (record) => string |
'id' |
| bordered | 是否显示边框 | boolean |
false |
| pagination | 分页配置 | false \| PaginationConfig |
- |
| search | 搜索配置 | false \| SearchConfig |
false |
| options | 选项配置 | OptionsConfig |
{} |
| toolBarRender | 工具栏渲染 | () => ReactNode[] |
- |
| toolbar | 工具栏配置 | ToolbarConfig |
- |
| headerTitle | 表格标题 | ReactNode |
- |
| params | 额外请求参数 | Record<string, any> |
- |
interface ActionType {
reload: (resetPageIndex?: boolean) => void; // 刷新表格
reset: () => void; // 重置表格
}
async (params, sorter, filter) => {
// params: { current: 1, pageSize: 20, keyword: '搜索词', ...自定义参数 }
// sorter: { field_name: 'ascend' | 'descend' }
// filter: { field_name: ['value1', 'value2'] }
return {
data: [], // 数据列表
total: 0, // 总数
success: true, // 是否成功
};
};
✅ 所有你代码中使用的功能都已实现 ✅ API 接口完全兼容 ✅ TypeScript 类型支持
这些简化不影响你当前代码的运行。
ProTable.tsx 到项目antd 4.24+reload(),使用防抖优化如需添加更多功能,可以在 ProTable.tsx 中扩展:
// 添加批量操作
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
<Table
rowSelection={{
selectedRowKeys,
onChange: setSelectedRowKeys,
}}
// ...
/>;
// 添加导出功能
const handleExport = () => {
// 导出逻辑
};
MIT