Rodrigo
Rodrigo

Reputation: 4802

Delete row using Jquery Datatable Plugin by Id

The rows of oTable datatable has unique ids.

Why this code doesnt work?

oTable.fnDeleteRow(
    oTable.fnGetPosition(
        $('#row'+id+'-tr')));

the error is:

[18:10:44.631] nNode.nodeName is undefined @ http://localhost: ... /jquery.dataTables.js:1903

Thank you!

edit:

Example:

<table>
  <thead>
   <th>
    <td>Name </td>
    <td>Delete</td>
   </th>
  </thead>

  <tbody>
   <tr id="row0-tr">
    <td> Row 0 </td>
    <td> <Button onclick="deleteRow(0)"> - </td>
   </tr>

   <tr id="row1-tr">
    <td> Row 1 </td>
    <td> <Button onclick="deleteRow(1)"> - </td>
   </tr>

   <tr id="row2-tr">
    <td> Row 2 </td>
    <td> <Button onclick="deleteRow(2)"> - </td>
   </tr>

  </tbody>
</table>

edit 2:

The real problem is: How to get row of table using id?

The method fnGetPosition($('#row'+id+'-tr') isn't returning row.

I put this code before calling fngetPosition: console.log($('#row'+id+'-tr')) and returns: ({length:1, 0:({}), context:({}), selector:"#row1-tr"})

Thanks for help!

Upvotes: 4

Views: 11232

Answers (2)

Rodrigo
Rodrigo

Reputation: 4802

The answer:

oTable.fnDeleteRow(
    oTable.fnGetPosition(
        document.getElementById('#row'+id+'-tr')));

Upvotes: 4

topek
topek

Reputation: 18979

Looking at the jquery.dataTables source code around line 1903, it seems to me that $('#row'+id+'-tr') does return an empty object in your case. This means that the selector does not exist on your page.

You can verify this by putting a console.log($('#row'+id+'-tr')) before the oTable.fnDeleteRow(

Upvotes: 0

Related Questions