|
| 1 | +--- |
| 2 | +title: Filter Column With DropDownList |
| 3 | +description: An example on how to filter column with complex objects |
| 4 | +type: how-to |
| 5 | +page_title: Filter Column With Complex Objects |
| 6 | +slug: grid-filter-column-with-dropdownlist |
| 7 | +position: |
| 8 | +tags: grid, filter, dropdownlist |
| 9 | +ticketid: 1144229 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tr> |
| 16 | + <td>Product Version</td> |
| 17 | + <td>2017.3 1026</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>Product</td> |
| 21 | + <td>Grid for Progress® Kendo UI®</td> |
| 22 | + </tr> |
| 23 | +</table> |
| 24 | + |
| 25 | + |
| 26 | +## Description |
| 27 | + |
| 28 | +How to filter a column with a DropDown editor when it is bound to a complex object? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +This could be achieved by programmatically applying the filters on the [filter](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/filter) event of the Grid. It will also require to programmatically clear the filters for that column when the clear button is clicked. |
| 33 | + |
| 34 | +Please check the following example demonstrating this: |
| 35 | + |
| 36 | +````html |
| 37 | + <script src="https://demos.telerik.com/kendo-ui/content/shared/js/products.js"></script> |
| 38 | + <div id="example"> |
| 39 | + <div id="grid"></div> |
| 40 | + |
| 41 | + <script> |
| 42 | +
|
| 43 | + $(document).ready(function () { |
| 44 | + var dataSource = new kendo.data.DataSource({ |
| 45 | + pageSize: 20, |
| 46 | + data: products, |
| 47 | + autoSync: true, |
| 48 | + schema: { |
| 49 | + model: { |
| 50 | + id: "ProductID", |
| 51 | + fields: { |
| 52 | + ProductID: { editable: false, nullable: true }, |
| 53 | + ProductName: { validation: { required: true } }, |
| 54 | + Category: { defaultValue: { CategoryID: 1, CategoryName: "Beverages"} }, |
| 55 | + UnitPrice: { type: "number", validation: { required: true, min: 1} } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | +
|
| 61 | + $("#grid").kendoGrid({ |
| 62 | + dataSource: dataSource, |
| 63 | + filterMenuInit:function(e){ |
| 64 | + if(e.field === "Category"){ |
| 65 | + $(e.container).find('[type="reset"]').click(function(event){ |
| 66 | + var filters = e.sender.dataSource.filter().filters |
| 67 | + for (let i = 0; i <= filters.length; i++){ |
| 68 | + if (filters[i].field == "Category.CategoryName"){ |
| 69 | + filters.splice(i, 1); |
| 70 | + e.sender.dataSource.filter(filters) |
| 71 | + } |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | + }, |
| 76 | + pageable: true, |
| 77 | + filterable:true, |
| 78 | + filter:function(e){ |
| 79 | + if(e.field == "Category" && e.filter !== null){ |
| 80 | + var currentFilter = [] |
| 81 | + var filtersCount = e.filter.filters[0].length |
| 82 | + var value = e.filter.filters[0].value |
| 83 | + if(e.sender.dataSource.filter() !== undefined){ |
| 84 | + currentFilter = e.sender.dataSource.filter().filters // Retain the other filters |
| 85 | + } |
| 86 | + currentFilter.push({field:"Category.CategoryName", operatot:"eq", value:value}) |
| 87 | + e.preventDefault() |
| 88 | + e.sender.dataSource.filter(currentFilter) |
| 89 | + } |
| 90 | + }, |
| 91 | + height: 550, |
| 92 | + toolbar: ["create"], |
| 93 | + columns: [ |
| 94 | + { field:"ProductName",title:"Product Name" }, |
| 95 | + { field: "Category", title: "Category", width: "180px", editor: categoryDropDownEditor, template: "#=Category.CategoryName#" }, |
| 96 | + { field: "UnitPrice", title:"Unit Price", format: "{0:c}", width: "130px" }, |
| 97 | + { command: "destroy", title: " ", width: "150px" }], |
| 98 | + editable: true |
| 99 | + }); |
| 100 | + }); |
| 101 | +
|
| 102 | + function categoryDropDownEditor(container, options) { |
| 103 | + $('<input required name="' + options.field + '"/>') |
| 104 | + .appendTo(container) |
| 105 | + .kendoDropDownList({ |
| 106 | + autoBind: false, |
| 107 | + dataTextField: "CategoryName", |
| 108 | + dataValueField: "CategoryID", |
| 109 | + dataSource: { |
| 110 | + type: "odata", |
| 111 | + transport: { |
| 112 | + read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Categories" |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | +
|
| 118 | + </script> |
| 119 | + </div> <script src="../content/shared/js/products.js"></script> |
| 120 | + <div id="example"> |
| 121 | + <div id="grid"></div> |
| 122 | + |
| 123 | + <script> |
| 124 | +
|
| 125 | + $(document).ready(function () { |
| 126 | + var dataSource = new kendo.data.DataSource({ |
| 127 | + pageSize: 20, |
| 128 | + data: products, |
| 129 | + autoSync: true, |
| 130 | + schema: { |
| 131 | + model: { |
| 132 | + id: "ProductID", |
| 133 | + fields: { |
| 134 | + ProductID: { editable: false, nullable: true }, |
| 135 | + ProductName: { validation: { required: true } }, |
| 136 | + Category: { defaultValue: { CategoryID: 1, CategoryName: "Beverages"} }, |
| 137 | + UnitPrice: { type: "number", validation: { required: true, min: 1} } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }); |
| 142 | +
|
| 143 | + $("#grid").kendoGrid({ |
| 144 | + dataSource: dataSource, |
| 145 | + filterMenuInit:function(e){ |
| 146 | + if(e.field === "Category"){ |
| 147 | + $(e.container).find('[type="reset"]').click(function(event){ |
| 148 | + var filters = e.sender.dataSource.filter().filters |
| 149 | + for (let i = 0; i <= filters.length; i++){ |
| 150 | + if (filters[i].field == "Category.CategoryName"){ |
| 151 | + filters.splice(i, 1); |
| 152 | + e.sender.dataSource.filter(filters) |
| 153 | + } |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | + }, |
| 158 | + pageable: true, |
| 159 | + filterable:true, |
| 160 | + filter:function(e){ |
| 161 | + if(e.field == "Category" && e.filter !== null){ |
| 162 | + var currentFilter = [] |
| 163 | + var filtersCount = e.filter.filters[0].length |
| 164 | + var value = e.filter.filters[0].value |
| 165 | + if(e.sender.dataSource.filter() !== undefined){ |
| 166 | + currentFilter = e.sender.dataSource.filter().filters // Retain the other filters |
| 167 | + } |
| 168 | + currentFilter.push({field:"Category.CategoryName", operatot:"eq", value:value}) |
| 169 | + e.preventDefault() |
| 170 | + e.sender.dataSource.filter(currentFilter) |
| 171 | + } |
| 172 | + }, |
| 173 | + height: 550, |
| 174 | + toolbar: ["create"], |
| 175 | + columns: [ |
| 176 | + { field:"ProductName",title:"Product Name" }, |
| 177 | + { field: "Category", title: "Category", width: "180px", editor: categoryDropDownEditor, template: "#=Category.CategoryName#" }, |
| 178 | + { field: "UnitPrice", title:"Unit Price", format: "{0:c}", width: "130px" }, |
| 179 | + { command: "destroy", title: " ", width: "150px" }], |
| 180 | + editable: true |
| 181 | + }); |
| 182 | + }); |
| 183 | +
|
| 184 | + function categoryDropDownEditor(container, options) { |
| 185 | + $('<input required name="' + options.field + '"/>') |
| 186 | + .appendTo(container) |
| 187 | + .kendoDropDownList({ |
| 188 | + autoBind: false, |
| 189 | + dataTextField: "CategoryName", |
| 190 | + dataValueField: "CategoryID", |
| 191 | + dataSource: { |
| 192 | + type: "odata", |
| 193 | + transport: { |
| 194 | + read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Categories" |
| 195 | + } |
| 196 | + } |
| 197 | + }); |
| 198 | + } |
| 199 | +
|
| 200 | + </script> |
| 201 | + </div> |
| 202 | +```` |
| 203 | + |
0 commit comments