Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 482
Moch Siswanto Avatar asked Feb 20 '17 14:02

Moch Siswanto


People also ask

How to get the selected values in a multi-select dropdown in JavaScript?

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

How to set a selected option in a selectize component?

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.

How do I select multiple items in a list?

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).

How do I get the value of a selectize?

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 :


2 Answers

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"]);
like image 194
Alex Avatar answered Oct 25 '22 18:10

Alex


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.

like image 42
mstrlaw Avatar answered Oct 25 '22 18:10

mstrlaw