Set current multiple value in selectize JS
Asked Answered
R

2

7

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

Rancell answered 20/2, 2017 at 14:40 Comment(0)
L
7

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"]);
Lexicologist answered 19/1, 2018 at 8:31 Comment(0)
C
1

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.

Care answered 17/4, 2017 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.