-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sorting state behavior is changed if column data includes undefined values #4289
Comments
I had a similar problem. In my case, some data values I was sorting had null values which caused this problem. What I have done is, I iterated through all object values and changed null to an empty string (null -> '') If not for this issue I guess I would be going crazy... Thanks |
I have also experienced this problem, it isn't just limited to sorting either. If you enable global filtering, |
There seems to be something more weird going on with undefined values, they're not really sorted properly at all. See repro: https://codesandbox.io/s/exciting-shape-5xgj1q?file=/src/main.tsx:573-592 Or when the column has numbers, the numbers won't even be sorted. Those unfedined values will somehow split these numbers to several different groups and the rows are only sorted inside their own groups. Repro: https://codesandbox.io/s/quizzical-paper-jkeu92?file=/src/main.tsx E: Seems like that's intended behavior and you need to add |
I have the same problem here where I have values that are Isn't there a way to use |
I also just ran into this issue. However, in my case, the sorting order just goes from nothing to TLDR: Workaround at the bottom. After digging through the code and adding some breakpoints, I think I have figured out what the issue is. The code tries to determine the first sort direction automatically based on the value in the first row: getAutoSortDir: () => {
const firstRow = table.getFilteredRowModel().flatRows[0]
const value = firstRow?.getValue(column.id)
if (typeof value === 'string') {
return 'asc'
}
return 'desc'
}, In my case, on the data I'm testing with, getFirstSortDir: () => {
const sortDescFirst =
column.columnDef.sortDescFirst ??
table.options.sortDescFirst ??
column.getAutoSortDir() === 'desc'
return sortDescFirst ? 'desc' : 'asc'
}, which itself gets used in getNextSortingOrder: (multi?: boolean) => {
const firstSortDirection = column.getFirstSortDir()
const isSorted = column.getIsSorted()
if (!isSorted) {
return firstSortDirection
}
if (
isSorted !== firstSortDirection &&
(table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
(multi ? table.options.enableMultiRemove ?? true : true) // If multi, don't allow if enableMultiRemove))
) {
return false
}
return isSorted === 'desc' ? 'asc' : 'desc'
}, This means the second if statement here returns
I would make a pull request to fix this problem, but I feel I don't have the required context to understand why tanner decided to have the special string exception in Workaround Thankfully there is a solution, though. One way is to do something like |
In my project, instead of using
I got inspired by looking at how it handles it inside the code and simply just forced it on my end. It works for me at the oment for my cases. My thought was, if |
Thanks @saevarb Summed up nicely here.
Adding |
The most suitable approach I found for my server-side sorting table was to add it to the
|
This was un extremely unintuitive solution, but |
Describe the bug
In some cases, when an undefined cell value is present in a column, the sorting toggle for that column doesn't follow the
asc-desc-undefined
pattern; instead, it may goasc-undefined
,asc-desc
,asc-undefined-desc-undefined
, and a multitude of other variations.I'm not sure exactly what's causing the bug to appear, but I did manage to get a somewhat simple repro case. I think this issue occurs when using manual sorting (whether or not passing in the
manualSorting: true
flag), and when the first row has an undefined value for a column. Interestingly, the problem disappears if you setsortDescFirst: false
on the column with the undefined value.Your minimal, reproducible example
https://codesandbox.io/s/fragrant-framework-2noe5r?file=/src/main.tsx
Steps to reproduce
lastName
header, note that sort goes intodesc
modelastName
header again, note that sort goes into undefined sort modelastName
header once more, note that sort goes back intodesc
mode and skipsasc
Expected behavior
As a user, I expect that clicking on a sortable header would toggle between the
asc-desc-undefined
states (or reverse, ifsortDescFirst
).How often does this bug happen?
Every time
Screenshots or Videos
Platform
react-table version
v8.5.11
TypeScript version
v4.7.4
Additional context
No response
Terms & Code of Conduct
The text was updated successfully, but these errors were encountered: