|
| 1 | +--- |
| 2 | +title: Use DropDownList as Boolean Filter in Grid |
| 3 | +description: How can I set up a custom filter for a Boolean column in the Grid and have a DropDownList which lists true, false, all? |
| 4 | +type: how-to |
| 5 | +page_title: Filter Boolean Grid Column with DropDownList |
| 6 | +slug: grid-boolean-dropdownlist-filter |
| 7 | +tags: grid, dropdownlist, filter, boolean, column, true, false |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Progress® Telerik® UI Grid for {{ site.product_short }}</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +How can I set up a custom filter for a Boolean column in the Grid and have a DropDownList which lists true, false, all? |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +1. Create a template function to initialize the DropDownList. |
| 29 | +``` |
| 30 | +function boolFilterTemplate(input) { |
| 31 | + input.kendoDropDownList({ |
| 32 | + dataSource: { |
| 33 | + data: [ |
| 34 | + { text: "True", value: true }, |
| 35 | + { text: "False", value: false } |
| 36 | + ] |
| 37 | + }, |
| 38 | + dataTextField: "text", |
| 39 | + dataValueField: "value", |
| 40 | + valuePrimitive: true, |
| 41 | + optionLabel: "All" |
| 42 | + }); |
| 43 | + } |
| 44 | +``` |
| 45 | + |
| 46 | +2. Use the [filterMenuInit](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/filtermenuinit) event of the Grid to replace the default filter label with more appropriate text. |
| 47 | + |
| 48 | +``` |
| 49 | +function onFilterMenuInit(e) { |
| 50 | + if (e.field == "Discontinued") { |
| 51 | + // replace default text in filter menu |
| 52 | + e.container.find(".k-filter-help-text").text("Show items with value:"); |
| 53 | + } |
| 54 | + } |
| 55 | +``` |
| 56 | + |
| 57 | +3. Use the [filter](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/filter) event of the Grid to replace the string value in the generated filter expression with its Boolean equivalent. |
| 58 | + |
| 59 | +``` |
| 60 | +function onFilter(e) { |
| 61 | + if (e.field === "Discontinued") { |
| 62 | + var filter = e.filter; |
| 63 | + if (filter && filter.filters && filter.filters.length > 0) { |
| 64 | + var filters = filter.filters; |
| 65 | + // convert the filter string value to a boolean one |
| 66 | + filters[0].value = (filters[0].value === "true"); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +``` |
| 71 | +Example: |
| 72 | + |
| 73 | +```View |
| 74 | +@(Html.Kendo().Grid<BooleanFilterGrid.Models.OrderViewModel>() |
| 75 | + .Name("grid") |
| 76 | + .Columns(columns => |
| 77 | + { |
| 78 | + columns.Bound(p => p.OrderID).Filterable(false); |
| 79 | + columns.Bound(p => p.Freight); |
| 80 | + columns.Bound(p => p.IsTrue); |
| 81 | + |
| 82 | + }) |
| 83 | + .Pageable() |
| 84 | + .Scrollable() |
| 85 | + .Filterable() |
| 86 | + .Events(ev=>ev.Filter("onFilter")) |
| 87 | + .HtmlAttributes(new { style = "height:440px;" }) |
| 88 | + .DataSource(dataSource => dataSource |
| 89 | + .Ajax() |
| 90 | + .PageSize(10) |
| 91 | + .Read(read => read.Action("Orders_Read", "Grid")) |
| 92 | + ) |
| 93 | +) |
| 94 | +``` |
| 95 | +```script.js |
| 96 | + $(document).ready(function () { |
| 97 | + var grid = $("#grid").data('kendoGrid'); |
| 98 | + var options = grid.getOptions(); |
| 99 | + options.columns[2].filterable = { ui: boolFilterTemplate }; |
| 100 | + options.filterMenuOpen = onFilterMenuInit; |
| 101 | + grid.setOptions(options); |
| 102 | + |
| 103 | + function boolFilterTemplate(input) { |
| 104 | + input.kendoDropDownList({ |
| 105 | + dataSource: { |
| 106 | + data: [ |
| 107 | + { text: "True", value: true }, |
| 108 | + { text: "False", value: false } |
| 109 | + ] |
| 110 | + }, |
| 111 | + dataTextField: "text", |
| 112 | + dataValueField: "value", |
| 113 | + valuePrimitive: true, |
| 114 | + optionLabel: "All" |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + function onFilterMenuInit(e) { |
| 119 | + if (e.field == "IsTrue") { |
| 120 | + // replace default text in filter menu |
| 121 | + e.container.find(".k-filter-help-text").text("Show items with value:"); |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + function onFilter(e) { |
| 127 | + if (e.field === "IsTrue") { |
| 128 | + var filter = e.filter; |
| 129 | + if (filter && filter.filters && filter.filters.length > 0) { |
| 130 | + var filters = filter.filters; |
| 131 | + // convert the filter string value to a boolean one |
| 132 | + filters[0].value = (filters[0].value === "true"); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +``` |
| 137 | +```Controller |
| 138 | +public partial class GridController : Controller |
| 139 | + { |
| 140 | + public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) |
| 141 | + { |
| 142 | + var result = Enumerable.Range(0, 50).Select(i => new OrderViewModel |
| 143 | + { |
| 144 | + OrderID = i, |
| 145 | + Freight = i * 10, |
| 146 | + OrderDate = DateTime.Now.AddDays(i), |
| 147 | + ShipName = "ShipName " + i, |
| 148 | + ShipCity = "ShipCity " + i, |
| 149 | + IsTrue = i % 2 == 0 |
| 150 | + }) ; |
| 151 | +
|
| 152 | + return Json(result.ToDataSourceResult(request)); |
| 153 | + } |
| 154 | + } |
| 155 | +``` |
| 156 | +```Model |
| 157 | + public class OrderViewModel |
| 158 | + { |
| 159 | + public int OrderID { get; set; } |
| 160 | + public bool IsTrue { get; set; } |
| 161 | + public decimal? Freight { get; set; } |
| 162 | + [Required] |
| 163 | + public DateTime? OrderDate { get; set; } |
| 164 | + public string ShipCity { get; set; } |
| 165 | +
|
| 166 | + public string ShipName{ get; set; } |
| 167 | + |
| 168 | + } |
| 169 | +``` |
0 commit comments