JavaScript Data GridRow Grouping - Complex Objects
javascript logo
Enterprise

This section covers how to group rows when the row data contains complex objects.

Creating group keys from complex objects

When grouping by columns that contain complex objects in the supplied row data, the values will be converted to "[object object]" by default. This will not produce the desired grouping results.

One way to get around this is to add a toString() method to the complex objects, however this may not be possible if you are working with JSON data.

A more flexible solution is to use the colDef.keyCreator(params) callback function to return a meaningful key for the supplied object, as shown in the following code snippets:

// row item has complex object for country
rowItem = {
    athlete: 'Michael Phelps',
    country: {
      name: 'United States',
      code: 'US'
    },
    ...
}
const gridOptions = {
    columnDefs: [
      // the column definition for country uses a keyCreator
      {
          field: "country",
          keyCreator: params => params.value.name
      }
    ],

    // other grid options ...
}

Note in the snippet above that the colDef.keyCreator(params) returns the country name to be used as the group key from country complex object supplied in the row data.

The example below shows grouping on the country column that contains complex object values:

If using Cell Data Types, the key creator is automatically set to use the value formatter so it does not need to be specified directly.

Next Up

Continue to the next section to learn about Unbalanced Groups.