【问题标题】:Getting all the filtered data from a non-ajaxified datatable从非 ajaxified 数据表中获取所有过滤后的数据
【发布时间】:2014-04-17 15:11:51
【问题描述】:

我使用数据表列出了我的应用程序中的所有用户。我必须为管理员添加对数据表中过滤数据执行“删除”操作的功能。例如,在数据表的搜索框中键入“R”会列出所有可能包含字母“R”的数据。现在单击“删除”按钮将删除所有这些用户。我已经实现了这个逻辑,但它只从第一页获取数据:

 $("#delete-users").click(function(){
        var status = confirm("All the users currently showing in the table will be deleted. Do you want to continue?");
        if(status){
            var users = new Array();
            $("table#users tbody tr").each(function() {
                users.push(this.id);
            });

            $.ajax({
                type: "POST",
                url: "/users/delete_selected",
                data: {
                    users: users
                }
            })
            .always(function() {
                window.location.reload();
            });
        }
    });

但我想删除过滤集中的所有数据。请帮忙!谢谢。

【问题讨论】:

  • 哪个版本的数据表?

标签: jquery ruby-on-rails ruby-on-rails-4 datatable jquery-datatables


【解决方案1】:

假设您使用的是 dataTables v1.9 或更高版本:您可以通过这种方式检索所有过滤后的行(请参阅下面的链接):

var filteredRows = dataTable._('tr', {"filter":"applied"});

你的函数应该是这样的:

$("#delete-users").click(function(){
    var status = confirm("All the users currently showing in the table will be deleted. Do you want to continue?");
    if(status){
        var users = []; 
        var filteredRows = dataTable._('tr', {"filter":"applied"});
        filteredRows.forEach(function(row) {
            //push value for the column index that holds id for the user
            //0 is just a demonstration
            users.push(row[0]);
        });
        //
        //...
        //
    }
});

这是一个演示 -> http://jsfiddle.net/73DEd/

如果你使用的是1.9以下的dataTables版本,则必须使用插件,见http://datatables.net/forums/discussion/214/how-to-get-searched-or-filtered-data/p1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多