最小表排序插件 jQuery Sortberg.js

  • 源码大小:8.02KB
  • 所需积分:1积分
  • 源码编号:19JP-3442
  • 浏览次数:1054次
  • 最后更新:2023年06月13日
  • 所属栏目:表格
本站默认解压密码:19jp.com 或 19jp_com

简介

Sortberg.js是一个轻量级、快速的jQuery表排序插件,它可以根据数据值自动选择合适的排序方法(按顺序或升序),并支持具有行span/colspan的复杂表。

参见:

  • 按列排序表的10个最佳JavaScript插件

如何使用它:

1.下载并加载jquery.sortberg.js查询在jQuery之后。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  2. <script src="/path/to/jquery.sortberg.js"></script>

2.调用函数sortberg公司在HTML表上,插件将完成其余工作。

  1. <table>
  2. <thead>
  3. <tr>
  4. <th>Company</th>
  5. <th>Contact</th>
  6. <th>Country</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <tr>
  11. <td>Alfreds Futterkiste</td>
  12. <td>Maria Anders</td>
  13. <td>Germany</td>
  14. </tr>
  15. <tr>
  16. <td>Centro comercial Moctezuma</td>
  17. <td>Francisco Chang</td>
  18. <td>Mexico</td>
  19. </tr>
  20. </tbody>
  21. </table>
  1. $(function(){
  2. $('table').sortberg();
  3. });

3.它还支持分组数据,如下所示:

  1. <table>
  2. <thead>
  3. <tr>
  4. <th>Name</th>
  5. <th>Phone</th>
  6. </tr>
  7. </thead>
  8. <tbody>
  9. <tr>
  10. <td>Alice</td>
  11. <td>+1 234-567-8901</td>
  12. </tr>
  13. <tr class="grouped">
  14. <td colspan="2">Additional Info For Alice</td>
  15. </tr>
  16. <tr>
  17. <td>Bob</td>
  18. <td>+1 987-654-3210</td>
  19. </tr>
  20. <tr class="grouped">
  21. <td colspan="2">Additional Info For Bob</td>
  22. </tr>
  23. </tbody>
  24. </table>
  1. $(function(){
  2. $('table').sortberg({
  3. // default: 'details'
  4. groupClass: 'grouped'
  5. });
  6. });

4.确定是否忽略区分大小写。默认值:false。

  1. $(function(){
  2. $('table').sortberg({
  3. ignoreCase: true,
  4. });
  5. });

5.添加您自己的排序逻辑。

  1. var myLogic = function(a, b){
  2. return $(a).data('specialdata') - $(b).data('specialdata');
  3. };
  4.  
  5. $('table').sortberg({
  6. comparator: myLogic
  7. });

更新日志:

2022-08-03

  • 仅在单击第th个元素时对列进行排序,而不是在单击第h个元素内的子元素时对其进行排序

预览截图