|
| 1 | +--- |
| 2 | +title: Displaying the count of distinct values in Grid column footer template |
| 3 | +description: An example on how to create a custom aggreagte that counts the distinct values in a column when using the Telerik UI for {{ site.framework }} Grid. |
| 4 | +type: how-to |
| 5 | +page_title: Displaying the count of distinct values in Grid column footer template |
| 6 | +slug: grid-distinct-values-aggregate |
| 7 | +tags: grid, count, distinct, values, aggregate, footer, template, telerik, core, mvc |
| 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.2.829 version</td> |
| 21 | + </tr> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +How can I calculate the count of the distinct values in a specified column and display the result within its footer template? |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +The Grid's DataSource supports [`Count()` aggregate]({% slug htmlhelper_datasourceaggregates %}) field that calculates the total number of values. However, if you need to get the number of the distinct values, follow the steps below: |
| 31 | + |
| 32 | +1. Execute a function within the `ClientFooterTemplate()` that will return the distinct values. Ensure that the server operations of the DataSource are disabled. |
| 33 | + |
| 34 | + ```HtmlHelper |
| 35 | + @(Html.Kendo().Grid<OrderViewModel>() |
| 36 | + .Name("grid") |
| 37 | + .Columns(columns => |
| 38 | + { |
| 39 | + columns.Bound(p => p.OrderID); |
| 40 | + columns.Bound(x => x.ShipName).ClientFooterTemplate("#: getDistinctValues() #"); |
| 41 | + }) |
| 42 | + ... |
| 43 | + .DataSource(dataSource => dataSource |
| 44 | + .Ajax() |
| 45 | + .PageSize(10) |
| 46 | + .ServerOperation(false) |
| 47 | + ... |
| 48 | + ) |
| 49 | + .Pageable() |
| 50 | + .Sortable() |
| 51 | + .Scrollable() |
| 52 | + .Groupable() |
| 53 | + .Filterable() |
| 54 | + ... |
| 55 | + ) |
| 56 | + ``` |
| 57 | + {% if site.core %} |
| 58 | + ```TagHelper |
| 59 | + @addTagHelper *, Kendo.Mvc |
| 60 | +
|
| 61 | + <kendo-grid name="grid"> |
| 62 | + <columns> |
| 63 | + <column field="OrderID"/> |
| 64 | + <column field="ShipName" footer-template="#: getDistinctValues() #"></column> |
| 65 | + </columns> |
| 66 | + <datasource type="DataSourceTagHelperType.Ajax" page-size="10" server-operation="false"> |
| 67 | + <!-- Other configuration --> |
| 68 | + </datasource> |
| 69 | + <pageable enabled="true"/> |
| 70 | + <scrollable enabled="true"/> |
| 71 | + <filterable enabled="true"/> |
| 72 | + <sortable enabled="true" /> |
| 73 | + <groupable enabled="true" /> |
| 74 | + <!-- Other configuration --> |
| 75 | + </kendo-grid> |
| 76 | +
|
| 77 | + ``` |
| 78 | + {% endif %} |
| 79 | + ```Script |
| 80 | + function getDistinctValues() { |
| 81 | + ... |
| 82 | + } |
| 83 | + ``` |
| 84 | +
|
| 85 | +1. Calculate the distinct values of the "ShipName" field on the client with jQuery. |
| 86 | +
|
| 87 | + ```Script |
| 88 | + function getDistinctValues() { |
| 89 | + var grid = $("#grid").getKendoGrid(); // Get a reference to the Grid. |
| 90 | + var allGridData = grid.dataSource.data(); // Get the Grid's data. |
| 91 | + var appliedFilters = grid.dataSource.filter(); // Get the current filter expressions. |
| 92 | + var aggregateResult = 0; |
| 93 | +
|
| 94 | + if (appliedFilters != null) { // Check if the Grid is filtered. |
| 95 | + var dataQuery = new kendo.data.Query(allGridData); // Create a Query (https://docs.telerik.com/kendo-ui/api/javascript/data/query). |
| 96 | + var filteredData = dataQuery.filter(appliedFilters).data; // Get a copy of the filtered data according to the applied filter expression. |
| 97 | + aggregateResult = getAggregates(filteredData); // Pass the filtered records to the getAggregates() function to calculate the distinct values count. |
| 98 | + } else { |
| 99 | + aggregateResult = getAggregates(allGridData); // If the Grid is not filtered, pass all records to the getAggregates() function. |
| 100 | + } |
| 101 | +
|
| 102 | + return `Total: ${aggregateResult}`; |
| 103 | + } |
| 104 | +
|
| 105 | + function getAggregates(gridData) { |
| 106 | + var distinctValues = []; |
| 107 | + $.each(gridData, function (i, el) { // Loop through the Grid records. |
| 108 | + if (distinctValues.indexOf(el.ShipName) == -1) { |
| 109 | + distinctValues.push(el.ShipName); // Store the unique values. |
| 110 | + } |
| 111 | + }); |
| 112 | + return distinctValues.length; // Return their count. |
| 113 | + } |
| 114 | + ``` |
| 115 | +
|
| 116 | +1. Handle the `DataBound` event of the Grid and update the displayed distinct values count when the Grid is filtered and/or grouped. |
| 117 | +
|
| 118 | + ```Script |
| 119 | + function onDataBound(e) { |
| 120 | + if (e.sender.dataSource.filter() != null) { // Check if the data is filtered. |
| 121 | + var appliedFilters = e.sender.dataSource.filter(); // Get the current filter expressions. |
| 122 | + var allGridData = e.sender.dataSource.data(); //Get all Grid records. |
| 123 | + var dataQuery = new kendo.data.Query(allGridData); //Create a Query. |
| 124 | + var filteredData = dataQuery.filter(appliedFilters).data; // Get a copy of the filtered data according to the applied filter expression. |
| 125 | + var updatedAggregate = getAggregates(filteredData); // Update the aggregation result. |
| 126 | + var columnFooterIndex = e.sender.dataSource.group().length + 2; // Get the column index when the Grid is grouped. "+2" is added because the "ShipName" column is the second column the Grid declaration. Replace the value with the index of the column at your end (1-based index). |
| 127 | + e.sender.footer.find(`.k-footer-template td:nth-child(${columnFooterIndex})`).html(`Total: ${updatedAggregate}`); // Update the column footer template. |
| 128 | + } |
| 129 | + } |
| 130 | + ``` |
| 131 | +
|
| 132 | +{% if site.core %} |
| 133 | +For a runnable example based on the code above, refer to the following REPL samples: |
| 134 | +
|
| 135 | +* [Sample code with the Grid HtmlHelper](https://netcorerepl.telerik.com/mdaDwCEr18tFpjd408) |
| 136 | +* [Sample code with the Grid TagHelper](https://netcorerepl.telerik.com/wRYNQsOh19KbF5LA18) |
| 137 | +{% else %} |
| 138 | +For a runnable example based on the code above, refer to the [REPL example on displaying distinct values in Grid column footer](https://netcorerepl.telerik.com/mdaDwCEr18tFpjd408). |
| 139 | +{% endif %} |
| 140 | +
|
| 141 | +## More {{ site.framework }} Grid Resources |
| 142 | +
|
| 143 | +* [{{ site.framework }} Grid Documentation]({%slug htmlhelpers_grid_aspnetcore_overview%}) |
| 144 | +
|
| 145 | +* [{{ site.framework }} Grid Demos](https://demos.telerik.com/{{ site.platform }}/grid/index) |
| 146 | +
|
| 147 | +{% if site.core %} |
| 148 | +* [{{ site.framework }} Grid Product Page](https://www.telerik.com/aspnet-core-ui/grid) |
| 149 | +
|
| 150 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiforcore%}) |
| 151 | +
|
| 152 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-core-ui) |
| 153 | +
|
| 154 | +{% else %} |
| 155 | +* [{{ site.framework }} Grid Product Page](https://www.telerik.com/aspnet-mvc/grid) |
| 156 | +
|
| 157 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiformvc%}) |
| 158 | +
|
| 159 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-mvc) |
| 160 | +{% endif %} |
| 161 | +
|
| 162 | +## See Also |
| 163 | +
|
| 164 | +* [Client-Side API Reference of the Grid for {{ site.framework }}](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid) |
| 165 | +* [Server-Side API Reference of the Grid for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/grid) |
| 166 | +{% if site.core %} |
| 167 | +* [Server-Side TagHelper API Reference of the Grid for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/taghelpers/grid) |
| 168 | +{% endif %} |
| 169 | +* [Telerik UI for {{ site.framework }} Breaking Changes]({%slug breakingchanges_2023%}) |
| 170 | +* [Telerik UI for {{ site.framework }} Knowledge Base](https://docs.telerik.com/{{ site.platform }}/knowledge-base) |
| 171 | +
|
0 commit comments