|
| 1 | +--- |
| 2 | +title: Customizing the Grid Column Sorting Icons |
| 3 | +description: An example on how to change the default column sorting icons when using the Telerik UI for {{ site.framework }} Grid. |
| 4 | +type: how-to |
| 5 | +page_title: Customizing the Grid Column Sorting Icons |
| 6 | +slug: grid-custom-sorting-icons |
| 7 | +tags: grid, sorting, icons, columns, header, template |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +<table> |
| 14 | + <tr> |
| 15 | + <td>Product</td> |
| 16 | + <td>{{ site.product }} Grid</td> |
| 17 | + </tr> |
| 18 | + <tr> |
| 19 | + <td>Progress {{ site.product }} version</td> |
| 20 | + <td>Created with the 2023.3.1010 version</td> |
| 21 | + </tr> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | +How can I change the default sorting icons of the Grid columns? |
| 26 | + |
| 27 | +## Solution |
| 28 | +To set custom ascending and descending sorting icons when a specified Grid column is sorted and display a double arrow icon when the column is not sorted, follow these steps: |
| 29 | + |
| 30 | +1. Add the following CSS style to hide the default sorting icons: |
| 31 | +```css |
| 32 | +#grid .k-grid-header .k-cell-inner .k-link .k-sort-icon { |
| 33 | + display: none; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +2. Handle the `DataBound` event of the Grid that triggers each time a specified column is sorted (the sorting data operation is performed on the server (`ServerOperation(true)`). |
| 38 | +```HtmlHelper |
| 39 | +@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>() |
| 40 | + .Name("grid") |
| 41 | + .Sortable(sortable => sortable |
| 42 | + .AllowUnsort(true) |
| 43 | + .SortMode(GridSortMode.Mixed) |
| 44 | + .ShowIndexes(true)) |
| 45 | + .Events(ev => ev.DataBound("onDataBound")) |
| 46 | + // other grid configuration |
| 47 | +) |
| 48 | +``` |
| 49 | +{% if site.core %} |
| 50 | +```TagHelper |
| 51 | + @addTagHelper *, Kendo.Mvc |
| 52 | +
|
| 53 | + <kendo-grid name="grid" on-data-bound="onDataBound"> |
| 54 | + <sortable enabled="true" allow-unsort="true" mode="mixed" show-indexes="true" /> |
| 55 | + <!-- Other configuration --> |
| 56 | + </kendo-grid> |
| 57 | + ``` |
| 58 | +{% endif %} |
| 59 | + |
| 60 | +3. Within the `DataBound` event handler, define the desired SVG icons as explained in the [SVG Icons section in the documentation](https://docs.telerik.com/{{ site.platform }}/styles-and-layout/sass-themes/svg-icons#setting-svg-icons-from-client). Then, access the currently sorted fields through the [`sort()`](https://docs.telerik.com/kendo-ui/api/javascript/data/datasource/methods/sort) method of the DataSource and append the respective icon (ascending or descending). |
| 61 | + |
| 62 | +```javascript |
| 63 | +<script> |
| 64 | +function onDataBound(e) { |
| 65 | + let grid = this; |
| 66 | + let sorts = grid.dataSource.sort(); // Get the currently sorted fields. |
| 67 | + |
| 68 | + var nonSortableIcon = kendo.ui.icon({ icon: 'caret-alt-expand', type: 'svg' }); // Specify the desired SVG icon for the non-sorted columns. |
| 69 | + var descSortIcon = kendo.ui.icon({ icon: 'caret-alt-down', type: 'svg' }); // Specify the desired SVG icon for descending order. |
| 70 | + var ascSortIcon = kendo.ui.icon({ icon: 'caret-alt-up', type: 'svg' }); // Specify the desired SVG icon for ascending order. |
| 71 | + |
| 72 | + if(sorts == undefined) { // Check if the Grid is sorted initially. |
| 73 | + grid.thead.find("th .k-cell-inner .k-link").each(function(){ // Loop through the Grid column headers. |
| 74 | + $(this).append(nonSortableIcon); // Add the "nonSortableIcon " icon on all column headers. |
| 75 | + }); |
| 76 | + } else { |
| 77 | + // Reset the sort icons. |
| 78 | + grid.thead.find("th .k-cell-inner .k-link").each(function(){ |
| 79 | + if($(this).find("span.k-svg-i-caret-alt-expand").length == 0) { |
| 80 | + $(this).find("span.k-icon").remove(); // Remove the "descSortIcon"/"ascSortIcon" icon. |
| 81 | + $(this).append(nonSortableIcon); // Add the default icon. |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + $.each(sorts, function(i,value) { // Loop through the sorted fields. |
| 86 | + let colHeader = grid.thead.find(`th[data-field='${value.field}']`); // Get the column header of the sorted column. |
| 87 | + if(colHeader) { |
| 88 | + colHeader.find(".k-link span.k-icon").remove(); // Remove the default icon. |
| 89 | + if(value.dir == "asc") { |
| 90 | + colHeader.find(".k-link").append(ascSortIcon); //Append the custom "ascSortIcon". |
| 91 | + } |
| 92 | + if(value.dir == "desc") { |
| 93 | + colHeader.find(".k-link").append(descSortIcon); //Append the custom "descSortIcon". |
| 94 | + } |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
| 99 | +</script> |
| 100 | +``` |
| 101 | +{% if site.core %} |
| 102 | +For a runnable example based on the code above, refer to the following REPL samples: |
| 103 | + |
| 104 | +* [Sample code with the Grid HtmlHelper](https://netcorerepl.telerik.com/cyYvlVFq13LOju7R01) |
| 105 | +* [Sample code with the Grid TagHelper](https://netcorerepl.telerik.com/QSaPFLPq2056hwO401) |
| 106 | +{% else %} |
| 107 | +For a runnable example based on the code above, refer to the [REPL example on customizing the default sorting icons of the Grid columns](https://netcorerepl.telerik.com/cyYvlVFq13LOju7R01). |
| 108 | +{% endif %} |
| 109 | + |
| 110 | +## More {{ site.framework }} Grid Resources |
| 111 | + |
| 112 | +* [{{ site.framework }} Grid Documentation]({%slug htmlhelpers_grid_aspnetcore_overview%}) |
| 113 | + |
| 114 | +* [{{ site.framework }} Grid Demos](https://demos.telerik.com/{{ site.platform }}/grid/index) |
| 115 | + |
| 116 | +{% if site.core %} |
| 117 | +* [{{ site.framework }} Grid Product Page](https://www.telerik.com/aspnet-core-ui/grid) |
| 118 | + |
| 119 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiforcore%}) |
| 120 | + |
| 121 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-core-ui) |
| 122 | + |
| 123 | +{% else %} |
| 124 | +* [{{ site.framework }} Grid Product Page](https://www.telerik.com/aspnet-mvc/grid) |
| 125 | + |
| 126 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiformvc%}) |
| 127 | + |
| 128 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-mvc) |
| 129 | +{% endif %} |
| 130 | + |
| 131 | +## See Also |
| 132 | + |
| 133 | +* [Client-Side API Reference of the Grid for {{ site.framework }}](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid) |
| 134 | +* [Server-Side API Reference of the Grid for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/grid) |
| 135 | +{% if site.core %} |
| 136 | +* [Server-Side TagHelper API Reference of the Grid for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/taghelpers/grid) |
| 137 | +{% endif %} |
| 138 | +* [Telerik UI for {{ site.framework }} Breaking Changes]({%slug breakingchanges_2023%}) |
| 139 | +* [Telerik UI for {{ site.framework }} Knowledge Base](https://docs.telerik.com/{{ site.platform }}/knowledge-base) |
0 commit comments