Custom GroupBy in AgGridReact

Summary

The issue at hand involves implementing a custom GroupBy functionality in AgGridReact. The requirement is to group rows by the ‘pallet’ field if it has a value, and display rows with a null ‘pallet’ field as separate, independent rows.

Root Cause

The root cause of this issue lies in the default grouping behavior of AgGridReact, which does not distinguish between null and non-null values for the grouping field. To achieve the desired behavior, a custom grouping function is necessary.

Why This Happens in Real Systems

This issue occurs in real systems when dealing with datasets that contain a mix of grouped and standalone records. The default grouping behavior of most grid components, including AgGridReact, is not designed to handle such scenarios, leading to the need for custom implementations.

Real-World Impact

The inability to properly group and display records can lead to confusion and difficulties in data analysis. In real-world applications, this can result in incorrect insights and decisions being made based on the data.

Example or Code

const gridOptions = {
  columnDefs: [
    { field: 'pallet' },
    { field: 'lpn' },
    { field: 'city' },
    { field: 'country' }
  ],
  groupDisplayType: 'groupRows',
  autoGroupColumnDef: {
    field: 'pallet',
    cellRenderer: 'agGroupCellRenderer',
    cellRendererParams: {
      innerRenderer: (params) => {
        if (params.data.pallet === null) {
          return params.data.lpn;
        } else {
          return `Pallet ${params.data.pallet}`;
        }
      }
    }
  },
  rowData: [
    { pallet: 987, lpn: 1234, city: "London", country: "UK" },
    { pallet: 987, lpn: 5634, city: "London", country: "UK" },
    { pallet: null, lpn: 4933, city: "London", country: "UK" },
    { pallet: 757, lpn: 9264, city: "London", country: "UK" }
  ]
};

How Senior Engineers Fix It

Senior engineers fix this issue by implementing a custom grouping function that checks for null values in the ‘pallet’ field. If the value is null, the row is displayed as a separate, independent row. Otherwise, the row is grouped by the ‘pallet’ field.

Why Juniors Miss It

Junior engineers may miss this issue due to a lack of experience with custom grouping implementations in AgGridReact. They may not be aware of the need to handle null values separately or may not know how to implement a custom grouping function. Additionally, they may not fully understand the requirements of the problem, leading to an incorrect implementation.