I have generated records and each row has delete button, And when I click delete button it will delete the records from the db. Also, after delete it will reload dataTable. Any Help for this?
我已生成记录,每行都有删除按钮,当我单击删除按钮时,它将删除数据库中的记录。此外,删除后它将重新加载dataTable。对此有何帮助?
DataTables:
var table = $('#table').DataTable({
"processing": true,
//some settings?
});
jQuery:
$(document).on('click', '[id^="delete-product-"]', function() {
var id = this.id.split('-').pop();
$.ajax({
type: 'post',
url: 'my_controller/delete_product',
dataType: 'json',
data: {id: id},
success: function(callback)
{
//What should I code here, that can't reload entire page, only the table
//after deleting records
},
error: function(status)
{
console.log(status);
}
});
});
Any help would be appreciated. Thank you in Advance
任何帮助,将不胜感激。先谢谢你
1 个解决方案
#1
7
You don't have to reload whole table data, just remove the deleted row.
您不必重新加载整个表数据,只需删除已删除的行。
$(document).on('click', '[id^="delete-product-"]', function() {
var $button = $(this);
var id = this.id.split('-').pop();
$.ajax({
type: 'post',
url: 'my_controller/delete_product',
dataType: 'json',
data: {id: id},
success: function(callback)
{
table.row( $button.parents('tr') ).remove().draw();
},
error: function(status)
{
console.log(status);
}
});
});
#1
7
You don't have to reload whole table data, just remove the deleted row.
您不必重新加载整个表数据,只需删除已删除的行。
$(document).on('click', '[id^="delete-product-"]', function() {
var $button = $(this);
var id = this.id.split('-').pop();
$.ajax({
type: 'post',
url: 'my_controller/delete_product',
dataType: 'json',
data: {id: id},
success: function(callback)
{
table.row( $button.parents('tr') ).remove().draw();
},
error: function(status)
{
console.log(status);
}
});
});