Skip to content

[data grid] rowCount set to -1 when fetching child rows in server-side Tree Data #17626

@mannam95

Description

@mannam95

Steps to reproduce

Description

After upgrading from DataGrid v7 to v8, we're experiencing an issue with server-side rendering in tree view mode. When expanding a parent row to fetch its children, DataGrid v8 incorrectly sets rowCount to -1 if the server response doesn't include a total row count.

In v7, the DataGrid correctly maintained the existing row count when fetching child rows. However, in v8, it seems to reset the row count to -1 when loading children, causing pagination issues.

Steps to Reproduce

  1. Set up a DataGrid with server-side pagination and tree data, the datsource would be something like below:
const dataSource = useMemo(
    () => ({
      getRows: async (params: GridGetRowsParams) => {
        try {
          const fetchedRows = await processDataService.fetchProcessNodes(params);
          return {
            rows: fetchedRows.rows,
            rowCount: fetchedRows.rowCount ?? 0,
          };
        } catch (error) {
          console.error('Error fetching rows:', error);
          return { rows: [], rowCount: 0 };
        }
      },
      getGroupKey: (row: GridValidRowModel) => String(row.id),
      getChildrenCount: (row: GridValidRowModel) => (row as Process).descendantCount || 0,
    }),
    [reloadTrigger],
  );
  1. Load initial data (e.g., 15 parent rows, which is less than our default page size of 25) with a total row count included in the response.
  2. Expand a parent row to fetch its children.
  3. In the server response for children, don't include a total row count (since we're only fetching children, not changing the total parent count).

Current behavior

  • When fetching children, DataGrid logs:
MUI X: useGridRowCount - Setting 'rowCount' to -1
  • This causes pagination issues as the grid thinks there are no rows.
  • gridPageCountSelector returns an incorrect page count (2 in our case - got this value from const pageCount = useGridSelector(apiRef, gridPageCountSelector);)

Expected behavior

  • DataGrid should maintain the previously known row count (15 in our case) when fetching children.
  • Only update row count when fetching top-level rows or when explicitly provided.

Context

We're using DataGrid with server-side pagination and tree data. Our goal is to hide pagination when the total number of rows fits within a single page (totalRows ≤ pageSize or pageCount ≤ 1).

This worked correctly in v7, but after upgrading to v8, we're experiencing an issue where expanding a parent row to fetch its children causes DataGrid to incorrectly set rowCount to -1, which then causes pagination to appear when it shouldn't.

Additional Context

In our logs, we can see:

  • When loading initial data:
MUI X: useGridRowCount - Setting 'rowCount' to 15
  • When fetching children (even though we do not explicitly return the rowCount):
MUI X: useGridRowCount - Setting 'rowCount' to -1

This appears to be a regression from v7, where the DataGrid correctly handled this scenario.

Workaround

Currently, we're forced to return the total row count with every request, including child row requests, even though this information is redundant for child fetching operations.

Your environment

"@mui/material": "7.0.2",
"@mui/x-data-grid-premium": "8.1.0",

Browser: tested in Chrome & Brave Browser

Search keywords: bug, regression, DataGrid, server-side pagination, tree data, rowCount, v8, Pagination, Custom pagintaion

Metadata

Metadata

Assignees

Labels

feature: Server integrationBetter integration with backends, e.g. data sourcefeature: Tree dataRelated to the data grid Tree data featurescope: data gridChanges related to the data grid.type: bugIt doesn't behave as expected.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions