javascript 使用 Jquery Datatable Plugin 按 Id 删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7947546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:50:04  来源:igfitidea点击:

Delete row using Jquery Datatable Plugin by Id

javascriptjqueryjquery-ui

提问by Rodrigo

The rows of oTabledatatable has unique ids.

数据表的行oTable具有唯一的 id。

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:

编辑2:

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

真正的问题是:如何使用id?

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

该方法fnGetPosition($('#row'+id+'-tr')不返回行。

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

我在调用 fngetPosition: 之前放置了这段代码console.log($('#row'+id+'-tr'))并返回:({length:1, 0:({}), context:({}), selector:"#row1-tr"})

Thanks for help!

感谢帮助!

采纳答案by Rodrigo

The answer:

答案:

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

回答by topek

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.

查看第 1903 行附近的 jquery.dataTables 源代码,在我看来,$('#row'+id+'-tr')在您的情况下确实返回了一个空对象。这意味着您的页面上不存在选择器。

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

您可以通过console.log($('#row'+id+'-tr'))oTable.fnDeleteRow(