|
| 1 | +--- |
| 2 | +title: Filtering Kendo UI Grid Based on Checkbox Selection |
| 3 | +description: Learn how to filter a Kendo UI Grid by checkbox selection using the dataSource filter method. |
| 4 | +type: how-to |
| 5 | +page_title: How to Filter Grid Rows in Kendo UI Based on Checkbox Clicks |
| 6 | +slug: filter-kendo-ui-grid-by-checkbox |
| 7 | +tags: kendo ui, grid, filter, checkbox, datasource |
| 8 | +res_type: kb |
| 9 | +ticketid: 1660908 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>Grid for Progress® Kendo UI®</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>2024.1.130</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +Filtering data in a Kendo UI Grid based on user interaction, such as a checkbox click, is a common scenario. This article demonstrates how to achieve this functionality by leveraging the dataSource [`filter()`](https://docs.telerik.com/kendo-ui/api/javascript/data/datasource/methods/filter) method. |
| 30 | + |
| 31 | +This KB article also answers the following questions: |
| 32 | +- How can I filter Grid records using a checkbox? |
| 33 | +- Is it possible to toggle Grid data filtering based on a checkbox state? |
| 34 | +- What method allows filtering Kendo UI Grid rows based on certain conditions? |
| 35 | + |
| 36 | +## Solution |
| 37 | + |
| 38 | +To filter the Kendo UI Grid records based on the state of a checkbox, follow these steps: |
| 39 | + |
| 40 | +1. Initialize the checkbox as a Kendo UI CheckBox component. |
| 41 | +2. Subscribe to the [`change`](https://docs.telerik.com/kendo-ui/api/javascript/ui/checkbox/events/change) event of the CheckBox to apply or remove the filter from the Grid's dataSource based on the checkbox state. |
| 42 | + |
| 43 | +Here is a practical example where a Grid is filtered to show only the records where the field "Discontinued" is `true` when the checkbox is checked. If the checkbox is unchecked, the filter is removed, and all records are shown. |
| 44 | + |
| 45 | +```html |
| 46 | +<!-- Include the necessary Kendo UI scripts and styles in your project --> |
| 47 | + |
| 48 | +<div id="eq1"></div> |
| 49 | +<div id="grid"></div> |
| 50 | + |
| 51 | +<script> |
| 52 | + $(document).ready(function() { |
| 53 | + // Initialize the checkbox |
| 54 | + $('#eq1').kendoCheckBox({ |
| 55 | + label: "Filter the True records", |
| 56 | + change: function(e) { |
| 57 | + var grid = $("#grid").data("kendoGrid"); |
| 58 | + if(e.checked) { |
| 59 | + // Apply the filter |
| 60 | + grid.dataSource.filter({ field: "Discontinued", operator: "eq", value: true }); |
| 61 | + } else { |
| 62 | + // Remove the filter |
| 63 | + grid.dataSource.filter({}); |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | +
|
| 68 | + // Initialize the Grid (ensure dataSource is configured) |
| 69 | + $("#grid").kendoGrid({ |
| 70 | + // Grid configuration |
| 71 | + }); |
| 72 | + }); |
| 73 | +</script> |
| 74 | +``` |
| 75 | + |
| 76 | +For a live example, check the below Dojo demo. |
| 77 | + |
| 78 | +```dojo |
| 79 | + <script src="https://demos.telerik.com/kendo-ui/content/shared/js/products.js"></script> |
| 80 | +
|
| 81 | + <div id="example"> |
| 82 | + <input type="checkbox" id="eq1" /> |
| 83 | + <br> |
| 84 | + <br> |
| 85 | + <div id="grid"></div> |
| 86 | +
|
| 87 | + <script> |
| 88 | + $(document).ready(function() { |
| 89 | +
|
| 90 | + $('#eq1').kendoCheckBox({ |
| 91 | + label: "Filter the True records", |
| 92 | + change: function(e) { |
| 93 | + if(e.checked) { |
| 94 | + $("#grid").data("kendoGrid").dataSource.filter({ field: "Discontinued", operator: "eq", value: true }); |
| 95 | + } else { |
| 96 | + $("#grid").data("kendoGrid").dataSource.filter({}); |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + }); |
| 101 | +
|
| 102 | + $("#grid").kendoGrid({ |
| 103 | + dataSource: { |
| 104 | + data: products, |
| 105 | + schema: { |
| 106 | + model: { |
| 107 | + fields: { |
| 108 | + ProductName: { type: "string" }, |
| 109 | + UnitPrice: { type: "number" }, |
| 110 | + UnitsInStock: { type: "number" }, |
| 111 | + Discontinued: { type: "boolean" } |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + pageSize: 20 |
| 116 | + }, |
| 117 | + height: 550, |
| 118 | + scrollable: true, |
| 119 | + sortable: true, |
| 120 | + filterable: true, |
| 121 | + pageable: { |
| 122 | + input: true, |
| 123 | + numeric: false |
| 124 | + }, |
| 125 | + columns: [ |
| 126 | + "ProductName", |
| 127 | + { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px" }, |
| 128 | + { field: "UnitsInStock", title: "Units In Stock", width: "130px" }, |
| 129 | + { field: "Discontinued", width: "130px" } |
| 130 | + ] |
| 131 | + }); |
| 132 | + }); |
| 133 | + </script> |
| 134 | +``` |
| 135 | + |
| 136 | +## See Also |
| 137 | + |
| 138 | +- [Kendo UI Grid DataSource Filter Method Documentation](https://docs.telerik.com/kendo-ui/api/javascript/data/datasource/methods/filter) |
| 139 | +- [Kendo UI CheckBox Component Documentation](https://docs.telerik.com/kendo-ui/controls/editors/checkbox/overview) |
| 140 | +- [Kendo UI Grid Overview](https://docs.telerik.com/kendo-ui/controls/data-management/grid/overview) |
0 commit comments