Skip to content
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 other sub-states to the initialState prop #3448

Closed
2 tasks done
cq-ubaid-khan opened this issue Dec 10, 2021 · 6 comments · Fixed by #3816
Closed
2 tasks done

[DataGrid] Add other sub-states to the initialState prop #3448

cq-ubaid-khan opened this issue Dec 10, 2021 · 6 comments · Fixed by #3816
Assignees
Labels
component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request

Comments

@cq-ubaid-khan
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

Currently the datagrid only support few properties as initial state one example pinnedColumns, We want it to receive columnsOrders, columnsSizes, pagination, columnVisibility as an initial state of grid.

Support for this configuration will be useful where we save all of the datagrid settings in persistent location and later when visited on the same page we get the same settings applied on the grid automatically.

Currently in order to achieve this behavior we have to interact with the apiRef.

Thanks.

Examples 🌈

const savedInitialState = {
   columnsSizes: { fieldName: { width: 30 }, otherField: { width: 50 } },
   columnsOrders: { fieldName: { orderIndex: 4 } },
   pagination: { size: 20, ... },
   columnsVisibility: { fieldName: { visible: false } },
   ...
}

<DataGrid 
   initialState={savedInitialState}
   ...
/>

Motivation 🔦

No response

@michaldudak michaldudak transferred this issue from mui/material-ui Dec 17, 2021
@alexfauquette
Copy link
Member

It sounds like it is a duplicate of #820

@flaviendelangle
Copy link
Member

flaviendelangle commented Dec 22, 2021

#820 is mainly about the new methods to imperatively store and restore the state.
But we can keep this one to discuss which state keys should be added to the initialState (most of the time I think those should also be added to the store / restore methods).

@cq-ubaid-khan as you saw we started small with some sub-states we knew were needed. The idea being to avoid adding to much complexity right from the start.

The pagination (pageSize and page) could very easily be added and for me it totally make sense.
For the columns related data it is slightly more complicated and I think we should do it in a second run. You can pre-process your column objects to provide custom initial order / sizes / visibility but we will have to think of a clean implementation on our side.


EDIT:

Here is the current status of the sub-states asked:

  • Column orders
  • Column sizes
  • Column Visibility
  • Pagination

@flaviendelangle flaviendelangle added component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request labels Dec 22, 2021
@cq-ubaid-khan
Copy link
Author

Hmm. Thanks for you response, I understand your thoughts about the complexity as I've gone through the same kind problem in the past.

However, I've implemented the solution from my own way by implementing a middle state mapping object which I can easily persist and reuse.

Pasting here so anyone in the future can get some help from this.

const layout = {
 layoutSettings: {
   field1: {
       orderIndex: 1,
       hidden: true,
       locked: false,
       ....
    }
   field2: ....,
   ....
 }
}

const layoutSettingFields = Object.keys(layout.layoutSettings);
if (layoutSettingFields.length) {
 layoutSettingFields.forEach((field) => {
   dtApi.setColumnIndex(field, layout.layoutSettings[field].orderIndex);
   dtApi.setColumnVisibility(field, !layout.layoutSettings[field].hidden);
   if (layout.layoutSettings[field].locked) dtApi.pinColumn(field, DataTablePinnedPosition.left);
   else dtApi.unpinColumn(field);
 });
}

@flaviendelangle flaviendelangle changed the title Provide more preferences as initialState to data grid [DataGrid] Add other sub-states to the initialState prop Jan 4, 2022
@tielushko
Copy link

@flaviendelangle, do you happen to know if there is a plan to work on additional states features, such as width and order? Is it something that serves as the priority for the team?

@flaviendelangle
Copy link
Member

It's under progress, see #3816
But the exact API is being discussed in the team so it will probably take a few weeks before the release.

@tielushko
Copy link

Thanks for linking that PR, @flaviendelangle! I will keep an eye on it, looking forward to seeing updates regarding it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants