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] Group Item With Checkbox Select Children #13277

Closed
KSafadi opened this issue May 28, 2024 · 11 comments
Closed

[data grid] Group Item With Checkbox Select Children #13277

KSafadi opened this issue May 28, 2024 · 11 comments
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 feature: Selection Related to the data grid Selection feature support: commercial Support request from paid users support: premium standard Support request from a Premium standard plan user. https://mui.com/legal/technical-support-sla/

Comments

@KSafadi
Copy link

KSafadi commented May 28, 2024

The problem in depth

I have a grouping in my MUI DataGrid and a checkbox selection. When the checkbox of the group headers are clicked, nothing happens. I need all the children to be selected when the group is selected, similar to the header checkbox selection.
I have provided a demo: https://stackblitz.com/edit/react-mqcgmm?file=Demo.tsx

Your environment

`npx @mui/envinfo`
System:
    OS: Windows 11 10.0.22631
  Binaries:
    Node: 18.17.1 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.0.5 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Chrome: Not Found
    Edge: Chromium (123.0.2420.97)
  npmPackages:
    @emotion/react: 11.11.3 => 11.11.3
    @emotion/styled: 11.11.0 => 11.11.0
    @mui/base:  5.0.0-beta.37
    @mui/core-downloads-tracker:  5.15.11
    @mui/lab: 5.0.0-alpha.166 => 5.0.0-alpha.166
    @mui/material: 5.15.11 => 5.15.11
    @mui/private-theming:  5.15.14
    @mui/styled-engine:  5.15.14
    @mui/system: 5.15.11 => 5.15.11
    @mui/types:  7.2.14
    @mui/utils: 5.15.11 => 5.15.11
    @mui/x-data-grid:  7.4.0
    @mui/x-data-grid-premium: 7.4.0 => 7.4.0
    @mui/x-data-grid-pro:  7.4.0
    @mui/x-date-pickers: 6.19.5 => 6.19.5
    @mui/x-license: ^7.2.0 => 7.2.0
    @types/react: 18.2.58 => 18.2.58
    react: 18.2.0 => 18.2.0
    react-dom: 18.2.0 => 18.2.0
    typescript: 5.3.3 => 5.3.3

Search keywords: DataGrid Grouping CheckBox Selection
Order ID: 89911

@KSafadi KSafadi added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users labels May 28, 2024
@zannager zannager added component: data grid This is the name of the generic UI component, not the React module! support: premium standard Support request from a Premium standard plan user. https://mui.com/legal/technical-support-sla/ labels May 28, 2024
@michelengelen
Copy link
Member

@cherniavskii we had a similar issue already, but I cannot find it ... 🤔
can you remember where that was?

@michelengelen michelengelen added status: waiting for author Issue with insufficient information status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer status: waiting for author Issue with insufficient information status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it labels May 29, 2024
@michelengelen
Copy link
Member

@cherniavskii nvm ... found it.

@KSafadi we currently do not support this behavior OOTB, but we have a working example written up here: #13036

This basically does exactly what you want.

Let me know if using this fixes your use case!

@michelengelen michelengelen added status: waiting for author Issue with insufficient information feature: Selection Related to the data grid Selection feature feature: Row grouping Related to the data grid Row grouping feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 29, 2024
@tsafadi
Copy link

tsafadi commented May 29, 2024

Could you show how to integrate this into the sample that was provided?
I am not sure how to integrate the checkboxColumn and also I am not able to get the imports correctly.

@KSafadi
Copy link
Author

KSafadi commented May 31, 2024

I have the same problem as @tsafadi. Could you please add the checkboxColumn to the sample provided. Thank you very much.

@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 May 31, 2024
@michelengelen
Copy link
Member

@KSafadi @tsafadi here is the updated example

@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 labels Jun 3, 2024
@michelengelen
Copy link
Member

@KSafadi did this solution work for you?

@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 Jun 7, 2024
@KSafadi
Copy link
Author

KSafadi commented Jun 7, 2024

Yes, thank you so much! Sorry for the delay

@KSafadi KSafadi closed this as completed Jun 7, 2024
@github-actions github-actions bot removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jun 7, 2024
Copy link

github-actions bot commented Jun 7, 2024

⚠️ 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.

@KSafadi: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

