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

[data grid] Can't group by nested value in fields #14520

Closed
maxd opened this issue Sep 6, 2024 · 8 comments · Fixed by #14599
Closed

[data grid] Can't group by nested value in fields #14520

maxd opened this issue Sep 6, 2024 · 8 comments · Fixed by #14599
Labels
component: data grid This is the name of the generic UI component, not the React module! feature: Row grouping Related to the data grid Row grouping feature plan: Premium Impact at least one Premium user

Comments

@maxd
Copy link

maxd commented Sep 6, 2024

Steps to reproduce

I want to group rows by the model column but DataGridPremium can't do this:

Screenshot 2024-09-06 at 15 39 22

As you can see the model column is empty.

MVP for reproducing this problem: link. The MVP is based on the first example from this page.

This happens because DataGridPremium can't extract values from fields in nested objects (e.g. from device.model and device.serial_number fields). Try to uncomment the model and serial_number fields in the rows array. When you make this you'll receive the error: row.device is undefined. If you fix this (e.g. replace row.device.??? with row.device?.xxx in valueGetter methods) the grouping will work fine:

Screenshot 2024-09-06 at 15 47 12

Current behavior

The DataGridPremium can't group by field in nested objects i.e. it can group by model field in such {model: "X"} row but can't group by the same field in {device: {model: 'x'}} row when valueGetter extract this value.

Expected behavior

The DataGridPremium should group by field in nested objects extracted with valueGetter method from column definition.

Context

No response

Your environment

No response

Search keywords: grouping by nested fields

@maxd maxd added bug 🐛 Something doesn't work status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Sep 6, 2024
@github-actions github-actions bot added the component: data grid This is the name of the generic UI component, not the React module! label Sep 6, 2024
@michelengelen
Copy link
Member

@MBilalShafi could you have a look at this one? Thanks!

@michelengelen michelengelen changed the title DataGridPremium can't group by nested fields [data grid] Can't group by nested value in fields Sep 6, 2024
@michelengelen michelengelen added plan: Premium Impact at least one Premium user feature: Row grouping Related to the data grid Row grouping feature labels Sep 6, 2024
@maxd
Copy link
Author

maxd commented Sep 6, 2024

I have found the solution to this problem. The groupingValueGetter method should be defined for extract nested fields:

...
groupingValueGetter: (_value, row) => row.device.model,
...

It's confusing. Is it possible to use the valueGetter method to extract nested fields if the groupingValueGetter method is undefined?

@michelengelen
Copy link
Member

I have found the solution to this problem. The groupingValueGetter method should be defined for extract nested fields:

...
groupingValueGetter: (_value, row) => row.device.model,
...

It's confusing. Is it possible to use the valueGetter method to extract nested fields if the groupingValueGetter method is undefined?

No, that's not possible. As stated in the docs the groupingValueGetter is specifically used for grouping. It can be a different value as the one received from the valueGetter.

Does using that solve your use-case?

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer bug 🐛 Something doesn't work labels Sep 9, 2024
@flaviendelangle
Copy link
Member

I went through all the discussions on the PR where I created the row grouping to find a reason not to fallback to valueGetter when groupingValueGetter is not defined, and I could not find any.

Not saying that we should change our behavior, just that remember a smart reason not to when we created this feature, it was just not discussed 👍

@maxd
Copy link
Author

maxd commented Sep 10, 2024

Does using that solve your use-case?

@michelengelen Yes, this solved the problem but I suppose it'll be better to mention in the documentation that the row grouping functionality uses the groupingValueGetter method instead of the valueGetter one to extract the value used for grouping i.e. both these methods should be defined if you are extracting values from nested fields.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Sep 10, 2024
@flaviendelangle
Copy link
Member

but I suppose it'll be better to mention in the documentation that the row grouping functionality uses the groupingValueGetter

It's documented here
Maybe the discoverability of this section is not good enough

@michelengelen
Copy link
Member

We could possibly add a callout and a crosslink here: Value getter

@michelengelen michelengelen removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 12, 2024
@github-project-automation github-project-automation bot moved this to 🆕 Needs refinement in MUI X Data Grid Sep 12, 2024
@michelengelen michelengelen moved this from 🆕 Needs refinement to 🏗 In progress in MUI X Data Grid Sep 12, 2024
@michelengelen michelengelen moved this from 🏗 In progress to 👀 In review in MUI X Data Grid Sep 12, 2024
Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

We value your feedback @maxd! How was your experience with our support team?
If you could spare a moment, we'd love to hear your thoughts in this brief Support Satisfaction survey. Your insights help us improve!

@DanailH DanailH moved this from 👀 In review to ✅ Done in MUI X Data Grid Nov 1, 2024
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! feature: Row grouping Related to the data grid Row grouping feature plan: Premium Impact at least one Premium user
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants