数据表循环遍历所有表行,只循环当前显示的表行

datatables loop through all table rows, only did loop on the currently shown table rows

本文关键字:循环 显示 数据表 遍历      更新时间:2023-09-26

我试图循环到数据表表上的所有表行,但只循环到当前显示的表行,如我有100条记录(100个表行),目前显示10个表行,不幸的是,它只循环到10个表行。

我使用下面的代码循环遍历每个表行

$("#admin_user_table tbody tr").each(function(){
    alert("yes");
});
有什么想法、帮助、线索、建议、建议吗?更具体地说,下面的

是我的小提琴。

$(document).ready(function(){
  $('#sample_table').DataTable( {
        "bSort": false,
        "pagingType": "full_numbers",
        "dom": 'T<"clear">lfrtip',
    });
  
  $("#test_loop").click(function(){
    $("#sample_table tbody tr").each(function(){
      alert("table row");
   });
  });
  
});
table{width: 100%}table td, table th{padding: 7px; text-align: center; vertical-align: middle}table thead{border-bottom: 2px solid #cccccc;}table tbody tr{border-bottom: 1px solid #ccc;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/r/dt/dt-1.10.8/datatables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/r/dt/dt-1.10.8/datatables.min.js"></script>
<table cellpadding="0" cellspacing="0" id="sample_table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Branch</th>
      <th>Department</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td><td>Sample position</td><td>sample branch</td><td>sample department</td>
    </tr>
</tbody>
</table>
<button id="test_loop">Loop through all the table rows</button>

您可以使用原始过滤器

$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var enabled = $('#my-filter').is(':checked');        
        return !enabled || /raw$/g.test(data[1]);
    }
);
$(document).ready(function() {
 var table =  $('#sample_table').DataTable({
    "bSort": false,
    "pagingType": "full_numbers",
    "dom": 'T<"clear">lfrtip',
  });
  $("#my-filter").change(function() {
    table.draw();
  });
});
table {
  width: 100%
}
table td,
table th {
  padding: 7px;
  text-align: center;
  vertical-align: middle
}
table thead {
  border-bottom: 2px solid #cccccc;
}
table tbody tr {
  border-bottom: 1px solid #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/r/dt/dt-1.10.8/datatables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/r/dt/dt-1.10.8/datatables.min.js"></script>
<table cellpadding="0" cellspacing="0" id="sample_table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Branch</th>
      <th>Department</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample raw</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
    <tr>
      <td>Sample Name 1</td>
      <td>Sample position</td>
      <td>sample branch</td>
      <td>sample department</td>
    </tr>
  </tbody>
</table>
<input id="my-filter" type="checkbox" />