1

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

$("#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: "https://stackoverflow.com/users/delete_selected", 
       data: { 
        users: users 
       } 
      }) 
      .always(function() { 
       window.location.reload(); 
      }); 
     } 
    }); 

但我想在过滤设置,删除每一个数据。请帮忙!谢谢。

+0

哪些版本的数据表? – davidkonrad

回答

2

假设你正在使用的数据表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版本的数据表,你必须使用插件,请参见http://datatables.net/forums/discussion/214/how-to-get-searched-or-filtered-data/p1