Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind data to bootstrap select

json data:

["string 1","string 2","test 07 ","here is test"]

and this is html

  <select class="selectpicker" id="ddl_location">
    <option select="selected">Select Location </option>
  </select>

and here is JQuery code

 for (var i = 0; i < jsonData.length; i++) {
     $("#ddl_location").append('<option value="' + jsonData[i] + '">' + jsonData[i] + '</option>');
      }

the issue is that

data is not populated in drop-down list

like image 993
Saif Avatar asked Sep 20 '25 05:09

Saif


1 Answers

after working around the issue, the solution was :

 var jsonData = JSON.stringify(data);
     $.each(JSON.parse(jsonData), function (idx, obj) {
        $("#ddl_location").append('<option value="' + obj.id + '">' + obj.location + '</option>').selectpicker('refresh');
     });

step 1: stringify received object
step 2: use each function instead of for loop
step 3: parse the data using JSON.parse(jsonData )
step 4: refresh drop-down list every time you append data to drop-down list using .selectpicker('refresh')

like image 95
Saif Avatar answered Sep 22 '25 01:09

Saif