@santhoshsis
Copy link

@michelengelen The header checkbox is not working as expected like when we click on it it is selecting everything but when selecting it again was not deselecting all. Also when we remove some selections from the rows it should go to indeterminate state but nothing is happening! I tried to debug and but nothing worked!

@tsafadi
Copy link

tsafadi commented Sep 23, 2024

@KSafadi @tsafadi here is the updated example

The solution that you provided is perfect. However, the select all from the header does not work anymore. Could you show how to fix that? Or is there an option to disable or remove the checkbox from the header?

@Hiteshshingala
Copy link

🎉 Checkbox Selection Enhancement for MUI DataGrid

Great news, @michelengelen @tsafadi! I've addressed the issue you reported with the header checkbox functionality. This solution improves the user experience and brings the checkbox behavior in line with expected standards.

🔧 Key Improvements:

  1. Header Checkbox Consistency: Now correctly toggles between select all and deselect all states.
  2. Indeterminate State: Properly reflects partial selections in both header and group row checkboxes.
  3. Group Row Handling: Enhanced to manage selections within grouped data accurately.

💡 Implementation Highlights:

  • Utilizes gridFilteredSortedRowIdsSelector for precise row management.
  • Implements custom renderHeader and renderCell functions for granular control.
  • Leverages useGridSelector with gridRowSelectionStateSelector for efficient state management.

🚀 Benefits:

  • Improved data interaction and visualization.
  • Enhanced user control over selections.
  • Consistent behavior across different data structures (flat and grouped).

#MUI #OpenSource #DataGrid #UserExperience

const checkboxColumn = (apiRef: MutableRefObject<GridApiPro>): GridColDef => {
    return {
      ...GRID_CHECKBOX_SELECTION_COL_DEF,
      renderHeader: function RenderHeader(params) {
        const children = gridFilteredSortedRowIdsSelector(
          apiRef.current.state,
          apiRef.current.instanceId
        ).filter((id) => !id.toString().includes("auto-generated"));

        const selectionLookup = useGridSelector(
          apiRef,
          gridRowSelectionStateSelector
        );

        const indeterminate =
          children?.some((child) => selectionLookup.includes(child)) &&
          children?.some((child) => !selectionLookup.includes(child));

        const checked = children?.every((child) =>
          selectionLookup.includes(child)
        );
        const data: GridColumnHeaderParams & {
          indeterminate?: boolean;
          checked?: boolean;
          disabled?: boolean;
          onClick?: (e: MouseEvent) => void;
        } = {
          ...params,
          onClick: (e) => {
            apiRef.current.selectRows(children, indeterminate || !checked);
            e.preventDefault();
          },
          indeterminate,
          checked,
        };
        return (
          <>
            <GridHeaderCheckbox {...data} />
          </>
        );
      },
      renderCell: function RenderCell(params) {
        const { rowNode } = params;

        const selectionLookup = useGridSelector(
          apiRef,
          gridRowSelectionStateSelector
        );

        if (rowNode.type !== "group") {
          return <GridCellCheckboxRenderer {...params} />;
        }

        const children = apiRef.current.getRowGroupChildren({
          groupId: rowNode.id,
          applyFiltering: true,
          applySorting: true,
        });

        const indeterminate =
          children?.some((child) => selectionLookup.includes(child)) &&
          children?.some((child) => !selectionLookup.includes(child));

        const checked = children?.every((child) =>
          selectionLookup.includes(child)
        );

        const extraData: GridRenderCellParams & {
          indeterminate?: boolean;
          checked?: boolean;
          disabled?: boolean;
          onClick?: (e: MouseEvent) => void;
        } = {
          ...params,
          disabled: false,
          onClick: (e) => {
            if (rowNode.type === "group") {
              if (children) {
                apiRef.current.selectRows(
                  [rowNode.id, ...children],
                  indeterminate || !checked
                );
              }
              e.preventDefault();
            }
          },
          indeterminate,
          checked,
        };

        return <GridCellCheckboxRenderer {...extraData} />;
      },
    };
  };

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 feature: Selection Related to the data grid Selection feature support: commercial Support request from paid users support: premium standard Support request from a Premium standard plan user. https://mui.com/legal/technical-support-sla/
Projects
None yet
Development

No branches or pull requests

6 participants