如何使用 jquery 数据表插件删除当前行

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

How to delete current row with jquery datatable plugin

jquerysortingdatatablesrowsdelete-row

提问by leora

I have a column with buttons in a table I'm using jQuery datatable plugin. The buttons say "Remove" and the idea is that when you click on that button it deletes the current row in the table.

我在使用 jQuery 数据表插件的表中有一个带有按钮的列。按钮显示“删除”,意思是当您单击该按钮时,它会删除表中的当前行。

When I call fnDeleteRowit seems to work the first time but no any further time for that row so it looks like its not really deleting the row properly.

当我打电话时,fnDeleteRow它似乎是第一次工作,但该行没有任何进一步的时间,所以看起来它并没有真正正确地删除该行。

回答by Jason Orendorff

Try this:

尝试这个:

var row = $(this).closest("tr").get(0);
oTable.fnDeleteRow(oTable.fnGetPosition(row));

If it doesn't work, check the following example

如果它不起作用,请检查以下示例

回答by Sridhar

Let's say you attached a function to be called when the user clicks on the button. The function would be something like this

假设您附加了一个在用户单击按钮时要调用的函数。该功能将是这样的

function DeleteRow(event)
{
  //get the row of the cell that is clicked
  var $row = $(this).parents("tr").eq(0)
  //if you need the id you can get it as
  var rowid = $row.attr("id");
  //now you can call delete function on this row
  $row.delete(); 
}

回答by Ryan Reynolds

How about this:

这个怎么样:

    // Delete Row
    $('.glyphicon-minus').on("click", function() {
        configTable.row($(this).closest("tr").get(0)).remove().draw();
    });

回答by Cengiz Araz

This is how it works for me. In document ready function I assign converted version of HTML table to a variable and when a button in the is clicked I go through parents/childs with JQuery and send the row you get as a parameter to the library's fnDeleteRow() function.

这就是它对我的工作方式。在文档就绪函数中,我将转换后的 HTML 表版本分配给一个变量,当单击中的按钮时,我使用 JQuery 遍历父/子并将您获得的行作为参数发送到库的 fnDeleteRow() 函数。

Here's is the comments from the library function. And an example that's mentioned in library.

这是库函数的注释。以及图书馆中提到的一个例子。

/**
* Remove a row for the table
*  @param {mixed} target The index of the row from aoData to be deleted, or
*    the TR element you want to delete
*  @param {function|null} [callBack] Callback function
*  @param {bool} [redraw=true] Redraw the table or not
*  @returns {array} The row that was deleted
*  @dtopt API
*  @deprecated Since v1.10
*
*  @example
*    $(document).ready(function() {
*      var oTable = $('#example').dataTable();
*
*      // Immediately remove the first row
*      oTable.fnDeleteRow( 0 );
*    } );
*/

// And here's how it worked for me.
var oTable;
$("document").ready(function () {
    oTable = $("#myTable").dataTable();
});

//Remove/Delete button's click.
$("a[name='deleteColumn']").click(function () {
    var $row = $(this).parent().parent();
    oTable.fnDeleteRow($row);
});

回答by cobbal

from this page:

这个页面

$('#example tbody td').click( function () {
    /* Get the position of the current data from the node */
    var aPos = oTable.fnGetPosition( this );

    //...
} );