简体   繁体   中英

Having issue deleting a html table row using jquery dataTable

I have a html table with data inside from the database.

I want to do is remove a row from the html table but not in the database.

In my action column i have a delete button in every row and if i press delete on that specific row that row should be remove.

I want to remove a specific row in the html table but not the actual data from the database.

script:

$(document).ready(function(){
$.ajax({
url: 'process.php',
type: 'post',
data: {tag: 'getData'},
dataType: 'json',
success: function(data){
        if(data.success){
            $.each(data, function(index, record){
                if($.isNumeric(index)){
                    var row = $("<tr />");
                    $("<td />").text(record.name).appendTo(row);
                    $("<td />").text(record.age).appendTo(row);
                    $("<td />").text(record.gender).appendTo(row);
                    $("<td />").html(record.action).appendTo(row);
                    row.appendTo("#myTable2 tbody");
                }
            })
            }
            $('#myTable2').dataTable({
            "bjQueryUI": true,
            "sPaginationType": "full_numbers"
            });

            $("#myTable2 .dltRow").bind( "click", function(event) {
            var target_row = $(this).closest("tr").get(0);
            var aPos = oTable.fnGetPosition(target_row); 
            oTable.fnDeleteRow(aPos);
            });

        }

});

});

It would be easier to bind your event in your each function, when you have a reference to the row you just created:

$.each(data, function(index, record){
    if($.isNumeric(index)){
        var row = $("<tr />");
        $("<td />").text(record.name).appendTo(row);
        $("<td />").text(record.age).appendTo(row);
        $("<td />").text(record.gender).appendTo(row);

        var $actionCell = $("<td />");
        $actionCell.html(record.action)
        $actionCell.find('.dltRow').on("click", 
            function() { row.remove(); });
        $actionCell.appendTo(row);
        row.appendTo("#myTable2 tbody");
    }
})

Or you could keep it in your bind call and use the remove method there:

$("#myTable2 .dltRow").bind( "click", function(event) {
    $(this).closest("tr").remove();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM