Answer the question
In order to leave comments, you need to log in
How to implement a filter in react-table?
react-table
Hello, please help me implement a filter of this type:
When you enter a letter, the filter returns all results where this letter is present.
But I need to make it so that when I enter a letter and press Enter, the filter method changes and returns only those values that contain only this letter.
Example:
Values (lines): test, rest, st
When I enter "st" - it will return all three lines to me, but when I press Enter - it will return only one line (st).
Right now I only have the first stage:
...
<ReactTable
columns={columns}
data={data}
filterable
defaultFilterMethod={(filter, row, column) => {
const id = filter.pivotId || filter.id;
return row[id] !== undefined
? String(row[id])
.toLowerCase()
.includes(filter.value.toLowerCase())
: true;
}}
/>
...
Answer the question
In order to leave comments, you need to log in
Make a custom filter with onChange and onKeyPress handlers, in which the filtering function will be switched. For example, like this (the lastName field, you can see it in action by driving in short words that are part of longer ones - such as war, son, comb, sign, child, work, can, etc.).
UPD. Taken from the comments:
Well, make a wrapper component for this table, which will set the default filter component and the default filter function - you can take them from my example.
UPD. https://codesandbox.io/s/nn1z53jn4l (only the first three fields, the last one has the filter overridden).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question