Skip to content

getCanGlobalFilter only checks against the values in the first row #4594

@vespasianvs

Description

@vespasianvs

Describe the bug

Bit of a fun one for me here. I was trying to global filter some data in a large table using just first and last name. As it happened, the first row in the data happened to have no value for first name or last name (they are undefined). All the other values in the data have a string value for the name.

Because getCanGlobalFilter only checks the first row of the data, the entire column was marked as not filterable.

Your minimal, reproducible example

https://codesandbox.io/s/cool-shape-gg15uo

Steps to reproduce

Use some data where the first row has undefined data and then try to global filter on those columns- the entire row isn't filterable because of the first row. See sandbox.

Expected behavior

For me it feels like there needs to be one of two things needs to happen here:

  1. Undefined is coped with and OK. It could have been that the first row was fine but the second row had the undefined values - could this then create an error later if we try to filter and the filterFn comes across an undefined value? It doesn't appear to: I have tried in the code sandbox below setting row 1 to be OK (which enables the filter) and then filtering, and everything seems fine. I guess it could cause problems though. Should we also cope with null?
  2. We check all of the data in the column. If there are data types that we don't like, do we really just want to rely on the first row to give us an overview of all of the data? Obviously, this comes with a cost for us to go through and check all of the data.

For now, I have done (what is possibly the correct thing anyway) and changed my accessorKey to an accessorFn and I check for undefined and return an empty string. If it is decided that this is actually the answer, then perhaps a docs thing explaining how getCanGlobalFilter works - I spent ages trying to workout why it returned false and the docs only talk about the options and columnDef.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Windows 11
Microsoft Edge Version 108.0.1462.42 (Official build) (64-bit)

react-table version

8.5.30

TypeScript version

4.8.4

Additional context

NOTE: In addition, the documentation for GlobalFilter is wrong, it currently reads:

The ability for a column to be globally filtered is determined by the following:

The column was defined a valid accessorKey/accessorFn.
If provided, options.getColumnCanGlobalFilter returns true for the given column. If it is not provided, the column is assumed > to be globally filterable.
column.enableColumnFilter is not set to false
options.enableColumnFilters is not set to false
options.enableFilters is not set to false

Obviously this is talking about enableColumnFilter etc (and in TS, options.getColumnCanGlobalFilter returns an error as it's not in the type definition for table options in react table.)

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions