4

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.

Asela
  • 303
  • 1
  • 5
  • 16

2 Answers2

9

 const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable(
        {
            columns,
            data,
            initialState: {
                filters: [
                    {
                        id: 'leaveRequestStatus',
                        value: 'Pending',
                    },
                ],
            },
        },
        useFilters,

    );

https://react-table.tanstack.com/docs/api/useFilters

sridhar..
  • 1,945
  • 15
  • 19
0

Filter needs to be mentioned as each column level.and as table row header with it's function. Please see below code sanbox:

https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/filtering?file=/src/App.js:7168-7180

Asutosh
  • 1,775
  • 2
  • 15
  • 22
  • Thanks for looking into this. I have already set the column filters in const columns, Filter: SelectColumnFilter, filter: 'includes', and I want to filter only with one column 'Status' not the global filter. Thanks – Asela Aug 02 '20 at 04:11