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 valueSetter #2876

Merged
merged 3 commits into from
Oct 22, 2021
Merged

[DataGrid] Add valueSetter #2876

merged 3 commits into from
Oct 22, 2021

Conversation

m4theushw
Copy link
Member

@m4theushw m4theushw commented Oct 15, 2021

@@ -189,6 +195,12 @@ Here is shown how a full-featured CRUD can be created.

{{"demo": "pages/components/data-grid/editing/FullFeaturedCrudGrid.js", "bg": "inline", "disableAd": true}}

### Saving rows with nested structures
Copy link
Member

@DanailH DanailH Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need that entry here when we already have the Saving nested structures? It seems that this only points to the one before it.

Besides that, I think it looks great!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern we have been doing is to have one section for cell editing and one for row editing. However, this still not scaling because many features are the same. I think we should rework this page in the future to not duplicate content .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so for now we can keep it as it is. I raised it because it seemed like a duplication. if this was on a different page then it would have made more sence.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone needs, I used this patch...
1- Creating Nested Field Access Functions

To address the need for nested fields in DataGrid, two useful functions have been created:
(for example)

function getSubField(params) {
  // The getSubField function extracts the value of a nested field
  const [fieldName, subFieldName] = params?.field?.toString().split('.');
  const field =  params?.row[fieldName];
  return field ? field[subFieldName] : null;
}

function setSubField(subFieldName) {
  // The setSubField function updates a nested field with a new value
  return (params) => {
    const field = { ...params.row.patrimony };
    field[subFieldName] = params.value;
    return { ...params.row, patrimony: field };
  };
}

These functions make it easy to retrieve and update values in nested fields.

2 - Configuring Columns in DataGrid

Next, to set up the DataGrid columns with nested fields, you make use of these functions:
(for example)

{ field: 'patrimony.employment', headerName: 'Employment', width: 180, editable: true,
  valueGetter: getSubField, valueSetter: setSubField('employment'),},
{ field: 'patrimony.salary', headerName: 'Salary', width: 100, editable: true, type: 'number',
  // Other columns with nested fields
}

Here, the valueGetter and valueSetter functions are set for each column, allowing DataGrid to effectively access and update nested fields.

3- Updating Values After Editing (Rendering)

Finally, to ensure that the values of nested fields are correctly set after data editing or updates, you can use a loop:
(for example)

Object.keys(clientRendered.patrimony).forEach(subField => {
  clientRendered[`patrimony.${subField}`] = clientRendered.patrimony[subField];
});

This loop ensures that the values of nested fields are properly reflected in the properties of the clientRendered object with the "patrimony." prefix.

@m4theushw m4theushw self-assigned this Oct 20, 2021
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 20, 2021
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 21, 2021
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 22, 2021
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 22, 2021
@m4theushw m4theushw merged commit 21a6f70 into mui:next Oct 22, 2021
@m4theushw m4theushw deleted the value-setter branch October 22, 2021 12:46
@oliviertassinari oliviertassinari added the component: data grid This is the name of the generic UI component, not the React module! label Jan 17, 2023
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!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Edit cell with nested data structure
4 participants