简体   繁体   中英

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!

The answer:

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

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(

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