I have build a filter in my DataTable so I can check for the status. I am using the following code to retrieve the status:
if(isset($_POST['status']))
{
if (!empty($where) ) {
$where .= "AND status = '". $_POST['status'] ."'";
} else {
$where .= "WHERE status = '". $_POST['status'] ."'";
}
}
else{
if (!empty($where) ) {
$where .= "AND status = '1'";
} else {
$where .= "WHERE status = '1'";
}
}
I have no issues with selecting the data. When I look at the WebConsole I can see that the script is posting the right data en gets the right response.
But I have some issues with the presentation of the data.
When the response is correct I want to update my DataTable.
I am using the following code for updating my datatable:
success:function(data){
$('#tb1').DataTable(data);
}
When I execute this code I get a DataTables warning:
DataTables warning: table id=tb1 - Cannot reinitialise DataTable.
I cant figure out whats wrong with my script. Looking to multiple examples the script should work. Does someone know what is wrong with my script and how I can solve this problem?
Here is my full script:
<script type="text/javascript">
$( document ).ready(function() {
$('#tb1').DataTable({
"bprocessing": true,
"serverSide": true,
"ajax": {
"url": "./Response1.php",
"type": "POST",
"error": function(){
$("#grid_processing").css("display","none");
}
}
});
$("div.toolbar1").html('<select id="status" type="status"><option value="1">Active</option><option value="0">Inactive</option></select>');
$("#status").on('change',function () {
var val = $(this).val();
$.ajax({
url: './Response1.php',
type: 'POST',
data: 'status='+val,
success:function(data){
$('#tb1').DataTable(data);
}
});
});
});
</script>
Updated with explanation and references
There is no need separate Ajax request. Stick with Datatables Ajax option is enough.
We can use Datatables ajax.data option to
add additional data
to the request, orto modify the data object
being submitted to server if required.To work with
new and refresh data input
we need to useajax.data as a function
otherwise it will initialized as a static object which will evaluated only once.And then use Datatables ajax.reload() to reload the table data from the Ajax data source within your event calls.
Possible ways to update data request using Datatables ajax.data is by:
Sample demo:
Try this
Add this after serverside: true,
to destroy the table after its initialised
Try using a single call
Call function from anywhere like this