例如创建示例User 接口和Group 接口
import * as React from "react";
import { Table, Input, Button, Space } from "antd";
import { ColumnsType } from "antd/es/table";
import { SearchOutlined } from "@ant-design/icons";
import "antd/dist/antd.css";
import "./styles.css";
interface User {
key: number;
name: string;
age: number;
address: string;
}
interface Group {
key: number;
some: string;
user: User;
}
const data: Group[] = [
{
key: 1,
some: "foo",
user: {
key: 9,
name: "John Brown",
age: 32,
address: "New York No. 1 Lake Park"
}
},
{
key: 2,
some: "bar",
user: {
key: 3,
name: "Joe Black",
age: 42,
address: "London No. 1 Lake Park"
}
},
{
key: 3,
some: "baz",
user: {
key: 10,
name: "Jim Green",
age: 32,
address: "Sidney No. 1 Lake Park"
}
},
{
key: 4,
some: "text",
user: {
key: 21,
name: "Jim Red",
age: 32,
address: "London No. 2 Lake Park"
}
}
];
interface AppState {
searchText: string;
searchedColumn: string;
}
interface AppProps {}
export default class App extends React.Component<AppProps, AppState> {
state = {
searchText: "",
searchedColumn: ""
};
searchInput: any;
getColumnSearchProps = (dataIndex: string) => ({
filterDropdown: ({
setSelectedKeys,
selectedKeys,
confirm,
clearFilters
}) => (
<div style={{ padding: 8 }}>
<Input
ref={(node) => {
this.searchInput = node;
}}
placeholder={`Search ${dataIndex}`}
value={selectedKeys[0]}
onChange={(e) =>
setSelectedKeys(e.target.value ? [e.target.value] : [])
}
onPressEnter={() =>
this.handleSearch(selectedKeys, confirm, dataIndex)
}
style={{ width: 188, marginBottom: 8, display: "block" }}
/>
<Space>
<Button
type="primary"
onClick={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
icon={<SearchOutlined />}
size="small"
style={{ width: 90 }}
>
Search
</Button>
<Button
onClick={() => this.handleReset(clearFilters)}
size="small"
style={{ width: 90 }}
>
Reset
</Button>
</Space>
</div>
),
filterIcon: (filtered: any) => (
<SearchOutlined style={{ color: filtered ? "#1890ff" : undefined }} />
),
onFilter: (value: any, record: any) =>
record[dataIndex]
? JSON.stringify(record[dataIndex])
.toLowerCase()
.includes(value.toLowerCase())
: "",
onFilterDropdownVisibleChange: (visible: boolean) => {
if (visible) {
setTimeout(() => this.searchInput.select(), 100);
}
},
render: (text: string, record: any, index: any) => {
return JSON.stringify(record[dataIndex]);
}
});
handleSearch = (selectedKeys: any, confirm: any, dataIndex: any) => {
confirm();
this.setState({
searchText: selectedKeys[0],
searchedColumn: dataIndex
});
};
handleReset = (clearFilters: any) => {
clearFilters();
this.setState({ searchText: "" });
};
render() {
const columns: ColumnsType<Group> = [
{
key: "some",
title: "Some",
dataIndex: "some",
...this.getColumnSearchProps("some")
},
{
key: "user",
title: "User",
dataIndex: "user",
...this.getColumnSearchProps("user")
}
];
return <Table<Group> columns={columns} dataSource={data} />;
}
}
在这里工作example