jQuery 数据表:如何删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10214307/
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
jQuery Datatables: how to delete the row
提问by Ibrahim Azhar Armar
I want to delete the row from datatable. Here is the datatables code I use:
我想从数据表中删除该行。这是我使用的数据表代码:
var aSelected = [];
oTable = $('.itemPublished').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bServerSide": true,
"bProcessing": true,
"sAjaxSource": "/item/datatable",
"bDeferRender": true,
"iDisplayLength":20,
"aLengthMenu": [[10, 20, 50, 75, 100, 150], [10, 20, 50, 75, 100, 150]],
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 2, 3, 4 ] },
{ "sClass": "left", "aTargets": [ 1 ] }
],
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
if ( jQuery.inArray(aData.DT_RowId, aSelected) !== -1 ) {
$(nRow).addClass('row_selected');
}
$(nRow).addClass('gradeA');
return nRow;
}
});
I wanted to test fire an event to delete a row from the datatable. The event is triggered by a button which is outside of datatables table DOM. I tried doing this:
我想测试触发事件以从数据表中删除一行。该事件由数据表表 DOM 之外的按钮触发。我尝试这样做:
$('.test').live('click', function () {
oTable.fnDeleteRow( 0 );
});
To check if it can delete the first row from the table, but it does not and nor does it produce any error. Where am I going wrong?
检查它是否可以从表中删除第一行,但它不会也不会产生任何错误。我哪里错了?
回答by Stefan
Found the following comment here: http://datatables.net/forums/discussion/6208/hyperlink-event-to-delete-row/p1:
在这里找到以下评论:http: //datatables.net/forums/discussion/6208/hyperlink-event-to-delete-row/p1:
"Since you are using server-side processing, and fnDeleteRow knows nothing about your server-side environment, you need to make an Ajax call to the server for it to do the delete and then call fnDraw on the table for it to refresh with the new data set."
“由于您使用的是服务器端处理,并且 fnDeleteRow 对您的服务器端环境一无所知,因此您需要对服务器进行 Ajax 调用以使其执行删除操作,然后在表上调用 fnDraw 以使其刷新新数据集。”