How to create a dropdown list with the option from phpmyadmin in the datatables editable column
How to create a dropdown list with the option from phpmyadmin in the datatables editable column
Hi, I am currently working on a search function that fetch the data and show it in a table that i created using datatables.net template. From the edit option and i would like to create a dropdown list that fetch the data from a table in the database and show it as an option in the datatable editable column.
The current javascript is as below : -
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
var dataTable = $('#sample_data').DataTable({
"processing" : true,
"serverSide" : true,
"order" : [],
"ajax" : {
url:"fetch.php",
type:"POST"
}
});
$('#sample_data').on('draw.dt', function(){
$('#sample_data').Tabledit({
url:'action.php',
dataType:'json',
columns:{
identifier : [0, 'serial_number'],
editable:[[1, 'asset_number'], [2, 'category', '{"1":"Male","2":"Female"}'], [3, 'brand'],[4, 'model'], [5, 'year'], [6, 'price'],[7, 'status'], [8, 'vendor'], [9, 'invoice_no'],
[10, 'DO_no'], [11, 'memo_no'], [12, 'PO_no'],[13, 'User_now'], [14, 'user'], [15, 'department']]
},
restoreButton:false,
onSuccess:function(data, textStatus, jqXHR)
{
if(data.action == 'delete')
{
$('#' + data.id).remove();
$('#sample_data').DataTable().ajax.reload();
}
}
});
});
});
</script>
from the code above it only can display a dropdown list that is typed in such as : -
[2, 'category', '{"1":"Male","2":"Female"}']
I would want to fetch data from table in the database and show it in a drop down list
I have created a php code to fetch data from table in database as an example : -
<select id="category" name="category">
<option disabled selected>-- Select Category --</option>
<?php
$records = mysqli_query($link, "SELECT DISTINCT ID,type From category"); // Use select query here
while($data = mysqli_fetch_array($records))
{
echo "<option value = '$data[ID]'>".$data[ID]."-".$data[type]."</option>"; // displaying data in option menu
}
?>
</select>
Hope you guys can help me. Thank you in advance
Answers
When you say editable column, are you using Editor, as in this example here with selects and radios?
Colin
Hi colin, as i know i didnt use editor as i refer to this website to create the table https://webslesson.info/2020/05/make-editable-datatable-using-jquery-tabledit-plugin-with-php-ajax.html
the table are as in below picture
OK, it would be worth asking on that page you linked to for support. Editor would make that process easier, so definitely worth considering,
Colin
Thank you colin.