dontbannedmeagain
dontbannedmeagain

Reputation: 480

Kendo MultiSelect make value to be selected and disable

Need help here. I create a simple demo here and what I want to achieve from dataBound if checked='yes' node is selected and disable(apply k-state-disable) from edit. I try to set (selected,true) & (disabled,true) but seem it not working.

DEMO IN DOJO

<select id="multiselect"></select>

$("#multiselect").kendoMultiSelect({
    dataSource: {
    data: [
      {id:1, Name: "John 1", checked: 'no'},
      {id:2, Name: "John 2", checked: 'yes'},
      {id:3, Name: "John 3", checked: 'no'},
      {id:4, Name: "John 4", checked: 'yes'},
      {id:5, Name: "John 5", checked: 'no'},
      {id:6, Name: "John 6", checked: 'no'}
    ]
  },
  dataTextField: "Name",
    dataValueField: "id", 
    dataBound: function(e) {
    var multiselect = $("#multiselect").data("kendoMultiSelect");
    var x = multiselect.dataSource.view();

    for (var i = 0; i < x.length; i++) {
      if (x[i].checked == "yes") {
        //x[i].set("selected", true);
        //x[i].set("disabled ", true);
        //x[i].prop("disabled", true).addClass("k-state-disabled");
      } 
    }
  },

});

Upvotes: 1

Views: 3726

Answers (2)

tata.leona
tata.leona

Reputation: 1058

You need to handle select and deselect events:

    function onDeselect(e) {
        if (e.dataItem.checked == 'yes') {
            e.preventDefault();
        }
    }  



    function onSelect(e) {
        if(e.dataItem.checked == 'yes') {
            e.preventDefault();
        }
    };  

$("#multiselect").kendoMultiSelect({
  select: onSelect,
  deselect: onDeselect,
  //...
});

working demo

Upvotes: 0

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

I want to suggest another way for achieve that. Avoid changing DOM in dataBound whenever possible. So I would like to suggest using itemTemplate option and select event:

You can apply .k-state-disabled class within the itemTemplate option:

itemTemplate: '<span # if (data.checked === "yes") { #class="k-state-disabled"# } #>#: Name #</span>'

That will make the option look like disabled. But it is still possible to select it in the list, so you can use select event to prevent that:

select: function(e) {
    if (e.dataItem.checked === 'yes') {
        e.preventDefault();
    }
},

Using e.preventDefault() inside that event will prevent user from selecting the option which matches the condition.

Updated Demo

Upvotes: 1

Related Questions