The Grouping Aggregates demo of the jQuery Grid.
A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.
showaggregates property – Shows or hides the aggregates in the grid's statusbar.showgroupaggregates property – Shows or hides the aggregates in Grid groups.showstatusbar property – Shows or hides the grid's statusbar.statusbarheight property – Sets the statusbar's height.selectionmode property – Sets or gets the selection mode.width property – Sets or gets the Grid's width.columns property – Sets the Grid columns.groupable property – This property enables or disables the grouping feature.groups property – Sets or gets the Grid groups when the Grouping feature is enabled.source property – Sets and gets the source.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxGrid shows the setup step by step.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxgrid.js, jqxgrid.edit.js, jqxgrid.selection.js, jqxgrid.grouping.js, jqxgrid.aggregates.js, jqxlistbox.js, jqxdropdownlist.js, jqxcheckbox.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers), generatedata.js (generatedata(), random sample rows).
$(document).ready(function () {
// prepare the data
var data = generatedata(50);
var source =
{
localdata: data,
datafields:
[
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'date', type: 'date' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' },
{ name: 'total', type: 'number' }
],
datatype: "array",
updaterow: function (rowid, rowdata) {
// synchronize with the server - send update command
}
};
var dataAdapter = new $.jqx.dataAdapter(source);
// initialize jqxGrid
$("#grid").jqxGrid(
{
width: getWidth('Grid'),
source: dataAdapter,
groupable: true,
selectionmode: 'singlecell',
showgroupaggregates: true,
showstatusbar: true,
showaggregates: true,
statusbarheight: 25,
groups: ['price'],
columns: [
{ text: 'First Name', groupable: true, aggregates: ["count"], datafield: 'firstname', width: 100 },
{ text: 'Last Name', groupable: true, aggregates: ["count"], datafield: 'lastname', width: 100 },
{ text: 'Product', groupable: true, columntype: 'dropdownlist', datafield: 'productname', width: 200 },
{ text: 'Quantity', datafield: 'quantity', aggregates: ["sum"], width: 70, cellsalign: 'right' },
{ text: 'Price', datafield: 'price', aggregates: ["sum"], cellsalign: 'right', width: 100, cellsformat: 'c2' },
{
text: 'Total', datafield: 'total', aggregates: ["sum"], cellsalign: 'right', cellsformat: 'c2',
cellsrenderer: function (row, column, value, defaultRender, column, rowData) {
if (value.toString().indexOf("Sum") >= 0) {
return defaultRender.replace("Sum", "Total");
}
},
aggregatesrenderer: function (aggregates, column, element) {
var renderstring = '<div style="position: relative; margin-top: 4px; margin-right:5px; text-align: right; overflow: hidden;">' + "Total" + ': ' + aggregates.sum + '</div>';
return renderstring;
}
}
]
});
});
<div id="jqxWidget">
<div id="grid"></div>
</div>