SelectPicker: How to get json value and display inside dropdownlist as checked option?

1 answer

How to get values from JSON and display these values as checked options inside selectpicker using ajax success response? I'm try to use selectpicker $('.selectpicker').selectpicker('val', ['values']) val method to get values and display as checked option.

To describe more clear, below are two tables: color_tbl table and cars_tbl table:

color_tbl

colorID | color    |  -------------------- 01      | White    | 02      | Gray     | 03      | Black    | 04      | Red      | -------------------- 

cars_tbl

carID | car    | Color (Foreing Key) and select multiple --------------------------------------------------------- 01    | Ford   | 01, 02, 03 02    | GM     | 02, 03, 04 03    | BMW    | 01, 03, 04 --------------------------------------------------------- 

Below are the selectpicker method:

$ ('.selectpicker').selectpicker('val', ['values']); 

And below are the jquery function that execute a bootstrap modal, and inside this function, ajax receive data from php script and pass values in JSON format:

  $(document).on('click', '.update', function(){         var user_id = $(this).attr("id");          $.ajax({             url:"fetch_single.php",             method:"POST",             data:{user_id:user_id},             dataType:"json",             success:function(data)             {                  $('#userModal').modal('show');                 $('#car').val(data.car);                 $('.selectpicker').selectpicker('val', [data.color]);              }         })     }); 

As the code above, when modal is open, the input #car receive correct data but selectpicker dropdownlist #color dont receive data.

I tried to put [data.color] inside selectpicker val method but no success. I'm try to find a correct way to get json object value to put inside this method and display checked options related to color column from cars_tbl table.

how could i do this?

All answers to this question, which has the identifier 61180390

The best answer:

I found a solution to solve my doubt. To turn a comma delimited string in to an array, i used 'split()' into selectpicker val method as below:

$('.selectpicker').selectpicker('val', data.color.split(',')); 

and it works now.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data