jquerydatatablesdelete-row

Removing specific row using DataTables


I want to delete a row from my table, the rows are added using server side processing and I have a delete button whose id is id of the row in the database.

On clicking the delete button, the row is getting deleted from database but not from table. Also to the whole row there is an assigned id = "row_"row id here.

enter image description here

Using the following code, how can i delete the row from the table also or fade it out:

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

TRIED FOLLOWING IN SUCCESS FUNCTION BUT NONE OF THEM FULFILLS MY DEMAND(it is deleting from the db but not from table)

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

ON CHANGING THE PAGINATION IT REMOVES THE ROW SO HOW CAN I DO THAT IS THERE AY REFRESH OR SOMETHING FOR SERVER SIDE?


Solution

  • Although after lots of trying ,the delete row ,ajax reload was not working as i was using PIPELINE data.So in order to refresh the ajax without losing pagination,i have to clear the pipeline cache and then reloading the current datatables page works perfectly:

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

    And no need to delete the specific row as data is getting refreshed after delete!

    UPDATE:

    For deleting a specific tr with specific id :

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