jQuery:删除数据表中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17504753/
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-08-26 19:13:41  来源:igfitidea点击:

jQuery: deleting row in datatables

javascriptjqueryhtmldomdatatables

提问by GVillani82

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

我正在使用 jquery 数据<tr>表,并且在具有以下结构的表中有一些:

<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.

我编写了以下 jquery 代码来删除与我单击的按钮关联的行。

$(".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.

"TypeError: a.nodeName is undefined"在 jquery min 脚本文件中。

EDIT:

编辑:

Here the code for creating datatables:

这里是创建数据表的代码:

if( $.fn.dataTable ) {
            $(".mws-datatable").dataTable();
            var oTable = $(".mws-datatable-fn").dataTable({
                bRetrieve: true,
            sPaginationType: "full_numbers"
            });
        }

回答by GVillani82

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);
});

回答by Hasnain Mehmood

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

      if (rowSelector.find("input[type='checkbox']").prop('checked')) {
        rowSelector.remove();
      }
    });
  });
});