[英]Ant Design table search customization
I'm using ant design table and I use getColumnSearchProps
for column-driven
search.我正在使用 ant 设计表,并使用getColumnSearchProps
进行column-driven
搜索。 I want to have the search input
in the header of the column.我想在列的标题中有search input
。 But I don't know how to handle search on this column and input?但是我不知道如何处理此列的搜索和输入?
data:数据:
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
},
{
key: '2',
name: 'Joe Black',
age: 42,
address: 'London No. 1 Lake Park',
}]
this is my columns:这是我的专栏:
const columns = [
{
title: (
<>
<Search
placeholder="search"
prefix={<CPIcon type="search" />}
onChange={??????????????}
/>
name
</>
),
dataIndex: 'name',
key: '1',
align: 'right',
render: text => (<h1>{text}</h1>)
},
other columns ...
]
and in render:并在渲染中:
<Table columns={columns} dataSource={data} />
Use Array.filter()
with String.includes()
使用Array.filter()
和String.includes()
Also, note that Input.Search
only adds two properties: onSearch
and enterButton
, so there is no point rendering it without using any additional props.另外,请注意Input.Search
只添加了两个属性: onSearch
和enterButton
,因此在不使用任何其他道具的情况下渲染它是没有意义的。
export default function App() {
const [dataSource, setDataSource] = useState(data);
const [value, setValue] = useState('');
const FilterByNameInput = (
<Input
placeholder="Search Name"
value={value}
onChange={e => {
const currValue = e.target.value;
setValue(currValue);
const filteredData = data.filter(entry =>
entry.name.includes(currValue)
);
setDataSource(filteredData);
}}
/>
);
const columns = [
{
title: FilterByNameInput,
dataIndex: 'name',
key: '1'
}
];
return (
<FlexBox>
<Table columns={columns} dataSource={dataSource} />
</FlexBox>
);
}
Does the other answer actually work?另一个答案真的有效吗? I actually used an additional useCallback
function:我实际上使用了一个额外的useCallback
函数:
export default () => {
const [data, setData] = useState(null)
const [val, setVal] = useState('')
const filterVals = useCallback((e) => {
const currValue = e.target.value
setVal(currValue)
const filteredVals = data.filter(entry =>
entry.val.includes(currValue)
)
setData(filteredVals)
}, [loading])
const columns = [
{
title:
<Input
placeholder="lalaland"
value={val}
onChange={filterVals}
/>,
dataIndex: 'val',
},
]
return (
<Table
bordered
dataSource={data}
columns={columns}
/>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.