table icon indicating copy to clipboard operation
table copied to clipboard

Filtering not working as expected for multiple columns with same accessor

Open dkravcovs opened this issue 9 years ago • 5 comments

What version of React-Table are you using?

6.6.0

What bug are you experiencing, or what feature are you proposing?

If displaying multiple columns with the same accessor setting filterable: false on one column disabled filtering on all of them, but the filter input is rendered. The use case here is having different renderers for the same data in the columns - e.g. one of the columns is a button that is bound to the value.

Sandbox

https://codesandbox.io/s/9z1x0wko6p

What are the steps to reproduce the issue?

  • Add two columns with same accessor
  • Set first one to filterable: false
  • Try to filter by second column

dkravcovs avatar Nov 16 '17 12:11 dkravcovs

Note that the workaround here is to turn:

{
  Header: "First Name",
  accessor: "firstName",
  filterable: false,
},
{
  Header: "First Name #2",
  accessor: "firstName"
},

into

{
  Header: "First Name",
  accessor: item => item.firstName,
  id: 'no-filter-column'
  filterable: false,
},
{
  Header: "First Name #2",
  accessor: "firstName"
},

dkravcovs avatar Nov 16 '17 12:11 dkravcovs

Given that filters work on the id of the column (which by default is the accessor if it is a string and no id is provided) and not specifically on a column position, then the behaviour is working as it is designed to work. Your "solution" is working because you have given the column a different id.

I think the use-case is an edge one and providing a different id accomplishes the goal so this is not considered a bug.

gary-menzel avatar Nov 16 '17 13:11 gary-menzel

Yep, I guessed that this was the case and I agree that this is a not a common scenario, but I still think there's an issue here. While the filtering behavior might be bound to a column id, the filtering display is not, i.e. while the other column becomes non-filterable it still renders the filter text input. To clarify - I would expect this to behave consistently. Either both behavior/display are determined the id, or by what's written in the column object.

dkravcovs avatar Nov 16 '17 13:11 dkravcovs

I've had a look at your codesandbox and the column that has filterable set to 'false' does not have the filter input box showing. So maybe I am misunderstanding what you mean.

The columns are still separate. But it is the id that is used to filter the data. Clearly, there'd be no point in creating the column at all if everything worked identically between the two. At some point there has to be rationale for picking how a column contributes to filtering (especially given the developer can inject filter criteria from outside ReactTable).

It might seem strange but I still don't believe there is any discrepancy here that needs any action taken.

gary-menzel avatar Nov 16 '17 13:11 gary-menzel

Let me put it a different way: If you want to use the same accessor on more than one column (which still seems an edge case just by itself), then the id needs to be set differently for everything to work consistently.

If you are talking about wanting to render data differently, then it is better to use the Cell prop. The accessor is used to get "raw" data that can be used for internal state use but the Cell can render any sort of text or component into what the user sees and has access to the whole row (not just the value returned by the accessor).

gary-menzel avatar Nov 16 '17 14:11 gary-menzel