在jQuery DataTables中选择列

前端之家收集整理的这篇文章主要介绍了在jQuery DataTables中选择列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Datatables API说明中,您可以切换列可见性 https://datatables.net/extensions/buttons/examples/column_visibility/columns.html
  1. $(document).ready(function() {
  2. $('#example').DataTable( {
  3. dom: 'Bfrtip',buttons: [
  4. {
  5. extend: 'colvis',columns: ':not(:first-child)'
  6. }
  7. ]
  8. } );
  9. } );

但是有没有办法通过鼠标点击来选择一个列,就像选择一行一样 – 即让用户通过突出显示列来了解列的选择 – 并从javascript访问该列中的数据(例如,在列之后添加另一列)选中列或删除所选列并重新加载表,计算列中数据的统计信息等.?)

解决方法

使用选择扩展名,可以选择列.

HTML

  1. <table id="example" class="display" cellspacing="0" width="100%">
  2. <thead>
  3. <tr>
  4. <th>Name</th>
  5. <th>Position</th>
  6. <th>Office</th>
  7. <th>Age</th>
  8. <th>Start date</th>
  9. <th>Salary</th>
  10. </tr>
  11. <tr>
  12. <th><input type="checkBox"></th>
  13. <th><input type="checkBox"></th>
  14. <th><input type="checkBox"></th>
  15. <th><input type="checkBox"></th>
  16. <th><input type="checkBox"></th>
  17. <th><input type="checkBox"></th>
  18. </tr>
  19. </thead>
  20.  
  21. <!-- skipped -->
  22.  
  23. </table>

JavaScript的

  1. var table = $('#example').DataTable({
  2. 'orderCellsTop': true,'select': 'multi'
  3. });
  4.  
  5. $('#example').on('change','thead input[type="checkBox"]',function(){
  6. var colIdx = $(this).closest('th').index();
  7. if(this.checked){
  8. table.column(colIdx).select();
  9. } else {
  10. table.column(colIdx).deselect();
  11. }
  12. });

有关代码和演示,请参见this example.

有关更多信息和示例,请参见jQuery DataTables: How to select columns.

猜你在找的jQuery相关文章