使用数据表删除特定行

时间:2018-08-29 07:32:09

标签: jquery datatables delete-row

我想从表中删除行,使用服务器端处理添加行,并且我有一个删除按钮,其ID为数据库中该行的ID。单击删除按钮后,该行将被删除数据库,但不是来自表。在整个行中,这里还分配了id = "row_"行ID。

enter image description here

使用以下代码,我如何也可以从表中删除行或淡出该行:

$('#users_table tbody').on( 'click', '.btn-delete', function () {
    var id = $(this).attr('id');
    var rowid = '#row_'+id;
    var sure = confirm("Are you sure you want to delete this user?");
    if (sure == true){
        $.ajax({
            type: "POST",
            url  : '../core/ajaxpdo.class.php',
            data: 'delete_user_id=' + id,
            success: function (response) {
                if (response=='ok'){


                }else{
                    operation_error();
                }
            },
            error: function() {
                alert('Something went wrong!');
            }
        });
        return false;
    }       
});

尝试成功执行功能,但没有满足我的要求(它是从数据库中删除,而不是从表中删除)

$('#users_table').dataTable().fnDeleteRow(rowid);

var linha = $(this).closest('tr');
linha.fadeOut(400, function () {
    tabelaP.row(linha).remove().draw()
});

var table = $('#users_table').dataTable();
table.fnDeleteRow( table.$(rowid)[0] ).draw();

在更改分页时,它清除了行,所以我该怎么做才能刷新服务器端内容?

1 个答案:

答案 0 :(得分:0)

尽管经过很多尝试,删除行,ajax重新加载均不起作用,因为我正在使用PIPELINE数据。因此,为了刷新ajax而不会丢失分页,我必须clear the pipeline cache然后重新加载当前数据表页面运行完美:

// CLEARING THE CACHE
$("#users_table").DataTable().clearPipeline();

// RELOAD CURRENT DATATABLES PAGE WITHOUT LOSING PAGINATION
$("#users_table").DataTable().ajax.reload(null, false );

由于删除后数据将刷新,因此无需删除特定行!

更新:

用于删除特定的tr with specific id

var id = 'row id here';
var rowid = '#row_'+id;
$("#users_table tbody").find(rowid).remove();