Kendo Grid Multiselect filter, selected values are not displayed if more than 1

1 Answer 668 Views
Filter Grid MultiSelect
Jan-Frederik
Top achievements
Rank 1
Iron
Iron
Jan-Frederik asked on 14 Feb 2023, 07:49 AM | edited on 14 Feb 2023, 01:57 PM

Hi, I recently created a grid with a multiselect filter and noticed that the selected values are not showing if the user selects more than one item and then filters. I could reproduce the behaviour in a short dojo script which comes from the telerik examples.

Is there any reason for this behaviour or did I forget something that makes the filter control fail? If this is a bug, is there a workaround?

In the example, the field that contains the filter is "Name" (2nd column).

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2023.1.117/styles/kendo.default-ocean-blue.min.css">
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2023.1.117/js/angular.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2023.1.117/js/jszip.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2023.1.117/js/kendo.all.min.js"></script>
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
    <div id="grid"></div>
    <script>
      var data = [
        { id: 1, name: "Fred", key: 1, value: "Green" },
        { id: 2, name: "Jed", key: 11, value: "Jorgensen" },
        { id: 3, name: "Red", key: 2, value: "Blah" },
        { id: 4, name: "Ted", key: 23, value: "Bleh" },
        { id: 5, name: "Ed", key: 3, value: "Toast" },
        { id: 6, name: "Zed", key: 4, value: "Smith" },
        { id: 7, name: "Ed", key: 41, value: "Johnson" }
      ];

      $(function() {
        var names = _.sortBy(_.uniq(_.pluck(data, "name")), function(n) { return n; });

        function createMultiSelect(element) {         
          element.kendoMultiSelect({
            dataSource: names
          });
        }

        var dataSource = new kendo.data.DataSource({
          data: data,
          schema: {
            model: {
              fields: {
                id: { type: "number" },
                name: { type: "string" },
                key: { type: "number" },
                value: { type: "string" }
              }
            }
          }
        });

        var grid = $("#grid").kendoGrid({
          dataSource: dataSource,
          sortable: true,
          filterable: true,
          columns: [
            {field: "id", title: "Id"},
            {
              field: "name",
              title: "Name",
              filterable: {
                ui : createMultiSelect,
                extra: false
              }
            },
            {field: "key", title: "Key"},
            { field: "value", title: "Value"}
          ]
        });
      });
    </script></body>
</html>

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 16 Feb 2023, 02:53 PM

Hello, Jan-Frederik,

I suppose you came across our How-To article on the matter. You can see a change event handler which is missing in the code you shared.

change: function(e) {
              var filter = { logic: "or", filters: [] };
              var values = this.value();
              $.each(values, function(i, v) {
                filter.filters.push({field: "name", operator: "eq", value: v });
              });
              console.log(this.dataSource.data());
              dataSource.filter(filter);
            }

Here is a complete example for reference.

Let me know how that works for you.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Jan-Frederik
Top achievements
Rank 1
Iron
Iron
commented on 22 Feb 2023, 11:55 AM

Hi Martin, thanks for your answer. I checked your example, but even with the change event, there is never more than one selected item showing up in the multiselect. F.ex. select "Ed", then "Fred" from the select -> the list is correctly filtered, but the multiselect shows only "Ed" as selected.
Martin
Telerik team
commented on 22 Feb 2023, 02:51 PM

Hello, Jan-Frederik, my apologies for missing the issue. There appears to be another line missing in the createMultiSelect function that changes the expected behaviour from the How-To article. 

function createMultiSelect(element) {    
          element.removeAttr("data-bind");
          element.kendoMultiSelect({
            dataSource: names,
            change: function(e) {
              var filter = { logic: "or", filters: [] };
              var values = this.value();
              $.each(values, function(i, v) {
                filter.filters.push({field: "name", operator: "eq", value: v });
              });
              console.log(this.dataSource.data());
              dataSource.filter(filter);
            }
          });
        }

Jan-Frederik
Top achievements
Rank 1
Iron
Iron
commented on 23 Feb 2023, 09:53 AM

Hello Martin, thank you, it works now. But I wonder, is it possible to achieve the expected behaviour without these modifications? Meaning without element.removeAttr and the change event? I would rather keep the default look and feel of the grid (filter button).
Martin
Telerik team
commented on 28 Feb 2023, 08:33 AM

Hello, Jan-Frederik, could you please elaborate what you mean by "default look and feel"? If you mean keeping the AND/OR operators as well as the second input, those are rather useless when using a MultiSelect as filter. Currently we do not have any other approach to achieve the behaviour, but we never had issue reports from users so far.
Tags
Filter Grid MultiSelect
Asked by
Jan-Frederik
Top achievements
Rank 1
Iron
Iron
Answers by
Martin
Telerik team
Share this question
or