Moch Siswanto
Moch Siswanto

Reputation: 83

Set current multiple value in selectize JS

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

Upvotes: 7

Views: 9049

Answers (2)

Alex
Alex

Reputation: 2775

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"]);

Upvotes: 7

mstrlaw
mstrlaw

Reputation: 79

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.

Upvotes: 1

Related Questions