Reputation: 113
I have a problem to append a selected value option in a DataTable.
Here is my JavaScript code
var table = $('#tablesurvey').DataTable( {
"ajax": {
"url": '<?php echo site_url('data'); ?>',
"type": "POST"
},
"columns": [
{"data": "id_vote"},
{"data": "nama"},
{"data": "nik"},
{"data": "desa"},
{"data": "rt"},
{"data": "vote"}
],
"columnDefs": [ {
"targets": -1,
"render": function (data, type, row, meta){
return "<select class='pilihan form-control' id='pilihan'><option value='0'>--Pilihan--</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option</select>";
}
} ],
} );
Result before
Result what I want
How do I set the initial selected option for the rendered select box?
Upvotes: 2
Views: 6334
Reputation: 85578
You could build the <select>
programmatically, i.e step by step adding <option>
's and compare with data.vote
. Or you could use a more generic jQuery based solution for convenience :
render: function (data, type, row, meta){
var $select = $("<select class='pilihan form-control' id='pilihan'><option value='0'>--Pilihan--</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>");
$select.find('option[value="'+data.vote+'"]').attr('selected', 'selected');
return $select[0].outerHTML
}
Upvotes: 9