Documentation
This tutorial groups sales by category, region or product, writes a custom header for each group, and shows totals: built-in aggregates such as sum and average, and a custom one, for each group and for the whole table.

Set up a page as in Bind the jQuery Grid to data. Grouping needs jqxgrid.grouping.js and jqxmenu.js; the totals need jqxgrid.aggregates.js:
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxcore.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxdata.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxbuttons.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxscrollbar.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxmenu.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.selection.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.grouping.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.aggregates.js"></script>
groupable turns grouping on and groups lists the columns to group by when the grid starts. showgroupsheader adds a bar above the headers: drag a column header there to group by it as well, or click the × on a group to remove it. Groups start collapsed; click the arrow at the start of a group's row to open it.
grid.jqxGrid({
theme: 'fluent',
width: '100%',
height: 480,
source,
columns,
groupable: true,
groups: ['category'],
groupsrenderer,
showgroupsheader: true,
showaggregates: true,
showgroupaggregates: true,
showstatusbar: true,
statusbarheight: 70
});
groupsrenderer writes each group's header. It receives the header's text, the group's value, whether the group is expanded, and an object with the group's rows (subItems) and, when the grid is grouped by more than one column, its subgroups (subGroups). When the grid also shows totals for each group, the total rows are in those lists too, marked totalsrow, so the counts leave them out. The group's value comes from the data, so it is escaped:
// The group value comes from the data, so the header escapes it.
const escapeHtml = (text) => String(text).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
// A group's header: its value and how many rows or subgroups it holds. data.subItems holds
// the group's rows, and data.subGroups its subgroups when the grid is grouped by more than
// one column. With showgroupaggregates, the group's total rows are in the same list, marked
// totalsrow, so the counts leave them out.
function groupsrenderer(text, group, expanded, data) {
const rows = (data.subItems ?? []).filter((row) => !row.totalsrow).length;
const groups = (data.subGroups ?? []).filter((row) => !row.totalsrow).length;
return `<div style="padding: 6px 4px;"><strong>${escapeHtml(group)}</strong> (${groups ? `${groups} groups` : `${rows} sales`})</div>`;
}
A column's aggregates lists what to compute: sum, avg, min, max and count, or a custom aggregate, an object whose key is its label and whose function is called for each row with the running result and the row's value. showaggregates with showstatusbar shows them for the whole table under the rows, and showgroupaggregates for each group, as rows at the end of the group: the first row holds each column's first aggregate, the second row the second, and so on.
aggregatesrenderer decides how a column's totals are written in the status bar. The values it receives are already formatted with the column's cellsformat, so the revenue total arrives as $57,320. A column without a renderer shows each aggregate as its label and value (Sum:2940), and so do the group total rows, which do not use the renderer.
const columns = [
{ text: 'Region', datafield: 'region', width: 110 },
{ text: 'Category', datafield: 'category', width: 140 },
{ text: 'Product', datafield: 'product', width: 160 },
{
text: 'Units', datafield: 'units', width: 150, cellsalign: 'right', align: 'right',
// A custom aggregate next to the built-in ones: how many sales were of 30 units or more.
aggregates: [
'sum',
'avg',
{ 'Large sales': (count, value) => (value >= 30 ? count + 1 : count) }
]
},
{
text: 'Revenue', datafield: 'revenue', cellsalign: 'right', align: 'right', cellsformat: 'c0',
aggregates: ['sum', 'max'],
// How the totals are written. The grid passes them already formatted with the column's
// cellsformat ("$57,320").
aggregatesrenderer: (aggregates) =>
`<div style="padding: 6px; text-align: right;">Total ${aggregates.sum}<br>Largest sale ${aggregates.max}</div>`
}
];
A select of the page's own groups by another column: cleargroups removes the grouping and addgroup adds a column to it. expandallgroups and collapseallgroups open and close every group.
// Grouping from the page's own control (the grid's header also takes drag and drop).
document.getElementById('groupBy').addEventListener('change', (event) => {
const field = event.target.value;
grid.jqxGrid('cleargroups');
if (field) {
grid.jqxGrid('addgroup', field);
}
});
document.getElementById('expand').addEventListener('click', () => grid.jqxGrid('expandallgroups'));
document.getElementById('collapse').addEventListener('click', () => grid.jqxGrid('collapseallgroups'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sales</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.base.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.fluent.css">
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxcore.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxdata.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxbuttons.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxscrollbar.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxmenu.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.selection.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.grouping.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.aggregates.js"></script>
</head>
<body>
<main style="padding: 16px;">
<h1>Sales</h1>
<p>
<label>Group by
<select id="groupBy">
<option value="">Nothing</option>
<option value="category" selected>Category</option>
<option value="region">Region</option>
<option value="product">Product</option>
</select>
</label>
<button id="expand" type="button">Expand all</button>
<button id="collapse" type="button">Collapse all</button>
</p>
<div id="grid"></div>
</main>
<script type="module">
const regions = ['North', 'South', 'East', 'West'];
const catalog = [
{ category: 'Beverages', product: 'Chai', price: 18 },
{ category: 'Beverages', product: 'Chang', price: 19 },
{ category: 'Condiments', product: 'Aniseed Syrup', price: 10 },
{ category: 'Seafood', product: 'Ikura', price: 31 },
{ category: 'Seafood', product: 'Konbu', price: 6 },
{ category: 'Dairy', product: 'Mascarpone', price: 32 }
];
const sales = Array.from({ length: 120 }, (_, i) => {
const item = catalog[(i * 7) % catalog.length];
const units = 5 + ((i * 13) % 40);
return { id: i + 1, region: regions[(i * 3) % regions.length], category: item.category, product: item.product, units, revenue: units * item.price };
});
const source = new $.jqx.dataAdapter({
localdata: sales,
datatype: 'array',
id: 'id',
datafields: [
{ name: 'region', type: 'string' },
{ name: 'category', type: 'string' },
{ name: 'product', type: 'string' },
{ name: 'units', type: 'number' },
{ name: 'revenue', type: 'number' }
]
});
const columns = [
{ text: 'Region', datafield: 'region', width: 110 },
{ text: 'Category', datafield: 'category', width: 140 },
{ text: 'Product', datafield: 'product', width: 160 },
{
text: 'Units', datafield: 'units', width: 150, cellsalign: 'right', align: 'right',
// A custom aggregate next to the built-in ones: how many sales were of 30 units or more.
aggregates: [
'sum',
'avg',
{ 'Large sales': (count, value) => (value >= 30 ? count + 1 : count) }
]
},
{
text: 'Revenue', datafield: 'revenue', cellsalign: 'right', align: 'right', cellsformat: 'c0',
aggregates: ['sum', 'max'],
// How the totals are written. The grid passes them already formatted with the column's
// cellsformat ("$57,320").
aggregatesrenderer: (aggregates) =>
`<div style="padding: 6px; text-align: right;">Total ${aggregates.sum}<br>Largest sale ${aggregates.max}</div>`
}
];
// The group value comes from the data, so the header escapes it.
const escapeHtml = (text) => String(text).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
// A group's header: its value and how many rows or subgroups it holds. data.subItems holds
// the group's rows, and data.subGroups its subgroups when the grid is grouped by more than
// one column. With showgroupaggregates, the group's total rows are in the same list, marked
// totalsrow, so the counts leave them out.
function groupsrenderer(text, group, expanded, data) {
const rows = (data.subItems ?? []).filter((row) => !row.totalsrow).length;
const groups = (data.subGroups ?? []).filter((row) => !row.totalsrow).length;
return `<div style="padding: 6px 4px;"><strong>${escapeHtml(group)}</strong> (${groups ? `${groups} groups` : `${rows} sales`})</div>`;
}
const grid = $('#grid');
grid.jqxGrid({
theme: 'fluent',
width: '100%',
height: 480,
source,
columns,
groupable: true,
groups: ['category'],
groupsrenderer,
showgroupsheader: true,
showaggregates: true,
showgroupaggregates: true,
showstatusbar: true,
statusbarheight: 70
});
// Grouping from the page's own control (the grid's header also takes drag and drop).
document.getElementById('groupBy').addEventListener('change', (event) => {
const field = event.target.value;
grid.jqxGrid('cleargroups');
if (field) {
grid.jqxGrid('addgroup', field);
}
});
document.getElementById('expand').addEventListener('click', () => grid.jqxGrid('expandallgroups'));
document.getElementById('collapse').addEventListener('click', () => grid.jqxGrid('collapseallgroups'));
</script>
</body>
</html>
The grid opens grouped by category: Beverages (40 sales), Condiments (20 sales), Seafood (40 sales) and Dairy (20 sales). The status bar shows the units (Sum:2940, Avg:24.5, Large sales:45) and the revenue (Total $57,320, Largest sale $1,408). Open Beverages and scroll to its end: its total rows read Sum:980 and Sum:$18,140, Avg:24.5 and Max:$836, Large sales:15. Pick Region to group by region instead, then drag the Category header into the bar above the grid: each region shows (3 groups).