How to set data values on Kendo Multi Select?

Rodney picture Rodney · Apr 19, 2013 · Viewed 50.1k times · Source

I'm using a Kendo Multi Select. I want to load selected values into the multi select. How do you set the data values in Java Script? I have the following script:

$('#selectedFilters').kendoMultiSelect({
                    dataSource: data,
                    dataTextField: 'name',
                    dataValueField: 'value',
                    filter: 'contains',
                    placeholder: "Add Filter",
                    delay: 0,
                    minLength: 2,
                    highlightFirst: true,
                    ignoreCase: true,
                    change: function (event) {
                        applyFilters();
                    },
                });

Answer

OnaBai picture OnaBai · Apr 19, 2013

You can use value() method for setting the values.

Example, give the following HTML:

<a href="#" id="button" class="k-button">Select</a>
<input id='selectedFilters'>

and the JavaScript:

var data = [
    { name : "name1", value : "value1" },
    { name : "name2", value : "value2" },
    { name : "name3", value : "value3" },
    { name : "name4", value : "value4" },
    { name : "name5", value : "value5" },
    { name : "name6", value : "value6" }
];
var multiselect = $('#selectedFilters').kendoMultiSelect({
    dataSource    : data,
    dataTextField : 'name',
    dataValueField: 'value',
    filter        : 'contains',
    placeholder   : "Add Filter",
    delay         : 0,
    minLength     : 2,
    highlightFirst: true,
    ignoreCase    : true,
    change        : function (event) {
        console.log("change");
    }
}).data("kendoMultiSelect");

$("#button").on("click", function () {
    console.log("multiselect", multiselect);
    multiselect.value(["value1", "value2", "value6"]);
});

If you click on button the multi-value input gets: name1, name2 and name6.

EDIT If you want to add to current selected values then do:

$("#button").on("click", function () {
    var selected = multiselect.value();
    var res = $.merge($.merge([], selected), ["value1", "value2", "value6"]);
    multiselect.value(res);
});

NOTE: For some sort of reason you cannot reuse selected array for setting new values, so you should create a new one.

Check it running here