删除记录/数据后如何重新加载dataTable?

时间:2016-11-16 08:25:01

标签: jquery ajax codeigniter datatables

我已生成记录,每行都有删除按钮,当我单击删除按钮时,它将删除数据库中的记录。此外,删除后它将重新加载dataTable。对此有何帮助?

数据表:

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);
        }
    });
});

任何帮助将不胜感激。提前谢谢

1 个答案:

答案 0 :(得分:11)

您不必重新加载整个表数据,只需删除已删除的行。

$(document).on('click', '[id^="delete-product-"]', function() {
    var $button = $(this);
    var id = this.id.split('-').pop();
    var table = $('#table_id_selector').DataTable(); // replace with your table id 
    selector
    $.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);
        }
    });
});
相关问题