-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[DataGrid] Prevent the grid from cleaning up the selectionModel
when selectionModel
prop is provided
#1782
Comments
Thanks for reporting this bug. We need to reapply the selection on page change. As a workaround, you can reset the selection model on page change. const handlePageChange = (params: GridPageChangeParams) => {
setSelectionModel([...selectionModel]);
setPage(params.page);
}; |
@dtassone Thanks! That works, much appreciated. |
It looks like it impacts the |
The state is updated even if the selection is controlled. Whenever the I think that a simple solution could be to not unselect rows when in controlled mode, but it seems like this is also an opportunity to better enforce the idea of using the prop (in controlled mode) as the source of truth, not the state. @oliviertassinari already did something like that in the pagination: https://github.com/mui-org/material-ui-x/blob/b7e84aaa031a355075eccc19749173a3583d16e3/packages/grid/_modules_/grid/hooks/features/pagination/useGridPagination.ts#L41-L46 |
@m4theushw I think that the behavior should always be identical if a state is controlled or not. Why? Because otherwise, it creates surprises and branch logic in the code. I'm not aware of any precedent in this direction in the components, so far. IMHO, it's a dead end.
Agree, there is a systematic issue here. It's like this for most of the states of the grid, not great. However, it's not specific to #1782, so I think that we can ignore it. @dtassone Is working on this problem in #1823, and not on solving #1782. @DanailH Looking at where #1864 was heading. It looks like we labeled this issue a "bug" but we didn't clarify what should be the correct behavior (what developed say is expected is only suppose to help us understand the root issue). From what I understand, there are two options for a sound solution:
I don't know which option would be better. @tforssander Do you have a preference?
@tforssander Actually, we have seen multiple related issues:
|
@dtassone When updating to alpha.36 your workaround no longer works, since the selectionModel is now always cleared. Not sure if this is intended due to other changes. @oliviertassinari So does that mean the fix is still under discussion until those related issues have been resolved? |
@tforssander Are you able to control the selection and ignore the update that are emptying the selection? I guess you are not able to differentiate between a user unselecting with the checkbox and changing page with the footer. |
@oliviertassinari I'm using a controlled selection, with paginationMode="server". What I'm currently trying to understand is why my Example: https://codesandbox.io/s/material-demo-forked-n3rem?file=/demo.tsx |
@tforssander Thanks for the reproduction. We miss a const handleSelectionModelChange = (
newSelectionModel: GridSelectionModel,
events,
details
) => {
if (details.reason === 'pageChange') {
return
}
setSelectionModel(newSelectionModel);
}; |
@oliviertassinari Ah, that alternative would be great! Is this still a suggestion or is it something that's currently being developed? |
@tforssander It's a suggestion, something that the community could be working on (It's part of the MIT component). |
@tforssander I investigated the problem but and tried to solve it with the current version. It can be done you can check a working demo here -> https://codesandbox.io/s/material-demo-forked-ok333?file=/demo.tsx To summarise - the Let me know if this solution will work for you. We can look into adding a |
We have a similar issue, not using server side pagination but using custom column filtering alongside controlled row selection. Our filter logic updates the rows data that is being passed to the datagrid, which results in the selected ids no longer being in the row data. When the filter is removed and the row data is present again the selection is missing in the same way as described for this bug. Therefore if its possible to have a solution that covers this too then that would be great. The workaround we have in place is to spread the ids we pass as the selectionModel prop in to a new array whenever the row data changes and this seems to trigger a re render but would be nice not to have to do this |
If adding a rerender is fixing it, I think this is due to this bug https://github.com/mui-org/material-ui-x/pull/2433/files#diff-87fa96c5d959b8acbc3c76f30e1750e8293f462996dd1a1e8cba8e72553d2f5aR157 Could you give me a reproduction example on Codesandbox so that I check if it works with my PR ? My PR does not solve the issue with |
A fix for the documentation has been added with an example of how to work around this issue for now. A more comprehensive solution will follow up as part of a separate PR. I'm reopening this issue so we can use it to track the new effort. I'll also rename the issue to be more relevant to what the problem actually is. |
selectionModel
when selectionModel
prop is provided
I am closing this |
Current Behavior 😯
When using
paginationMode="server"
together with a controlled checkbox selection, it does not retain the selection model when changing pages.https://codesandbox.io/s/material-demo-forked-k5bnx?file=/demo.tsx
Expected Behavior 🤔
It should retain selection model when changing pages, as it does with
paginationMode="client"
.https://codesandbox.io/s/material-demo-forked-s28wx?file=/demo.tsx
Steps to Reproduce 🕹
Steps:
selectionModel
state is still intactContext 🔦
I'm trying to implement server-side pagination and together with the controlled selection model, keep track of all selected row IDs.
Environement
v4.0.0-alpha.30
The text was updated successfully, but these errors were encountered: