Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set data values on Kendo Multi Select?

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();
                    },
                });
like image 208
Rodney Hickman Avatar asked Apr 19 '13 20:04

Rodney Hickman


People also ask

How do I initialize multiselect in Kendo UI?

To initialize the MultiSelect by binding the widget to remote data arrays and then utilizing the <input> or the <select> element, use the Kendo UI Data Source. The Data Source component is an abstraction for local and remote data.

What is the use of the value method in multiselect?

Gets or sets the value of the MultiSelect. Important: If there are no items, the value method will pre-fetch the data before continue with the value setting. Important: The widget will clear the applied filter if a new value is set. Thus it ensures that the original/whole data set is available for selection.

How to persist the selected tag on consecutive selection in multiselect?

You can persist the selected tag on its consecutive selection, if the valueNormalizer returns null. For example, if the user types in small, the MultiSelect will select the Small item from its item list on Enter. If the user types in small again, the MultiSelect will not remove the already selected Small tag on Enter.

How do I configure the multiselect component to accept custom values?

To configure the MultiSelect to accept custom values, set the allowCustom property to true. If the component is bound to primitive values (strings, numbers, or other), set the allowCustom property to true.


1 Answers

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

like image 129
OnaBai Avatar answered Oct 10 '22 14:10

OnaBai