DHTMLX Docs & Samples Explorer

Grouping rows by column

Grid provides grouping rows by column.

  
expand / colapse 'John Grisham'
expand / colapse all groups
add / delete selected
Source
<link rel="STYLESHEET" type="text/css" href="../../codebase/dhtmlxgrid.css">
<link rel="stylesheet" type="text/css" href="../../codebase/skins/dhtmlxgrid_dhx_skyblue.css">
<script  src="../../codebase/dhtmlxcommon.js"></script>
<script  src="../../codebase/dhtmlxgrid.js"></script>
<script  src="../../codebase/dhtmlxgridcell.js"></script>    
<script  src="../../codebase/ext/dhtmlxgrid_splt.js"></script>    
<script  src="../../codebase/ext/dhtmlxgrid_group.js"></script>    
 
 
 
<table width="600px">
    <tr>
        <td>
            <input type="button" name="a2" value="ungroup" onclick="mygrid.unGroup();">&nbsp;&nbsp;
            <input type="button"  name="q1" value="regroup by" onclick='mygrid.groupBy(document.getElementById("sel").value);'>
            <select id="sel">
                <option value="0">Sales</option>
                <option value="1">Book title</option>
                <option value="2">Author</option>
                <option value="3">Price</option>
                <option value="4">In Store</option>
                <option value="5">Shipping</option>
                <option value="6">Bestseller</option>
                <option value="7">Date of Publication</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            <div id="gridbox" style="width:100%;height:250px;background-color:white;"></div>
        </td>
    </tr>
    <tr>
        <td>
            <a href="javascript:void(0)" onclick="if(mygrid._groups['John Grisham']) mygrid.expandGroup('John Grisham'); else alert('There is no such group!');">expand </a>/<a href="javascript:void(0)" onclick="if(mygrid._groups['John Grisham']) mygrid.collapseGroup('John Grisham'); else alert('There is no such group!')"> colapse</a> 'John Grisham'<br/>
            <a href="javascript:void(0)" onclick="mygrid.expandAllGroups()">expand </a>/<a href="javascript:void(0)" onclick="mygrid.collapseAllGroups()"> colapse</a> all groups<br/>
            <a href="javascript:void(0)" onclick="mygrid.addRow((new Date()).valueOf(),[1,'The King of Torts','John Grisham',3,1,1,1,0],-1)">add </a>/<a href="javascript:void(0)" onclick="if (mygrid.getSelectedId()) mygrid.deleteRow(mygrid.getSelectedId())"> delete selected</a>
        </td>
    </tr>
</table>
<script>
mygrid = new dhtmlXGridObject('gridbox');
mygrid.setImagePath("../../codebase/imgs/");
mygrid.setHeader("Sales,Book Title,Author,Price,In Store,Shipping,Bestseller,Date of Publication");
mygrid.setInitWidths("50,150,100,80,80,80,80,200");
mygrid.setColAlign("right,left,left,right,center,left,center,center");
mygrid.setColTypes("dyn,edtxt,ed,price,ch,co,ra,ro");
mygrid.setColSorting("na,str,str,int,int,int,na,na");
mygrid.init();
mygrid.setSkin("dhx_skyblue");
mygrid.loadXML("../common/grid.xml", function() {
    mygrid.groupBy(2);
});
</script>