-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[data grid] DataGrid initialState column order and width not working #11017
Comments
Please provide a minimal reproduction test case with the latest version. This would help a lot 👷 . |
It's not really easy to make the same example in codesandbox since it requires two api calls from an external server. I am really just trying to ask how this situation should be handled. If my initialState includes orderedFields and it has field1, field3, field2 for example: "orderedFields": [ But when the data comes in the data itself is ordered as "field1", "field2", "field3" should the DataGrid maintain the orderedFields value that was passed into the initialState or will it use the order of the data? My hope is it will maintain what was sent in the initialState but what I'm seeing is the initialState is updated to change the order of the fields to match the data. Can you provide any feedback on what the expected behavior is? I'm combed through your documentation and don't see a straight forward answer. Thanks |
It's difficult to follow the problem with just a written explanation, that's why I asked for a code reproduction. |
Ok I created a test project for you to look at: https://codesandbox.io/p/sandbox/data-grid-initialstate-bug-p8xfw2 You will notice with this project the initialState is loaded, putting the Name field on the right hand side: Durning loading the columns are in the right initial state, but after the 5 second timer and the data comes in the initialState is lost. How can I keep the initialState so that the data itself isn't change things like the order and width? |
Ok, I see what the problem is - if you move |
Thanks for the details. The problem is actually coming from the initialState. Does this need to be memorized as well? |
The problem in depth 🔍
I’m having an issue with the initialState and I’ve looked at your documentation and can’t seem to figure out what I’m doing wrong.
I have the initialState saving and loading on each page load. The initialStat and columns are passed in first to the table while the data loads. Once the data comes in the columns.orderedFields is overrided to the order the data itself comes in. I would think the state our set the column orders, not the data.
Here is what the DataGrid looks like:
<DataGridPremium
apiRef={apiRef}
rows={flattenedTableData}
columns={filteredTableSchema}
slots={slots}
onReady={saveSnapshot}
disableRowSelectionOnClick
pagination
autoHeight
density="compact"
pageSizeOptions={[5, 10, 25, 50, 100]}
initialState={{
...initialState,
pagination: { paginationModel: { pageSize: 5 } },
}}
/>
Here is the initialState object:
{
"rowGrouping": {
"model": []
},
"pinnedColumns": {},
"columns": {
"columnVisibilityModel": {},
"orderedFields": [
"name",
"email",
"is_active",
"is_superuser",
"phone_number",
"country",
"address",
"city",
"state",
"zip_code"
],
"dimensions": {
"name": {
"maxWidth": -1,
"minWidth": 50,
"width": 180,
"flex": 0
},
"email": {
"maxWidth": -1,
"minWidth": 50,
"width": 274,
"flex": 0
},
"is_active": {
"maxWidth": -1,
"minWidth": 50,
"width": 180
},
"is_superuser": {
"maxWidth": -1,
"minWidth": 50,
"width": 180
},
"phone_number": {
"maxWidth": -1,
"minWidth": 50,
"width": 180,
"flex": 0
},
"country": {
"maxWidth": -1,
"minWidth": 50,
"width": 180
},
"address": {
"maxWidth": -1,
"minWidth": 50,
"width": 180
},
"city": {
"maxWidth": -1,
"minWidth": 50,
"width": 180
},
"state": {
"maxWidth": -1,
"minWidth": 50,
"width": 180
},
"zip_code": {
"maxWidth": -1,
"minWidth": 50,
"width": 180
}
}
},
"preferencePanel": {
"open": false
},
"filter": {
"filterModel": {
"items": [
{
"field": "is_active",
"operator": "is",
"id": 22559,
"value": "true"
},
{
"field": "is_superuser",
"operator": "is",
"id": 46125,
"value": "true"
}
],
"logicOperator": "and",
"quickFilterValues": [],
"quickFilterLogicOperator": "and"
}
},
"sorting": {
"sortModel": []
},
"pagination": {
"paginationModel": {
"page": 0,
"pageSize": 5
}
}
}
When the table loads, before the data is there the columns are in their saved state, but as soon as the data loads the column ordering and width get reset.
Is there a way to hold the state and not have it overridden when the data is loaded?
This is all setup with apiRef.current.exportState(); and loaded and passed into the initialState on page load (object included above).
Thanks a lot, let me know if you can point me in the right direction.
Your environment 🌎
`npx @mui/envinfo`
Search keywords: DataGrid initialState column order
Order ID: 29521
The text was updated successfully, but these errors were encountered: