如何使用页面中包含多个表的 jquery 数据表插件删除当前行 [英] How to delete current row with jquery datatable plugin with multiple tables in the page

查看:21
本文介绍了如何使用页面中包含多个表的 jquery 数据表插件删除当前行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站中使用数据表插件.通常我每页只有 1 个数据表,但在特殊显示中我有 2 个数据表.

I use datatable plugin in a website. Normally I have only 1 datatable per page but in a special display I have 2 datable.

其实我有这个代码

var oTable = $('.datatable').dataTable({
    'sPaginationType':'full_numbers',
    "iDisplayLength": 50,
    "oLanguage": {
        "sUrl": "js/locales/dataTables.french.txt"
    }
});

/* Add a click handler to the rows - this could be used as a callback */
$(".delete-ajax").click(function(event) {
    event.preventDefault();
    var answer = confirm("Supprimer l'élément ?")
    if (answer){
    var loading = $('.loading-notification');
    loading.removeClass('hidden');
        $(oTable.fnSettings().aoData).each(function (){
            $(this.nTr).removeClass('row_selected');
        });
        $(event.target).parents('tr').addClass('row_selected');
        var url = $(this).attr('href');
        var id = $(this).attr('data-ajax');
        var anSelected = fnGetSelected( oTable );
        $.ajax({
            type: "POST",
            url: url,
        data: "delete=true&id="+ id,
        async : true,
        success: function(msg) {
            loading.addClass('hidden');
            oTable.fnDeleteRow( anSelected[0] );
            }
        });
    }
});
/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal ){
    var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();
for ( var i=0 ; i<aTrs.length ; i++ ){
    if ( $(aTrs[i]).hasClass('row_selected') ){
        aReturn.push( aTrs[i] );
    }
}
return aReturn;
}

当我只有 1 个数据表时,此代码运行良好,但当我有更多数据表时,我会在控制台中获取:

This code work well when I have only 1 datatable but when I have more I obtain In my console :

k is undefined
[Stopper sur une erreur]   h=a._iDisplayEnd;if(a.oFeatures.bServe...push(a.aoOpenRows[k].nTr)}}else{d[0]= 

知道如何解决这个问题吗?

Any idea on how to solve this problem ?

推荐答案

这里是如何删除表格中的一行(不管页面中有多少表格)

Here is how you can delete a row in the table (regardless the number of amount of tables in page)

我在同一页有两张桌子

我不确定您提供给表的 ID 有多好,但如果它是.datatable",则:

I am not sure about how good is the id that you gave to the table, but if its ".datatable" then:

将此功能添加到您的页面

Add this function to your page

$(".datatable tbody").delegate("tr", "click", function () {
    var iPos = oTable.fnGetPosition(this);
    if (iPos !== null) {
        oTable.fnDeleteRow(iPos);//delete row
    }
});

这篇关于如何使用页面中包含多个表的 jquery 数据表插件删除当前行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