▦K2 Advanced GridsDeveloper Portal / 0.24
Documentation / Data presentation

Row grouping

Organize purchase orders into expandable groups, add nested levels and display group-level totals without replacing the underlying K2 List View.

Before you begin

Use the 17-column Purchase Orders List and the Data Visualization configuration from Lesson 9 — Data Bars & Color Scales. The downloadable configurations retain filtering, Summary, Boolean icons and Data Visualization, use 25 records per page, and disable cell merging and browser preferences for repeatable results.

Download single grouping configuration ↓ Download nested grouping configuration ↓
Required after saving a configuration file

Replace REPLACE_WITH_YOUR_AREAITEMID with your own ID (see Grid Discovery). Save the chosen download as purchase-orders.config.js in the server config folder, increment configVersion in the Data Label (for example, from 0.2.0 to 0.2.1), and reload the form. The engine is unchanged.

1. Single-level grouping

Start from the ungrouped list. Group by Department to collect records into expandable sections with a count and two aggregate values.

Before grouping
Figure 090 · Before grouping · Actual K2 Runtime capture
K2AdvancedGridsConfig.defaults.cellMerging.enabled = false;
K2AdvancedGridsConfig.defaults.rowGrouping = {
  "enabled": true,
  "columns": [
    "Department"
  ],
  "allowUserGrouping": true,
  "collapsedByDefault": false,
  "showGroupingBar": true,
  "showRecordCount": true,
  "showExpandCollapseAll": true,
  "aggregates": [
    {
      "column": "Order Amount",
      "operation": "sum",
      "label": "Total order value",
      "precision": 2
    },
    {
      "column": "Budget Variance",
      "operation": "sum",
      "label": "Budget variance",
      "precision": 2
    }
  ]
};
K2AdvancedGrids.instance.refresh();
Single-level grouping by Department
Figure 091 · Single-level grouping by Department · Actual K2 Runtime capture

2. Expand and collapse groups

Click the chevron in a group header to toggle its records. Use Collapse all or Expand all in the grouping bar to change all group states. Collapsing is a presentation operation, not a filter: it does not change matching record counts.

IT collapsed while Procurement remains expanded
Figure 092 · IT collapsed while Procurement remains expanded · Actual K2 Runtime capture
All groups on the current page collapsed
Figure 093 · All groups on the current page collapsed · Actual K2 Runtime capture
Groups expanded again
Figure 094 · Groups expanded again · Actual K2 Runtime capture

3. Grouping with filtering

Apply Order Amount greater than 100000, Delivery Progress greater than 0 and Is Urgent = Yes. The example returns 35 matching orders. The Summary and group aggregates reflect the matching records, while Data Visualization remains active.

Grouping with three active filters
Figure 095 · Grouping with three active filters · Actual K2 Runtime capture

4. Multi-level grouping

Clear filters before the next example. Add Status after Department to create nested groups. The order of columns in the configuration defines the hierarchy.

Download nested grouping configuration ↓
K2AdvancedGridsConfig.defaults.cellMerging.enabled = false;
K2AdvancedGridsConfig.defaults.rowGrouping = {
  "enabled": true,
  "columns": [
    "Department",
    "Status"
  ],
  "allowUserGrouping": true,
  "collapsedByDefault": false,
  "showGroupingBar": true,
  "showRecordCount": true,
  "showExpandCollapseAll": true,
  "aggregates": [
    {
      "column": "Order Amount",
      "operation": "sum",
      "label": "Total order value",
      "precision": 2
    },
    {
      "column": "Budget Variance",
      "operation": "sum",
      "label": "Budget variance",
      "precision": 2
    }
  ]
};
K2AdvancedGrids.instance.refresh();
Department followed by Status
Figure 096 · Department followed by Status · Actual K2 Runtime capture

5. Group-level aggregates

Each group header displays its record count, sum of Order Amount and sum of Budget Variance. Parent totals cover matching records within the parent, and child totals cover matching records within the child. Procurement contains 32 matching records with a total order value of 3,844,000.14; its Pending Approval subgroup contains seven records with a total value of 949,143.03.

Parent and child aggregate values
Figure 097 · Parent and child aggregate values · Actual K2 Runtime capture

6. Manage grouping interactively

Right-click a column header to open Manage columns. Use Group by this column to replace the grouping, Add to grouping to append a level, Remove from grouping to remove a level, or Clear all grouping to restore the ungrouped presentation. You can also remove a grouping level using its chip in the grouping bar.

Header context menu with grouping commands
Figure 098 · Header context menu with grouping commands · Actual K2 Runtime capture

7. Grouping and pagination

Pagination counts records rather than groups. A group can span several pages, with its header repeated on each relevant page. On page 2, Procurement can show only part of its 32 records while the header retains the full group count and aggregates. Collapsing groups does not repaginate the data.

Nested groups continuing on the next page
Figure 099 · Nested groups continuing on the next page · Actual K2 Runtime capture

Configuration reference

ParameterMeaning
enabledEnables row grouping
columnsOrdered array of column titles; each entry adds a level
allowUserGroupingAllows grouping commands in the header menu
collapsedByDefaultInitial collapsed state
showGroupingBarDisplays grouping chips and controls
showRecordCountDisplays the matching count per group
showExpandCollapseAllDisplays the global expand and collapse controls
aggregatesArray of aggregate definitions: column, operation, label and precision in these examples

Diagnostics and reset

(()=>{const id='REPLACE_WITH_YOUR_AREAITEMID';
const grid=K2AdvancedGrids.instance.inspect().find(g=>g.id===id);
console.log(grid?.rowGrouping);})();
Runtime inspection of grouping, pagination and other features
Figure 100 · Runtime inspection of grouping, pagination and other features · Actual K2 Runtime capture
K2AdvancedGridsConfig.defaults.rowGrouping.enabled = false;
K2AdvancedGrids.instance.refresh();

Benefits and current limitations

Grouping provides an interactive hierarchy and subtotals, unlike visual cell merging, which combines adjacent equal values without creating group headers. Active grouping suppresses cell merging. Grouping and aggregates operate on records already loaded into the browser, not on an unloaded server-side result set. The examples demonstrate filtering, paging and Data Visualization together; native record actions are covered in the next lesson. Console changes are temporary; persist them in the server configuration file if required.

Enlarged screenshot