SelectPicker: How to get json value and display inside dropdownlist as checked option?
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?