How can I refresh DataTable after an ajax success?
To successfully reload the table
table.ajax.reload();//where the table variable is the variable that holds the DataTable itself
Example
var table = $('#giacenza').DataTable({
//ajax call that fetches data from the database.
});
Another idea would be to have a javascript function which gets the record from the database. After getting success on deleting from the database, call the function again.
Example
function getRecords(){
//this gets the full list from the database on document ready
//make Ajax call to retrieve from database
}
if(result == 'OK'){
//its been successfully deleted
getRecords();//call the js function that gets from database again
}
EDIT
You are using mysqli. This API supports prepared statements. Please do use that..
$query = "UPDATE records SET PARESTALLA = PARESTALLA-1 WHERE CODBARRA = ?";
$result = $mysqli->prepare($query);
$result->bind_param('s', $codbarra);
$result->execute();
echo $result->affected_rows > 0 ? 'OK' : 'KO';
how to refresh datatables after ajax call
Edit: try this (For - datatable version 1.10.9)
$('#formdeposit').submit(function(e){
e.preventDefault();
if (grecaptcha.getResponse()) {
var formData = {
"_token": "{{ csrf_token() }}",
'deposit' : $('#deposit').val(),
'coin' : $('#coin option:selected').val()
};
$.ajax({
type : 'POST',
url : '{{ asset("/member/deposit/process") }}',
data : formData,
dataType : 'json',
encode : true
})
.done(function(data){
console.log(data);
$('#myModal').modal('show');
//
if (data.pesan=='ok')
{
$('#test').html(data.html);
}
else{
$('#test').html(data.pesan);
}
var table = $('#transaction_data').DataTable();
table.ajax.reload( null, false );
});
e.preventDefault();
}
else{ alert('Please Confirm The Captcha') }
});
how to refresh datatables after ajax response
Instead of using jQuery Ajax to populate your DataTable, why don't you use the built-in Ajax function? It's a lot simpler. Follow this example to define your DataTable, then when you want to reload inside your button click handler do:
VendorClient.ajax.reload();
Or if you want to change the url or search parameters do:
VendorClient.ajax.url('new-url-here').load();
If you have options that you want to send with your ajax url you can define it as an object.
By doing it this way you don't have the problems of recreating your DataTable each time.
Related Topics
Process a Dictionary and Return Each Entry in React
Angular 6 Store Data from Json Array Inside Json
Special Character Validation Using JavaScript
How to Check If Element Has Focused Child Using JavaScript
How to Replace a String With Square Brackets Using JavaScript Replace Function
Bootstrap Modal Hide Not Working With Jquery Ajax
Change the Arrow Buttons in Slick Slider
How to Move the Mouse Pointer and Click With Selenium Webdriver JavaScript
How to Add and Remove Class to Menu Items on Mouseover
Google Charts - Labels Are Not Showing
How to Override the Onbeforeunload Dialog and Replace It With My Own
Redirecting to Another Page With JavaScript and Jsp
Chart.Js V2 - Hiding Grid Lines
How to Add Space Between Every 4 Characters in JavaScript
Resource Blocked Due to Mime Type Mismatch (X-Content-Type-Options: Nosniff)
Bootstrap Carousel: How to Stop Auto Sliding on Large Screen