|
| 1 | +--- |
| 2 | +title: Changing the Expand/Collapse SVG Icon in Kendo UI Grid Hierarchy |
| 3 | +description: Learn how to change the expand/collapse SVG icon in a hierarchy grid in Kendo UI Grid |
| 4 | +type: how-to |
| 5 | +page_title: How to Customize Expand/Collapse SVG Icons in Kendo UI Grid Hierarchy |
| 6 | +slug: customize-expand-collapse-icons-kendo-ui-grid |
| 7 | +tags: kendo ui, grid, hierarchy, expand, collapse, css, databound, detailexpand, detailcollapse |
| 8 | +res_type: kb |
| 9 | +ticketid: 1687918 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td> |
| 19 | +Grid for Progress® Kendo UI® |
| 20 | +</td> |
| 21 | +</tr> |
| 22 | +<tr> |
| 23 | +<td>Version</td> |
| 24 | +<td> |
| 25 | +2025.2.520 |
| 26 | +</td> |
| 27 | +</tr> |
| 28 | +</tbody> |
| 29 | +</table> |
| 30 | + |
| 31 | +## Description |
| 32 | + |
| 33 | +I want to change the expand/collapse [SVG icon](/styles-and-layout/sass-themes/svg-icons) in a hierarchy Grid in Kendo UI. The default Kendo UI caret icon needs to be replaced with custom SVG icons, such as `plus-outline` for expand and `minus-outline` for collapse. This requires implementing custom logic in the[`dataBound`](/api/javascript/ui/grid/events/databound), [`detailExpand`](/api/javascript/ui/grid/events/detailexpand), and [`detailCollapse`](/api/javascript/ui/grid/events/detailcollapse) event handlers of the Kendo UI Grid. |
| 34 | + |
| 35 | +This knowledge base article also answers the following questions: |
| 36 | +- How to replace default icons in Kendo UI Grid hierarchy? |
| 37 | +- How to use custom expand/collapse icons in Kendo UI Grid? |
| 38 | +- How to modify Kendo UI Grid hierarchy icons using event handlers? |
| 39 | + |
| 40 | +## Solution |
| 41 | + |
| 42 | +To achieve this, follow these steps: |
| 43 | + |
| 44 | +1. Implement the [`dataBound`](/api/javascript/ui/grid/events/databound), [`detailExpand`](/api/javascript/ui/grid/events/detailexpand), and [`detailCollapse`](/api/javascript/ui/grid/events/detailcollapse) event handlers: |
| 45 | + |
| 46 | +### `dataBound` Event Handler |
| 47 | + |
| 48 | +Customize the hierarchy cells to hide the default icons and replace them with custom icons. |
| 49 | + |
| 50 | +```javascript |
| 51 | +dataBound: function() { |
| 52 | + // Iterate over each hierarchy cell in the table body |
| 53 | + this.tbody.find('.k-hierarchy-cell').each(function(_, cell) { |
| 54 | + cell = $(cell); // Convert DOM element to jQuery object |
| 55 | + |
| 56 | + // Prepend a span element with class 'expand' and an onclick handler to expand the row |
| 57 | + cell.prepend("<span class='expand' onclick='expandRow(this)'></span>"); |
| 58 | + |
| 59 | + // Hide the default Kendo UI caret icon |
| 60 | + cell.find(".k-svg-i-caret-alt-right").hide(); |
| 61 | + }); |
| 62 | + |
| 63 | + // Replace the default icon with a custom 'plus-outline' icon for all elements with class 'expand' |
| 64 | + kendo.ui.icon($(".expand"), { icon: 'plus-outline' }); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### `detailExpand` Event Handler |
| 69 | + |
| 70 | +Update the expand button to show a collapse icon when the detail row is expanded. |
| 71 | + |
| 72 | +```javascript |
| 73 | +detailExpand: function(e) { |
| 74 | + // Update all 'expand' buttons when a detail row is expanded |
| 75 | + this.tbody.find('.expand').each(function(_, button) { |
| 76 | + button = $(button); // Convert DOM element to jQuery object |
| 77 | + |
| 78 | + // Change the text to "Collapse" |
| 79 | + button.text("Collapse"); |
| 80 | + |
| 81 | + // Switch class from 'expand' to 'collapse' |
| 82 | + button.removeClass("expand").addClass("collapse"); |
| 83 | + |
| 84 | + // Update the onclick handler to collapse the row |
| 85 | + button.attr("onclick", "collapseRow(this)"); |
| 86 | + }); |
| 87 | + |
| 88 | + // Replace the icon with a 'minus-outline' for all elements with class 'collapse' |
| 89 | + kendo.ui.icon($(".collapse"), { icon: 'minus-outline' }); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### `detailCollapse` Event Handler |
| 94 | + |
| 95 | +Revert the collapse button to show an expand icon when the detail row is collapsed. |
| 96 | + |
| 97 | +```javascript |
| 98 | +detailCollapse: function(e) { |
| 99 | + // Update all 'collapse' buttons when a detail row is collapsed |
| 100 | + this.tbody.find('.collapse').each(function(_, button) { |
| 101 | + button = $(button); // Convert DOM element to jQuery object |
| 102 | + |
| 103 | + // Change the text to "Expand" |
| 104 | + button.text("Expand"); |
| 105 | + |
| 106 | + // Switch class from 'collapse' to 'expand' |
| 107 | + button.removeClass("collapse").addClass("expand"); |
| 108 | + |
| 109 | + // Update the onclick handler to expand the row |
| 110 | + button.attr("onclick", "expandRow(this)"); |
| 111 | + }); |
| 112 | + |
| 113 | + // Replace the icon with a 'plus-outline' for all elements with class 'expand' |
| 114 | + kendo.ui.icon($(".expand"), { icon: 'plus-outline' }); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +2. Integrate these event handlers into your Kendo UI Grid initialization. |
| 119 | + |
| 120 | +For a runnable example, please refer to the next demo. |
| 121 | + |
| 122 | +```dojo |
| 123 | +<div id="grid"></div> |
| 124 | + <script> |
| 125 | + $(document).ready(function() { |
| 126 | + var element = $("#grid").kendoGrid({ |
| 127 | + dataSource: { |
| 128 | + type: "odata", |
| 129 | + transport: { |
| 130 | + read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Employees" |
| 131 | + }, |
| 132 | + pageSize: 6, |
| 133 | + serverPaging: true, |
| 134 | + serverSorting: true |
| 135 | + }, |
| 136 | + height: 600, |
| 137 | + sortable: true, |
| 138 | + pageable: true, |
| 139 | + detailInit: detailInit, |
| 140 | + dataBound: function() { |
| 141 | + this.tbody.find('.k-hierarchy-cell').each(function(_,x){ |
| 142 | + x= $(x); |
| 143 | + x.prepend("<span class='expand' onclick='expandRow(this)'></span>"); |
| 144 | + x.find(".k-svg-i-caret-alt-right").hide(); |
| 145 | + }); |
| 146 | + kendo.ui.icon($(".expand"), { icon: 'plus-outline' }); |
| 147 | + }, |
| 148 | + detailExpand: function(e) { |
| 149 | + this.tbody.find('.expand').each(function(_,x){ |
| 150 | + x= $(x); |
| 151 | + x.text("Collapse"); |
| 152 | + x.removeClass("expand").addClass("collapse"); |
| 153 | + x.attr("onclick","collapsdRow(this)"); |
| 154 | + }); |
| 155 | + kendo.ui.icon($(".collapse"), { icon: 'minus-outline' }); |
| 156 | + }, |
| 157 | + detailCollapse: function(e) { |
| 158 | + this.tbody.find('.collapse').each(function(_,x){ |
| 159 | + x= $(x); |
| 160 | + x.text("Expand"); |
| 161 | + x.removeClass("collapse").addClass("expand"); |
| 162 | + x.attr("onclick","expandRow(this)"); |
| 163 | + }); |
| 164 | + kendo.ui.icon($(".expand"), { icon: 'plus-outline' }); |
| 165 | + }, |
| 166 | + columns: [ |
| 167 | + { |
| 168 | + field: "FirstName", |
| 169 | + title: "First Name", |
| 170 | + width: "110px" |
| 171 | + }, |
| 172 | + { |
| 173 | + field: "LastName", |
| 174 | + title: "Last Name", |
| 175 | + width: "110px" |
| 176 | + }, |
| 177 | + { |
| 178 | + field: "Country", |
| 179 | + width: "110px" |
| 180 | + }, |
| 181 | + { |
| 182 | + field: "City", |
| 183 | + width: "110px" |
| 184 | + }, |
| 185 | + { |
| 186 | + field: "Title", |
| 187 | + width: "110px", |
| 188 | + } |
| 189 | + ] |
| 190 | + }); |
| 191 | + }); |
| 192 | +
|
| 193 | + function expandRow(e) { |
| 194 | + let row = $(e).parent().parent(); |
| 195 | + var grid = $("#grid").data("kendoGrid"); |
| 196 | + grid.expandRow(row); |
| 197 | + } |
| 198 | +
|
| 199 | + function collapsdRow(e) { |
| 200 | + let row = $(e).parent().parent(); |
| 201 | + var grid = $("#grid").data("kendoGrid"); |
| 202 | + grid.collapseRow(row); |
| 203 | + } |
| 204 | +
|
| 205 | + function detailInit(e) { |
| 206 | + $("<div/>").appendTo(e.detailCell).kendoGrid({ |
| 207 | + dataSource: { |
| 208 | + type: "odata", |
| 209 | + transport: { |
| 210 | + read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders" |
| 211 | + }, |
| 212 | + serverPaging: true, |
| 213 | + serverSorting: true, |
| 214 | + serverFiltering: true, |
| 215 | + pageSize: 10, |
| 216 | + filter: { field: "EmployeeID", operator: "eq", value: e.data.EmployeeID } |
| 217 | + }, |
| 218 | + scrollable: false, |
| 219 | + sortable: true, |
| 220 | + pageable: true, |
| 221 | + columns: [ |
| 222 | + { field: "OrderID", width: "110px" }, |
| 223 | + { field: "ShipCountry", title:"Ship Country", width: "110px" }, |
| 224 | + { field: "ShipAddress", title:"Ship Address", width: "110px" }, |
| 225 | + { field: "ShipName", title: "Ship Name", width: "300px" } |
| 226 | + ] |
| 227 | + }); |
| 228 | + } |
| 229 | + </script> |
| 230 | +``` |
| 231 | + |
| 232 | +## See Also |
| 233 | + |
| 234 | +- [Kendo UI Grid Documentation](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/overview) |
| 235 | +- [dataBound Event Documentation](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/databound) |
| 236 | +- [detailExpand Event Documentation](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/detailexpand) |
| 237 | +- [detailCollapse Event Documentation](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/detailcollapse) |
| 238 | +- [Kendo UI SVG Icons](https://docs.telerik.com/kendo-ui/styles-and-layout/sass-themes/svg-icons) |
| 239 | +- [Progress® Design System Kit Iconography](https://www.telerik.com/design-system/docs/foundation/iconography/icon-list/) |
0 commit comments