You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Global filter doesn't run when accessor key points to an object #4673
If the accessor key points to an object in the data, then the global filter never gets invoked, even if I define a custom filter function.
This wasted a few hours of my time trying to track down why my global filter wasn't working. I find it very surprising that it would silently ignore my column.
Type things in the global filter, notice the rows are not filtered. Open the console, see that console.log() in fuzzyFilter is never called.
Expected behavior
If the accessor points to an object and there's a filter defined (either via globalFilterFn or a filterFn on the column), then it should still attempt to filter that column.
Or at the very least, there should be some kind of debug log if debug is turned on to explain why a column isn't getting filtered. There's really no observability to discover why the column is ignored.
The docs also make no mention that the column type would make a difference.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Any
react-table version
8.7.6
TypeScript version
4.9.4
Additional context
No response
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.
Reacted by Tanner Hoisington, Noam, Daniel Schlabach, Maurits Bos, David Gomez, Alexandre Daubricourt, Blitz2145, Slava and Sumanth Chinthagunta
@francislavoie I figured out why. It's very briefly mentioned in the column def guide that the accessor retrieves a primitive value, and that's the value used for filtering (and other functions). Once I created an accessor function that transforms the object to a string, global filtering works again.
For my use case, the only object being passed in is a dayjs object, so it's easy enough to transform it into a string (I'm doing that to display on-screen anyways). Your use case might be different.
Either way, I do think there should be better documentation about this potential issue, or some kind of error message when the accessor value is being accessed.
I used to be able to do a global search filter on an array column in v7.8.0, but now I cannot (v8.7.9).
The FilterFnOption type (for a filterFn of a ColumnDef) allows the string values: arrIncludes, arrIncludesAll, arrIncludesSome. Which implies that filtering on array-type column values would work, but it does not.
Also, in the globalFilterFn callback passed into useReactTable, the function never runs with the columnId of the array-type column, so I can't even manually write a filter function that can access the array-type value
Reacted by Elias Melgaço, Tanner Hoisington, Gaël Mayer, Noam, Daniel Schlabach, Nahoc, Murray Rowan, florianmartens and Sumanth Chinthagunta
This was driving me insane for awhile. My column value is an array and tried adding a filterFn function to my column def so I can hook in my filter logic for it. This was after trying to simply set it to arrIncludes since that implied it works on an array column. Nothing worked. After stumbling on this thread and flattening my array to a string it started searching the field but isn't a perfect search:
I used to be able to do a global search filter on an array column in v7.8.0, but now I cannot (v8.7.9).
The FilterFnOption type (for a filterFn of a ColumnDef) allows the string values: arrIncludes, arrIncludesAll, arrIncludesSome. Which implies that filtering on array-type column values would work, but it does not. Also, in the globalFilterFn callback passed into useReactTable, the function never runs with the columnId of the array-type column, so I can't even manually write a filter function that can access the array-type value
Yes exactly, the globalFilterFn never runs and no feedback is provided, so it's impossible to figure out this is the cause without finding this issue. No console logging from within, type warnings, debug settings on the hook, etc can help point the way.
My accessors were pointing to objects with the shape { value: any, error: boolean } so that the cell could access data via cell: (info) => info.getValue().value, and I could in the markup style the cells that have errors differently.
And just as a side note, as a newcomer to the library I have to say it seems very powerful but I am really struggling with the v8 documentation. v7/react-table seemed to have a bit more in terms of explanation and at least some of the examples were more focused on a specific topic rather than having 4 different table features on display at once.
Reacted by Moritz Jacobs, Guillaume, Daniel Schlabach, Evan Kysley and Oliver Faro
Or does the TanTable not support objects as values?
TanStack Table doesn't encourage accessors resolving to objects if you care about sorting filtering, or grouping. Those only work by default with resolved primitive values.
That's not to say you can't render complex values in a column though. You can still have multiple values in a custom cell render and access any data in the data row via row.original
@KevinVandy IMO it's fine if objects aren't considered, but I think it's wrong for the global filter to not work at all if any column is an object. I think it should either throw a error/warning, or skip that column and still filter the other non-object columns.
Any updates on this?
Or does the TanTable not support objects as values?
TanStack Table doesn't encourage accessors resolving to objects if you care about sorting filtering, or grouping. Those only work by default with resolved primitive values.
That's not to say you can't render complex values in a column though. You can still have multiple values in a custom cell render and access any data in the data row via row.original
When defining the columns (for simplicity the example only have one column):
typeTableData={name: Cell<string>;}constdata: TableData[]=[{name: {value: 'John',href: 'www.google.com',target: '_blank',tooltip: 'Person John'}},{name: {value: 'Doe',href: 'www.google.com',target: '_blank',tooltip: 'Person Doe'}}];constcolumns=[{accessorKey: 'name',// If I put 'name.value' here the global filter function is called, but I loose access to href, target and tooltipheader: 'Name',cell: CellRenderer,// this is a function that can handle rendering of links and tooltips}];
It would be nice if the globalFilterFn just received the TableData object in the value property and you could implement your own filter fn. Setting the accessor to an object would mean that you can not use the build in filters. This is fine as the build in filters are for primitive values.
Thanks for the discussion. The accessorFn did it for me.
I'm working with a project where we have data that the user can modify, including adding columns. We're using a solution to send the column definitions with the data from the server to the client, sometimes having to parse the data using a accessor-prop on a columdef field object. Looks something like this. Hope this helps someone some day.
# Data format defined in Python backend for a column header"last_used": {
"title": "Last used", # Visible on the column"accessor": "last_used", # Way to get the value"type": "dateFromUnix", # Used when determening how to render the value"display": True, # If it's supposed to be in the table in the first place. Sometimes tags are excluded because they're added differently"hidden": False, # Should the col be hidden by default"priority": 4, # Order of column"editable": False# Is the content editable, e.g. uuid's are not editable
},...]
// So in the columndef it works like this:Object.values(Fields).forEach((field: fieldObject)=>{if(field.hidden){return}// The basecolumns already have for example a checkbox-columnbaseColumns.push({id: field.accessor,
...field.fieldAccessor,header: ({ column })=>{return(<Buttonvariant="ghost"onClick={()=>column.toggleSorting(column.getIsSorted()==="asc")}>{field.title}<ArrowUpDownclassName="ml-2 h-4 w-4"/></Button>)},cell: ({ row })=>{returnparseDataFormat(field,row);// A custom function parsing the data according to it's type},enableGlobalFilter: true,accessorFn: (row)=>{returnrow[field.accessor]// THIS FIXED THE PROBLEM}}
the root of this lies in the getCanGlobalFilter function called when the column is created in the table. basically the default table options getColumnCanGlobalFilter() function checks if the column's resolved value is a string or a number. if it isn't (in the case of an object or an array) it returns false . the below logic built into the createColumn function then resolves to false because its a bunch of and statements strung together.
i think adding the feature that allows a user to explicitly set enableGlobalFilter: true in the column definition object should allow the user to override the default number/string check which would allow the column to leveraged in the global filter. i've forked and tried this locally and it seems to work, even with some of the baked-in global filter functions (i.e., includesString, equals)
@KevinVandy - i realize that the package discourages object/arrays in the table, but i think this offers a decent compromise, as the current workarounds to get a non-primitive column to be recognized by the global filter are arduous. My guess is that users aren't explicitly setting the enableGlobalFilter: true, (rather the use case is to set it to false to prevent it from being globally filtered), so this seems like a fairly safe way to enable the feature.
another way to implement the override is to alter the default getColumnCanGlobalFilter function to conditionally return values that are not strings or numbers, but i think the proposed solution is more straightforward and streamlined.
i think i can figure out how to open a pr request to submit the below change if its acceptable by maintainers
also, i should mention that i've tried this only with arrays of strings. i imagine you'd have to write a custom global filter function to appropriately access objects and their key/value pairs.
original code (table-core/src/features/GlobalFiltering.ts)
after digging into this further i've found a much easier shortcut to enable this without changing the source code. However, i still think there is merit in my suggested improvement to the source code. As a user, I would expect that if i set the enableGlobalFilter to true for a column, that the column should be used by the global filter. Having that explicit declaration in the definition overridden is unexpected behavior.
But for anyone who's looking for the quick workaround, you can modify the getColumnCanGlobalFilter option passed to the table creation like below. Make sure that any columns that you don't want to be used have their enableGlobalFilter key set to false for them to be excluded from the global filter. All other columns, regardless of what you put for the enableGlobalFilter value, will return true and be included in the global filter
Activity
anthonyma94 commentedon Feb 9, 2023
+1, trying to update to v8 and I run into this issue. Seems like a big oversight.
anthonyma94 commentedon Feb 10, 2023
@francislavoie I figured out why. It's very briefly mentioned in the column def guide that the accessor retrieves a primitive value, and that's the value used for filtering (and other functions). Once I created an accessor function that transforms the object to a string, global filtering works again.
For my use case, the only object being passed in is a dayjs object, so it's easy enough to transform it into a string (I'm doing that to display on-screen anyways). Your use case might be different.
Either way, I do think there should be better documentation about this potential issue, or some kind of error message when the accessor value is being accessed.
THowland1 commentedon Feb 27, 2023
+1 for this issue
I used to be able to do a global search filter on an array column in v7.8.0, but now I cannot (v8.7.9).
The
FilterFnOptiontype (for afilterFnof a ColumnDef) allows the string values:arrIncludes,arrIncludesAll,arrIncludesSome. Which implies that filtering on array-type column values would work, but it does not.Also, in the
globalFilterFncallback passed intouseReactTable, the function never runs with thecolumnIdof the array-type column, so I can't even manually write a filter function that can access the array-type valueeliasmelgaco commentedon Mar 28, 2023
+1
basememara commentedon Apr 2, 2023
This was driving me insane for awhile. My column value is an array and tried adding a
filterFnfunction to my column def so I can hook in my filter logic for it. This was after trying to simply set it toarrIncludessince that implied it works on an array column. Nothing worked. After stumbling on this thread and flattening my array to a string it started searching the field but isn't a perfect search:tannerbaum commentedon Apr 6, 2023
Yes exactly, the globalFilterFn never runs and no feedback is provided, so it's impossible to figure out this is the cause without finding this issue. No console logging from within, type warnings, debug settings on the hook, etc can help point the way.
My accessors were pointing to objects with the shape
{ value: any, error: boolean }so that the cell could access data viacell: (info) => info.getValue().value,and I could in the markup style the cells that have errors differently.And just as a side note, as a newcomer to the library I have to say it seems very powerful but I am really struggling with the v8 documentation. v7/react-table seemed to have a bit more in terms of explanation and at least some of the examples were more focused on a specific topic rather than having 4 different table features on display at once.
chiptus commentedon May 9, 2023
maybe the
AccessorFnshould document somehow that its return value should be primitiveFlemmingBehrend commentedon Jun 1, 2023
Any updates on this?
Or does the TanTable not support objects as values?
KevinVandy commentedon Jun 1, 2023
TanStack Table doesn't encourage accessors resolving to objects if you care about sorting filtering, or grouping. Those only work by default with resolved primitive values.
That's not to say you can't render complex values in a column though. You can still have multiple values in a custom cell render and access any data in the data row via
row.originalfrancislavoie commentedon Jun 1, 2023
@KevinVandy IMO it's fine if objects aren't considered, but I think it's wrong for the global filter to not work at all if any column is an object. I think it should either throw a error/warning, or skip that column and still filter the other non-object columns.
FlemmingBehrend commentedon Jun 2, 2023
Hi @KevinVandy
Is there another way to get cell specific "extra information" into the cell renderer?
Basically what I'm trying to do is the following:
The object structure I want to render have this type:
When defining the columns (for simplicity the example only have one column):
It would be nice if the globalFilterFn just received the TableData object in the value property and you could implement your own filter fn. Setting the accessor to an object would mean that you can not use the build in filters. This is fine as the build in filters are for primitive values.
Hephaestian commentedon Jan 8, 2024
Added some commentary to #4919 that is potentially relevant to this.
LiMao00 commentedon Jul 8, 2024
+1
Aavato-c commentedon Oct 23, 2024
Thanks for the discussion. The accessorFn did it for me.
I'm working with a project where we have data that the user can modify, including adding columns. We're using a solution to send the column definitions with the data from the server to the client, sometimes having to parse the data using a accessor-prop on a columdef field object. Looks something like this. Hope this helps someone some day.
btroop commentedon Mar 18, 2025
the root of this lies in the
getCanGlobalFilterfunction called when the column is created in the table. basically the default table optionsgetColumnCanGlobalFilter()function checks if the column's resolved value is astringor anumber. if it isn't (in the case of an object or an array) it returnsfalse. the below logic built into thecreateColumnfunction then resolves tofalsebecause its a bunch ofandstatements strung together.i think adding the feature that allows a user to explicitly set
enableGlobalFilter: truein the column definition object should allow the user to override the default number/string check which would allow the column to leveraged in the global filter. i've forked and tried this locally and it seems to work, even with some of the baked-in global filter functions (i.e.,includesString,equals)@KevinVandy - i realize that the package discourages object/arrays in the table, but i think this offers a decent compromise, as the current workarounds to get a non-primitive column to be recognized by the global filter are arduous. My guess is that users aren't explicitly setting the
enableGlobalFilter: true, (rather the use case is to set it tofalseto prevent it from being globally filtered), so this seems like a fairly safe way to enable the feature.another way to implement the override is to alter the default
getColumnCanGlobalFilterfunction to conditionally return values that are not strings or numbers, but i think the proposed solution is more straightforward and streamlined.i think i can figure out how to open a pr request to submit the below change if its acceptable by maintainers
also, i should mention that i've tried this only with arrays of strings. i imagine you'd have to write a custom global filter function to appropriately access objects and their key/value pairs.
original code (table-core/src/features/GlobalFiltering.ts)
suggested improvement
btroop commentedon Mar 21, 2025
after digging into this further i've found a much easier shortcut to enable this without changing the source code. However, i still think there is merit in my suggested improvement to the source code. As a user, I would expect that if i set the
enableGlobalFiltertotruefor a column, that the column should be used by the global filter. Having that explicit declaration in the definition overridden is unexpected behavior.But for anyone who's looking for the quick workaround, you can modify the
getColumnCanGlobalFilteroption passed to the table creation like below. Make sure that any columns that you don't want to be used have theirenableGlobalFilterkey set tofalsefor them to be excluded from the global filter. All other columns, regardless of what you put for theenableGlobalFiltervalue, will return true and be included in the global filterhere's the documentation: https://tanstack.com/table/latest/docs/api/features/global-filtering#getcolumncanglobalfilter