使用数据表删除特定行

问题描述 投票:0回答:1

我想从表中删除一行,这些行是使用服务器端处理添加的,并且我有一个删除按钮,其 id 是数据库中该行的 id。

单击删除按钮后,该行将从数据库中删除,但不会从表中删除。另外,对于整行,这里还有一个指定的

id = "row_"
行 id。

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

$('#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();

更改分页时,它会删除行,所以我该怎么做,有刷新或服务器端的东西吗?

jquery datatables delete-row
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.