jquery最佳实现展开收缩table多个列

r囧r小猫 2023-07-10 14:21 51阅读 0赞
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Show and Hide Columns in a Table</title>
  5. <link href="CSS/table.css" rel="stylesheet" />
  6. <script src="../scripts/jquery-1.11.1.min.js"></script>
  7. <script> $(function () { var $chk = $("#grpChkBox input:checkbox"); var $tbl = $("#someTable"); $chk.prop('checked', true); $chk.click(function () { var colToHide = $tbl.find("." + $(this).attr("name")); $(colToHide).toggle(); }); }); </script>
  8. </head>
  9. <body>
  10. <h2>Show and Hide Columns in a Table</h2>
  11. <p>Click on each Checkbox to hide corresponding Column<</p>
  12. <div id="grpChkBox">
  13. <p><input type="checkbox" name="empid" /> Employee ID</p>
  14. <p><input type="checkbox" name="fname" /> First Name</p>
  15. <p><input type="checkbox" name="lname" /> Last Name</p>
  16. <p><input type="checkbox" name="email" /> Email</p>
  17. <p><input type="checkbox" name="phone" /> Phone</p>
  18. </div>
  19. <table id="someTable">
  20. <thead>
  21. <tr>
  22. <th class="empid">EmpId</th>
  23. <th class="fname">First Name</th>
  24. <th class="lname">Last Name</th>
  25. <th class="email">Email</th>
  26. <th class="phone">Phone</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. <tr>
  31. <td class="empid">E342</td>
  32. <td class="fname">Bill</td>
  33. <td class="lname">Evans</td>
  34. <td class="email">Bill@devcurry.com</td>
  35. <td class="phone">234-2345-2345</td>
  36. </tr>
  37. <tr>
  38. <td class="empid">E343</td>
  39. <td class="fname">Laura</td>
  40. <td class="lname">Matt</td>
  41. <td class="email">laura@devcurry.com</td>
  42. <td class="phone">123-1234-5678</td>
  43. </tr>
  44. <tr>
  45. <td class="empid">E344</td>
  46. <td class="fname">Ram</td>
  47. <td class="lname">Kumar</td>
  48. <td class="email">ram@devcurry.com</td>
  49. <td class="phone">345-3456-7890</td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. </body>
  54. </html>

效果:

demo:http://www.jquerycookbook.com/demos/S3-TablesTabsPanels/21-HideShowColumns.html

在这里插入图片描述

同时隐藏展开多个列:

th和tr里面的td使用同样的class即可:

  1. // 点击展开收缩 by yangshuiping 2020-03-03
  2. var $chk = $("#grpChkBox"); // cache the selector
  3. var $tbl = $(".table-hover");
  4. $chk.click(function () {
  5. var colToHide = $tbl.find("." + $(this).attr("name"));
  6. $(colToHide).toggle();
  7. });
  8. // 点击展开收缩 end by yangshuiping 2020-03-03
  9. <button id="grpChkBox" type="button" class="btn btn-primary btn-lg" name="empid">Employee ID</button>
  10. <th class="empid">变更原因(第2次)</th>
  11. <td class="empid">2020-03-29</td>

参考:

https://www.dotnetcurry.com/jquery/1122/jquery-show-hide-table-column-using-checkbox

发表评论

表情:
评论列表 (有 0 条评论,51人围观)

还没有评论,来说两句吧...

相关阅读