Kendo MultiSelect make value to be selected and disable

不问归期 提交于 2020-05-31 10:08:07

问题


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");
      } 
    }
  },

});

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/61137386/kendo-multiselect-make-value-to-be-selected-and-disable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!