-
-
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] Add methods to save and restore the state #820
Comments
Hi @dheeraj1447, quick question. Did you try doing There is a list of events that are being fired when columns are reordered, you can check them HERE |
Yes @DanailH I tried that.. It would be lot better If that can be exposed on the table itself. Because |
Yes @dheeraj1447 Maybe I'm not understanding the problem correctly, can you help me understand what do you call a table, is it the rows of the grid? Or maybe you are reordering columns using the API, in which case these events won't be fired? |
Since For example,
@DanailH hope that gives you a clear picture on what I'm suggesting for. Thanks! |
@dheeraj1447 I don't think that we need to solve the reorder event problem in isolation. What we need to solve is how to store the state of the grid between two sessions. Handling each state manually won't scale well (order of the columns, size of the column, sorting applied, density applied, etc). Is this correct? We have started to work on it in #533, however, we aren't here yet. I'm at least aware of the problem problems to solve:
Once you have this callback, how do you intent to feed the data back into the state in the next session? I think that the API could look like: <DataGrid
defaultState={defaultState}
onStateChange={(state) => {
saveState({
filterModel: state.filterModel,
sortingModel: state.sortingModel,
hiddenColumn: state.hiddenColumn,
// Ignore everything else
})
}}
/> |
Perhaps this is a dumb idea, but my expectation was that I could use a useState variable to hold my columns, and that any change within the X-Grid would get reflected back out to my variable and I could listen to changes on it. I'm pretty sure I've seen and used this behavior on other components like Autocomplete and I've found it very useful. e.g.
|
Has more work been done to make the state portable? Additionally, it would be cool if we could subscribe to a "central state" object but only certain properties. I'd like to store filters, sorting, and column order, but not selection. I'll probably end up subscribing to the events I want then patching it all together with something like https://material-ui-x.netlify.app/storybook/?path=/story/x-grid-tests-state--partial-control-use-state |
Prerequisite Besides the problems raised in 1 from #820 (comment), I think we have a couple more keys in the state that need to be reworked to make the state portable:
And repeating the items from #820 (comment):
|
We currently have a prop If it's to allow saving / restoring the state in a DB / local storage, then I think it should be renamed For the work for portability Starting by removing the keys that are only used locally like I'm not sure removing the elements like More broadly, we need to rework the state coming months, this is the goal of #2231 I think at this point, any major state change should first have some proof of concept to discuss the tradeoffs done. |
From what I recall, Damien added the
It's one option, I think that we mentioned it in the past. I personally feel that we could already try to add a demo for this problem in the documentation, something developers can use in production. We could have:
Then, progressively rework the state for covering more cases, and making the restore DB demo more useful. |
I think we can pause the effort on cleaning the state #820 (comment) and proceed with adding API methods to allow to save and restore parts of the state. Looking at what is stored and what users seems to ask most, I think we should expose the following items: interface GridInitialState { // Rename/alias to GridSavedState?
pagination: GridPaginationState;
columns: GridColumnsInitialState;
selection: GridSelectionModel;
sorting?: GridSortingInitialState;
filter?: GridFilterInitialState;
preferencePanel?: GridPreferencePanelInitialState;
density: GridDensityInitialState;
}
interface GridColumnsInitialState {
sizes: { [field: string]: number; }; // The user can choose to restore only sizes or column order
order: string[]
}
interface GridDensityInitialState {
value: GridDensity; // "standard" | "confortable"
} The As for the API methods, it could be: apiRef.current.exportState(): GridInitialState apiRef.current.restoreState(state: GridInitialState): void I don't know if we should support restoring the session in the |
It does not have to be. interface GridInitialState {
...
columns: {
dimensions: { field: string, computedWidth: number }[]
}
} The format above is an example, it could be any other format for I built the
I agree that the main concern right now is the control models. |
We can keep the top level keys. I updated #820 (comment) to add them.
Probably For the controllable model, if there's no callback we don't have to do anything. The user must add the |
I'm reopening, the docs still link this issue https://mui.com/components/data-grid/state/#save-and-restore-the-state From what I understand in #3593, we did an iteration with private APIs: #3593 (review) |
Closed by #4028 |
The documentation seems to still not be available? https://mui.com/components/data-grid/state/#save-and-restore-the-state I also happened to notice there was a release of 5.8 on npm, but no release notes on GitHub. |
The doc of v5.8 will come soon, we have a few problems releasing it. |
Currently, no column re-order events are exposed on the tables both (DataGrid & XGrid). Need this feature as we can handle custom events while the column reordering happens.
Summary 💡
When column A is dragged to column B. I should be able to track it.
Examples 🌈
ColumnReorderApi
present as part of the table but need to expose those as event handlers.Motivation 🔦
Benchmark
The text was updated successfully, but these errors were encountered: