Kendo UI - MultiSelect从外部文件

时间:2016-01-19 11:00:36

标签: jquery kendo-ui kendo-dropdown kendo-multiselect

如何从外部.js文件中替换Kendo UI MultiSelect dataSource值...

PS :实际值来自index.html文件,如下所示:

的index.html

<select multiple="multiple" class="city_fields initi-select-218" id="test">
</select>

var data = [
     "New Jersey",
     "San Francisco",
     "Las Vegas",
     "Chicago",
     "Los Angeles",
     "New York"
];

jQuery(".city_fields").kendoMultiSelect({
  dataSource: data,
  filter: "contains",
});

以上值我试图从init.js(外部)文件替换如下...

init.js

尝试选项1:

var newData = ["Delhi",  "Bangalore"];
jQuery(".initi-select-218").kendoMultiSelect({
    dataSource: newData,
    filter: "contains",
    placeholder: "Select",
});

尝试过选项2:

var newData = ["Delhi", "Bangalore"];
$("#test").kendoMultiSelect({
    dataSource: newData,
    filter: "contains",
    placeholder: "Select",
}); 
$("#test").setDataSource(new kendo.data.DataSource({ data: newData }));

尝试过选项3:

var newData = ["Delhi",  "Bangalore"];
var multiselect = $("#test").data("kendoMultiSelect");
multiselect.setDataSource(new kendo.data.DataSource({ data: newData }));

对于选项2&amp; 3,我收到错误

Uncaught TypeError: $(...).setDataSource is not a function ...

但是,它仍然是从index.html file

获取值

1 个答案:

答案 0 :(得分:1)

这是因为你有css类的jquery选择器。 kendo multiselect也将相同的类应用于包装器。

<div class="k-widget k-multiselect k-header city_fields initi-select-218" unselectable="on" title="" style="">
    ...
    <select multiple="multiple" class="city_fields initi-select-218" data-role="multiselect" aria-disabled="false" aria-readonly="false" style="display: none;">
        ...
    </select>
</div>

改为使用id属性:

<select id="multiselect" multiple="multiple" class="city_fields initi-select-218"></select>
<script>
    $("#multiselect").kendoMultiSelect({...});
    var multiselect = $("#multiselect").data("kendoMultiSelect");
    ...
</script>

更新工作示例:http://dojo.telerik.com/iHoDU/2