reactdatagrid
reactdatagrid copied to clipboard
Improve sorting when there are grouped columns
-
what edition are you using - enterprise (paying customer)
-
version for
@inovua/reactdatagrid-community
or@inovua/reactdatagrid-enterprise
- 4.0.16
What you did:
implemented a grid with column grouping, groupColumn={true}
and sorting:
Then I sorted by the "enlisted" column:
What happened:
As you can see above, russia should be first in order since the value for that row is 3, higher than two (usa).
Reproduction repository: https://codesandbox.io/s/laughing-euler-l8vhi?file=/src/App.js
Problem description: This looks like a bug. Our users are trying to sort columns when there are other columns grouped, and they don't understand why it doesn't work properly.
Suggested solution: in "group mode" (when there are other columns grouped) sorting a column should also sort the grid by group level. In my second screenshot for example, russia would appear above usa.
in code, I'd do something like this (following the example in the sandbox posted above):
const columns = [
{ ..., sortGroupBy: ({ data }) => data.array.reduce((acc, item) => acc + item, 0) }
]
data
would be the same as in columns.render
.
That's the simplest most intuitive API I could think of. I'm sure there are other ways to implement this, but ATM it looks and behaves like a bug, and it's a must for us and our clients. Thanks.
In your repro example, you used a custom renderer for the "enlisted" column (for grouped rows) - but sorting has no way of knowing what those numbers are - you are basically sorting on a boolean column.
Sorting, when you have grouping, basically sorts all records, and the does the grouping. So after the normal sorting is done, the first group that is found in the dataSource is displayed as the first group row, and so on. As a result, inside groups, rows are obviously sorted, and groups themselves are sorted by first occurence. If you want to change the sort order, you can specify a column.sort function to sort in the desired way.
In a future version, we can consider adding a new sorting strategy when there are groups - it can be a good idea, but we have to carefully consider all implications, since there are many things that can interact in this area (grouping, sorting, pivoting, reducers, etc)
PS: Instead of doing the group aggregation in the column.render function, you could use a group summary reducer - see https://reactdatagrid.io/docs/api-reference#props-columns-groupSummaryReducer
@inovua-admin will using group summary reducer affect the sorting by group level?
and if not, is there an estimation when sorting by group level would be implemented?
group summary reducer does not affect sorting. we don't have a timeline for this yet, as it's a new feature request - we're aggregating those and will prioritize after fixing our current reported bugs