2

如何替换外部 .js 文件中的 Kendo UI MultiSelect 数据源值...

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

索引.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 (外部)文件替换如下......

初始化.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 和 3,我收到错误消息

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

但是,它仍然从index.html文件中获取值

4

1 回答 1

2

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

<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

于 2016-01-19T11:28:10.440 回答