Reputation: 323
I am using react-table 7.0.4 (https://www.npmjs.com/package/react-table). I want to apply a default filter for the column 'Status' ('leaveRequestStatus') when the table loads for the first time (filter is a dropdown). I tried with 'defaultFiltered' like this but doesn't work, neither the 'Pending' option is selected in the dropdown nor the data is filtered,
const columns = React.useMemo(() => [
{
Header: 'Status',
accessor: 'leaveRequestStatus',
id: 'leaveRequestStatus',
Filter: SelectColumnFilter,
filter: 'includes',
},
....
<Table columns={columns} data={data} defaultFiltered={[{id:'leaveRequestStatus', value:'Pending'}]} />
Is there another way to do this? Thanks.
Upvotes: 4
Views: 9523
Reputation: 2133
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable(
{
columns,
data,
initialState: {
filters: [
{
id: 'leaveRequestStatus',
value: 'Pending',
},
],
},
},
useFilters,
);
https://react-table.tanstack.com/docs/api/useFilters
Upvotes: 10
Reputation: 1828
Filter needs to be mentioned as each column level.and as table row header with it's function. Please see below code sanbox:
Upvotes: 0