In general, dhtmlxGrid displays data on the page without any grouping.
To change dhmlxGrid layout, you can group data by rows or by columns
(read the article Grouping columns).
Grouping can't be used with the Paging and Smart Rendering data loading modes.
Grouping rows will produce the view where rows are filtered by the values of the specified column.
Grouping is invoked by a simple command groupBy(col).
Activating grouping rows in the grid
mygrid = new dhtmlXGridObject('gridbox');
...
mygrid.load("../common/grid.xml",function(){
mygrid.groupBy(2);// the grid is grouped by the second column
});
To ungroup a grid and bring the standard view back you should call the method unGroup().
Ungrouping the grid
mygrid = new dhtmlXGridObject('gridbox');
...
mygrid.unGroup();
Related sample: Grouping rows by column
There are 4 methods that can be used in the mentioned context:
mygrid.expandGroup('John Grisham');
mygrid.collapseGroup('Stephen King');
mygrid.expandAllGroups();
mygrid.collapseAllGroups();
You can customize the presentation of the group summary rows to meet your needs.
There are 2 ways for this:
The mask for the summary rows is set through the second (optional) parameter of the groupBy() method. It's an array, each item of which is mapped to the related column.
Customizing the group summary rows through the row mask
mygrid.groupBy(2,["#stat_max","#title","","#stat_total"]);
The possible aggregate shortcuts:
Note, the stat-based values are rendered using the same data type that is used in the related column. It allows using the setNumberFormat() method against them (when the source column is of the ron/edn type).
Related sample: Custom group format
To customize the group summary rows appearance you can define some handler function and apply it through the customGroupFormat property.
The function takes 2 parameters:
Customizing the group summary rows through a handler function
mygrid.customGroupFormat=function(name,count){
return name+" :: found " +count+ " records";
}
mygrid.groupBy(2);
Related sample: Custom group format
Note, you can use the aggregation shortcuts in your handler function as well (see the list of such shortcuts in the chapter above). To use the shortcuts in the function, you should call the groupStat method that returns the result of aggregation for a column and accepts the following parameters:
Using aggregate shortcuts in the handler function
mygrid.customGroupFormat=function(name,count){
return name
+", Max sales="
+grid.groupStat(name,3,"stat_max")
+", total="
+grid.groupStat(name,3,"stat_total");
}
mygrid.groupBy(2);
Related sample: Custom group format
The library provides a built-in iterator that can be used to iterate through all rows in some group and calculate a custom math.
Using iterator for a group
mygrid.forEachRowInGroup(name,function(id){
do_something_with_row(id);
});
where name is the group key (e.g. 'John Grisham').
Back to top