{"id":2505,"date":"2014-10-21T19:58:12","date_gmt":"2014-10-21T15:58:12","guid":{"rendered":"http:\/\/www.dhtmlx.com\/blog\/?p=2505"},"modified":"2025-05-19T14:31:50","modified_gmt":"2025-05-19T11:31:50","slug":"grouping-data-in-dhtmlxgrid","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/grouping-data-in-dhtmlxgrid\/","title":{"rendered":"DHTMLX Little-Known Features: Grouping Data in dhtmlxGrid"},"content":{"rendered":"<p>There are so many features available in our <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxGrid\/\" target=\"_blank\">JavaScript grid control<\/a> and it is so difficult to pick up the one to tell you about! In this article we would like to remind you about the possibility to group data in dhtmlxGrid.<\/p>\n<p>There are two option to do this: you can group data <a href=\"https:\/\/docs.dhtmlx.com\/grid__grouping_rows.html\" target=\"_blank\">by rows<\/a> and <a href=\"https:\/\/docs.dhtmlx.com\/grid__grouping_columns.html\" target=\"_blank\">by columns<\/a>.<\/p>\n<p>This time we&#8217;ll talk about grouping data by rows. By default, there is no data grouping displayed on the page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/without-grouping.png\" alt=\"grid-without-grouping\" width=\"598\" height=\"256\" class=\"aligncenter size-full wp-image-2506\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/without-grouping.png 598w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/without-grouping-300x128.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/p>\n<p>But we can easily do it by a command <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxgrid_groupby.html\" target=\"_blank\">groupBy(col)<\/a>, where <strong>col<\/strong> is the index of a column the grid will be grouped by.<\/p>\n<p>To activate grouping rows in the grid:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">mygrid <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> dhtmlXGridObject<span class=\"br0\">&#40;<\/span><span class=\"st0\">'gridbox'<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n...<br \/>\n<span class=\"me1\">mygrid<\/span>.<span class=\"me1\">load<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;..\/common\/grid.xml&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; mygrid.<span class=\"me1\">groupBy<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> <br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/grouped_grid.png\" alt=\"grouped-grid\" width=\"600\" height=\"256\" class=\"aligncenter size-full wp-image-2507\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/grouped_grid.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/grouped_grid-300x128.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><br \/>\n<!--more--><br \/>\nIf you want to ungroup the grid, you should use <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxgrid_ungroup.html\" target=\"_blank\">unGroup()<\/a> method:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">mygrid.<span class=\"me1\">unGroup<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><\/br><br \/>\n<strong>Other related features<\/strong><\/p>\n<p>The following are 4 methods that can be used in order to <strong>expand\/collapse a group<\/strong>:<\/p>\n<p>    expandGroup() &#8211; expands a group with the specified key;<br \/>\n    collapseGroup() &#8211; collapses a group with the specified key;<br \/>\n    expandAllGroups() &#8211; expands all groups;<br \/>\n    collapseAllGroups() &#8211; collapses all groups.<br \/>\n<\/br><br \/>\nAlso based on your needs you can <strong>customize the presentation of the group summary rows<\/strong>. Two options are available for this: row mask and handler function.<\/p>\n<p>1) Row Mask<\/p>\n<p>The group contains a value that may be aggregated and shown as some kind of total. The second parameter of groupBy() command allows to define the mask used by group-line:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">mygrid.<span class=\"me1\">groupBy<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span><span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;#stat_max&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;#title&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;#stat_total&quot;<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/group_mask.png\" alt=\"row-mask\" width=\"365\" height=\"116\" class=\"aligncenter size-full wp-image-2508\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/group_mask.png 365w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/group_mask-300x95.png 300w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/p>\n<p>The second parameter of <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxgrid_groupby.html\" target=\"_blank\">groupBy()<\/a> command is an array, each value of which is mapped to the related column. <a href=\"https:\/\/docs.dhtmlx.com\/grid__grouping_rows.html\">Check the possible values in the documentation<\/a>.<\/p>\n<p>2) Handler Function<\/p>\n<p>The grid allows to redefine the text of group-row with the help of grid.customGroupFormat:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">&nbsp; &nbsp; mygrid.<span class=\"me1\">customGroupFormat<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>text<span class=\"sy0\">,<\/span>count<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> <span class=\"st0\">&quot;Grouped by &quot;<\/span><span class=\"sy0\">+<\/span>text<span class=\"sy0\">+<\/span><span class=\"st0\">&quot;, there are &quot;<\/span><span class=\"sy0\">+<\/span>count<span class=\"sy0\">+<\/span><span class=\"st0\">&quot; related records&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; mygrid.<span class=\"me1\">groupBy<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/handler-function.png\" alt=\"handler-function\" width=\"600\" height=\"251\" class=\"aligncenter size-full wp-image-2509\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/handler-function.png 600w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/handler-function-300x125.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Also you can use the aggregation shortcuts in your handler function as well. To use the shortcuts in the function, you should call the <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxgrid_groupstat.html\" target=\"_blank\">groupStat<\/a> method. It will return the result of aggregation for a column and will accept the following parameters:<\/p>\n<p>&#8211; group name;<br \/>\n&#8211; column index;<br \/>\n&#8211; name of stat operation.<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">mygrid.<span class=\"me1\">customGroupFormat<\/span><span class=\"sy0\">=<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>name<span class=\"sy0\">,<\/span>count<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> name<br \/>\n&nbsp; &nbsp; <span class=\"sy0\">+<\/span><span class=\"st0\">&quot;, Max sales=&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">+<\/span>grid.<span class=\"me1\">groupStat<\/span><span class=\"br0\">&#40;<\/span>name<span class=\"sy0\">,<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;stat_max&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">+<\/span><span class=\"st0\">&quot;, total=&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">+<\/span>grid.<span class=\"me1\">groupStat<\/span><span class=\"br0\">&#40;<\/span>name<span class=\"sy0\">,<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;stat_total&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\nmygrid.<span class=\"me1\">groupBy<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/custom_summary_row2.png\" alt=\"aggregate-shortcuts\" width=\"365\" height=\"116\" class=\"aligncenter size-full wp-image-2510\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/custom_summary_row2.png 365w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2014\/10\/custom_summary_row2-300x95.png 300w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/p>\n<p>Check all the details about <a href=\"https:\/\/docs.dhtmlx.com\/grid__grouping_rows.html\" target=\"_blank\">grouping by row<\/a> in documentation. Also have a look at the <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxGrid\/samples\/07_extended_modes\/01_pro_group.html\" target=\"_blank\">Grid demo<\/a>. Read about our <a href=\"\/\/dhtmlx.com\/docs\/products\/dhtmlxGrid\/\">JavaScript table library<\/a> and download it right now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are so many features available in our JavaScript grid control and it is so difficult to pick up the one to tell you about! In this article we &#8230;<\/p>\n","protected":false},"author":2,"featured_media":3318,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,71,1],"tags":[33,43,6,14],"class_list":["post-2505","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-posts-for-codeproject-com","category-tutorials","category-uncategorized","tag-all-posts","tag-dhtmlx-tips","tag-dhtmlxgrid","tag-tips"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/comments?post=2505"}],"version-history":[{"count":4,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2505\/revisions"}],"predecessor-version":[{"id":13904,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2505\/revisions\/13904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media\/3318"}],"wp:attachment":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media?parent=2505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=2505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=2505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}