I have problem in selectize JS when set current value is multiple value. I set from Ajax response in Json format, here is my code.
$(".rule_list").on("click",function(e) {
e.preventDefault();
$.ajax({
url: 'getruledata,
dataType: 'json'
})
.done(function(data){
console.log(data);
$selectz[0].selectize.setValue(data[0].control_country);
})
});
Here my HTML code
<select id="select-country" placeholder="Pick a countries..."></select>
And here code for selectize
var $selectz = $('#select-country').selectize({
maxItems: null,
valueField: 'iso',
labelField: 'nice_name',
searchField: 'nice_name',
options: {!! $country !!},
create: false,
});
Here my value format from Json response
[{"id":2,"name":"XSA 2","user_id":"3","control_device":"Mobile","control_country":"US,CA","offer_id":"2","rule_id":"1","status":"2"}]
I'm stuck in this steps, if "control_country":"US,CA"
(multiple value) not working when set current value to input form, but if "control_country":"US"
(single value) working correctly
This post will discuss how to get the selected values in a multi-select dropdown in plain JavaScript. We can select the multiple options in the dropdown list using the multipleattribute. There are several ways in JavaScript to return the selected values in a multi-select dropdown. 1. Using for…ofstatement
To set a selected option you need to use setValue if the selectize is a dropdown and you want to use it as a select dropdown. Suppose that you already have the preselected value and the selectize componente is already built and load all the values.
If multiple items can be selected with a "select" input tag (e.g. <select multiple> ), this returns an array. Otherwise, returns a string (separated by delimiter if "multiple"). Resets the selected items to the given value. Moves the caret to the specified position ( index being the index in the list of selected items).
You have to select your selectize first with input [0].selectize and then use the native method getValue () of selectize. Based on your fiddle this should work :
You can set multiple values to selectize like this:
$selectz[0].selectize.setValue([optionid,optionid]);
So in your example it should be:
$selectz[0].selectize.setValue(["US","CA"]);
You need to use the method addItem(value, silent)
. As explained in the docs, addItem "selects" items, where passing true to "silent" will apply the change to the original input.
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