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();
},
});
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With