【问题标题】:Removing specific row using datatables使用数据表删除特定行
【发布时间】:2019-02-03 21:58:08
【问题描述】:

我想从我的表中删除一行,这些行是使用服务器端处理添加的,我有一个删除按钮,其 id 是数据库中行的 id。单击​​删除按钮时,该行正在从中删除数据库,但不是来自表。对于整行,这里还有一个分配的id = "row_"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】:

    尽管经过大量尝试,删除行,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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2017-01-15
      • 2015-10-15
      相关资源
      最近更新 更多