2

How can I replace the Kendo UI MultiSelect dataSource values from external .js file...

PS: Actual values are coming from index.html file like below:

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

Above values I am trying to replace from init.js (external) file as below...

init.js

Tried Option 1:

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

Tried Option 2:

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

Tried Option 3:

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

For Option 2 & 3, I am getting error as

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

But still, it is taking the values from index.html file

Reddy
  • 1,477
  • 29
  • 79

1 Answers1

2

It's because you have jquery selector by css class. kendo multiselect apply same classes to wrapper also.

<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>

Use id attribute instead:

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

Update working example: http://dojo.telerik.com/iHoDU/2

Gene R
  • 3,684
  • 2
  • 17
  • 27
  • Thanks **@Gene R**... but still it is not working... instead I am getting error as `Uncaught TypeError: Cannot read property 'setDataSource' of undefined` – Reddy Jan 19 '16 at 12:57
  • for below code: ` var newData = ["Delhi", "Bangalore"]; var multiselect = $("#test").data("kendoMultiSelect"); multiselect.setDataSource(new kendo.data.DataSource({ data: newData }));` – Reddy Jan 19 '16 at 12:58
  • @Reddy you need init multiselect first:`$("#test").kendoMultiSelect({...});` – Gene R Jan 19 '16 at 13:03
  • Hi **Gene R**... tried again without luck though... I have updated the question with Tried options... please help me out :( – Reddy Jan 20 '16 at 05:31
  • **@Gene R**... thanks... But the issue is: it is working great if both (data and newData) coming from same place, whereas it is not working if newData coming from external file :( – Reddy Jan 20 '16 at 13:04
  • @Reddy you need include your external js file after multiselect is initialized. I tested myself and it works. – Gene R Jan 20 '16 at 13:14
  • **@Gene R**.. yes, I am doing the same.. adding the external file just before `

    ` tag... relevant code is above of external file

    – Reddy Jan 20 '16 at 13:25