Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: deleting row in datatables

I'm using jquery datatables and I have some <tr> inside a table with the following structure:

<tr class="odd">
     <td class="  sorting_1">0</td>
     <td class=" ">2011</td>
     <td class=" ">20</td>
     <td class=" ">
         <span class="btn-group">
            <a class="del btn btn-small" href="#"><i class="icon-delete"></i></a>       
         </span>
     </td>
</tr>

I writed the following jquery code for deleting the row associated to the button I click on.

$(".del").bind("click", function(event){
        var target_row = $(this).parent().parent().parent();
        var aPos = oTable.fnGetPosition(target_row); // the error occurs here!
        oTable.fnDeleteRow(aPos);
          });

but I obtain an error like this:

"TypeError: a.nodeName is undefined" in jquery min script file.

EDIT:

Here the code for creating datatables:

if( $.fn.dataTable ) {
            $(".mws-datatable").dataTable();
            var oTable = $(".mws-datatable-fn").dataTable({
                bRetrieve: true,
            sPaginationType: "full_numbers"
            });
        }
like image 697
GVillani82 Avatar asked Jul 06 '13 16:07

GVillani82


People also ask

How to remove a row from a DataTable?

There are two methods you can use to delete a DataRow object from a DataTable object: the Remove method of the DataRowCollection object, and the Delete method of the DataRow object. Whereas the Remove method deletes a DataRow from the DataRowCollection, the Delete method only marks the row for deletion.

How to Delete DataTable in jQuery?

The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements. Parameters: It accepts single parameter selector which is optional.


2 Answers

I solved the problem using this code:

$(".del").bind( "click", function(event) {
    var target_row = $(this).closest("tr").get(0); // this line did the trick
    var aPos = oTable.fnGetPosition(target_row); 

    oTable.fnDeleteRow(aPos);
});
like image 64
GVillani82 Avatar answered Oct 04 '22 15:10

GVillani82


$().ready(function () {
  $('body').on('click', '#deletebtn', function () {
    $("#example1 tr").each(function () {
      var rowSelector = $(this);

      if (rowSelector.find("input[type='checkbox']").prop('checked')) {
        rowSelector.remove();
      }
    });
  });
});
like image 22
Hasnain Mehmood Avatar answered Oct 04 '22 15:10

Hasnain Mehmood