|
| 1 | +--- |
| 2 | +title: Display Different Filter Editors Depending on Operators |
| 3 | +page_title: Display Different Editors Depending on Operators - Kendo UI for jQuery Filter |
| 4 | +description: "Learn how to display different editors depending on the operator in the Kendo UI Filter for jQuery." |
| 5 | +slug: filter-different-editor-depending-on-operators |
| 6 | +tags: filter, editor, operator |
| 7 | +component: filter |
| 8 | +type: how-to |
| 9 | +ticketid: 1616662 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +## Environment |
| 15 | + |
| 16 | +<table> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>Progress® Kendo UI® Filter for jQuery</td> |
| 20 | + </tr> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +How can I have different editors appear depending on the current operator of the Filter? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +1. Bind the [`change`](/api/javascript/ui/filter/events/change) event to the `filterModel` of the component and identify the exact `Filter expression` from its items. |
| 30 | +1. Find the `editor` of the expression, empty it and then append a new `input` element. |
| 31 | +1. Check for the current operator of the `Filter expression` and initialize the corresponding component for the editor in the input element you appended. |
| 32 | + |
| 33 | +The following example demonstrates how to achieve the desired scenario: |
| 34 | + |
| 35 | +```dojo |
| 36 | + <script src="../content/shared/js/products.js"></script> |
| 37 | + <div id="example"> |
| 38 | + <div id="filter"></div> |
| 39 | + <br /> |
| 40 | + <br /> |
| 41 | + <br /> |
| 42 | +
|
| 43 | + <script> |
| 44 | + $(document).ready(function () { |
| 45 | + var dataSource = new kendo.data.DataSource({ |
| 46 | + pageSize: 20, |
| 47 | + data: products, |
| 48 | + autoSync: true, |
| 49 | + schema: { |
| 50 | + model: { |
| 51 | + id: "ProductID", |
| 52 | + fields: { |
| 53 | + ProductID: { editable: false, nullable: true }, |
| 54 | + ProductName: { validation: { required: true } }, |
| 55 | + Category: { defaultValue: { CategoryID: 1, CategoryName: "Beverages" } }, |
| 56 | + UnitPrice: { type: "number", validation: { required: true, min: 1 } } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | +
|
| 62 | + $("#filter").kendoFilter({ |
| 63 | + dataSource: dataSource, |
| 64 | + expressionPreview: true, |
| 65 | + fields: [ |
| 66 | + { name: "ProductName", label: "Product Name" }, |
| 67 | + { name: "CategoryID", type: "number", label: "Category", defaultValue: 1, editorTemplate: categoryDropDownEditor }, |
| 68 | + { name: "UnitPrice", type: "number", label: "Unit Price", editorTemplate: unitPriceEditor }, |
| 69 | + { name: "UnitsInStock", type: "number", label: "Units In Stock" }, |
| 70 | + { name: "QuantityPerUnit", label: "Quantity Per Unit" }, |
| 71 | + ] |
| 72 | + }); |
| 73 | +
|
| 74 | + $("#filter").data("kendoFilter").filterModel.bind("change", function(e) { |
| 75 | + if(!e.items) { |
| 76 | + return; |
| 77 | + } |
| 78 | +
|
| 79 | + let model = e.items[0]; |
| 80 | + if(model.field == "ProductName") { |
| 81 | +
|
| 82 | + setTimeout(function(){ |
| 83 | + var editorContainer = $("[id='"+model.uid+"']").find(".k-toolbar-item.k-filter-value"); |
| 84 | + editorContainer.empty(); |
| 85 | + let input = $("<input />") |
| 86 | + .appendTo(editorContainer); |
| 87 | +
|
| 88 | + if(model.operator == "eq" || model.operator == "neq") { |
| 89 | + input.kendoDropDownList({ |
| 90 | + optionLabel: "Select value...", |
| 91 | + dataSource: ["1", "2"], |
| 92 | + value: model.value, |
| 93 | + change: function(e) { |
| 94 | + model.set("value", e.sender.value()); |
| 95 | + } |
| 96 | + }); |
| 97 | +
|
| 98 | + } else { |
| 99 | + input.kendoTextBox({ |
| 100 | + value: model.value, |
| 101 | + change: function(e) { |
| 102 | + model.set("value", e.sender.value()); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + }) |
| 107 | +
|
| 108 | + } |
| 109 | + }) |
| 110 | + }); |
| 111 | +
|
| 112 | + function unitPriceEditor(container, options) { |
| 113 | + $('<input data-bind="value: value" name="' + options.field + '"/>') |
| 114 | + .appendTo(container) |
| 115 | + .kendoNumericTextBox(); |
| 116 | + } |
| 117 | +
|
| 118 | + function categoryDropDownEditor(container, options) { |
| 119 | + $('<input data-bind="value: value" name="' + options.field + '"/>') |
| 120 | + .appendTo(container) |
| 121 | + .kendoDropDownList({ |
| 122 | + dataTextField: "CategoryName", |
| 123 | + dataValueField: "CategoryID", |
| 124 | + dataSource: { |
| 125 | + type: "odata", |
| 126 | + transport: { |
| 127 | + read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Categories" |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + </script> |
| 133 | + </div> |
| 134 | +``` |
| 135 | + |
| 136 | +## See Also |
| 137 | +* [JavaScript API Reference of the Filter](/api/javascript/ui/filter) |
0 commit comments