KendoUI Multiselect Remove Multiple

1.1k views Asked by At

I am using Kendo UI Multiselect: http://demos.kendoui.com/web/multiselect/events.html

I have this Data:

var data =
    [
        { text: "Shirt-Black", value: "1" },
        { text: "Shirt-Brown", value: "2" },
        { text: "Shirt-Blue", value: "3" },
        { text: "Cap-Green", value: "4" },
        { text: "Cap-Red", value: "5" },
        { text: "Cap-White", value: "6" },
        { text: "Jacket-Denim", value: "7" }
    ];

Now I want that if I select "Shirt-Brown" then rest entries for shirt i.e. "Shirt-Black" and "Shirt-Blue" should not appear in the list which means that the user should not be able to choose the Shirt of two colors.

Similarly, If a "Cap" of any color has been chosen then user should not be able to choose the "Cap" of any other color.

Is there any way to achieve this?

1

There are 1 answers

0
Jarosław Kończak On

This is not build-in functionality. You can't even use dataSource filter() method because it will remove selected items from list as well.

However, this code will do what you're asking:

$("#select").kendoMultiSelect({
    ...
    change: function(e) {
      var dataItems = e.sender.dataItems();
      var categories = [];

      for(var i = 0; i < dataItems.length; i++){
        var category = dataItems[i].text.substring(0, dataItems[i].text.indexOf('-'));
        categories.push(category);
      }

      e.sender.ul.find('li').each(function(index, value){
        var $li = $(value);
        var hidden = false;
        for(var i = 0; i < categories.length; i++){
          var category = categories[i];
          if ($li.text().match("^" + category)){
            $li.css('display', 'none');
            hidden = true;
          }
        }
        if(!hidden){
          $li.css('display', 'list-item');
        }
      });
    }
});

Working KendoUi Dojo: http://dojo.telerik.com/AGisi