tablesorter 的使用

 1 <table id="myTable" class="tablesorter"> 
 2 <thead> 
 3 <tr> 
 4     <th>Last Name</th> 
 5     <th>First Name</th> 
 6     <th>Email</th> 
 7     <th>Due</th> 
 8     <th>Web Site</th> 
 9 </tr> 
10 </thead> 
11 <tbody> 
12 <tr> 
13     <td>Smith</td> 
14     <td>John</td> 
15     <td>jsmith@gmail.com</td> 
16     <td>$50.00</td> 
17     <td>http://www.jsmith.com</td> 
18 </tr> 
19 <tr> 
20     <td>Bach</td> 
21     <td>Frank</td> 
22     <td>fbach@yahoo.com</td> 
23     <td>$50.00</td> 
24     <td>http://www.frank.com</td> 
25 </tr> 
26 <tr> 
27     <td>Doe</td> 
28     <td>Jason</td> 
29     <td>jdoe@hotmail.com</td> 
30     <td>$100.00</td> 
31     <td>http://www.jdoe.com</td> 
32 </tr> 
33 <tr> 
34     <td>Conway</td> 
35     <td>Tim</td> 
36     <td>tconway@earthlink.net</td> 
37     <td>$50.00</td> 
38     <td>http://www.timconway.com</td> 
39 </tr> 
40 </tbody> 
41 </table>

1. 需要引入的资源

1 <script type="text/javascript" src="/path/to/jquery-latest.js"></script> 
2 <script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script> 

2.使用demo

  1 $(document).ready(function() 
  2    
  3     // demo1 : 初始化,使表格可排序 
  4     $("#myTable").tablesorter(); 
  5 
  6     // demo1 : 默认第一列,第二列按升序排序 
  7     $("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} ); 
  8 
  9 
 10     // demo3 : 手动触发排序
 11     $("myTable").trigger("sorton",[[0,0],[2,0]]);
 12 
 13     // demo4 : 禁止列排序
 14     $("table").tablesorter({ 
 15         headers: { 
 16             // 列序号默认从0开始
 17             1: { 
 18                 // 第二列不可排序 
 19                 sorter: false 
 20             }, 
 21             2: { 
 22                 sorter: false 
 23             } 
 24         } ,
 25         // 启用调试模式
 26         debug: true 
 27     }); 
 28 
 29     // demo5 : 强制默认排序列
 30     $("table").tablesorter({ 
 31         // set forced sort on the fourth column and i decending order. 
 32         sortForce: [[0,0]] 
 33     }); 
 34 
 35     // demo6 : 改变多条件排序使用的辅助键,默认shift
 36     $("table").tablesorter({ 
 37         sortMultiSortKey: 'altKey' ,
 38         textExtraction: function(node) { 
 39             // extract data from markup and return it  
 40             return node.childNodes[0].childNodes[0].innerHTML; 
 41         } 
 42     }); 
 43 
 44 
 45     // demo7 : 给table 添加元数据也可达到排序的目的,metadata插件会自动获取类属性
 46     <table cellspacing="1" class="tablesorter {sortlist: [[0,0],[4,0]]}"> 
 47 
 48     // demo8 : 也可以在th的class中指定排序
 49     <tr> 
 50         <th class="{sorter: false}">first name</th> 
 51         <th>last name</th> 
 52         <th>age</th> 
 53         <th>total</th> 
 54         <!-- 指定数据解析类型 -->
 55         <th class="{sorter: 'text'}">first name</th> 
 56         <th class="{sorter: false}">discount</th> 
 57         <th>date</th> 
 58     </tr> 
 59 
 60     // demo9 : 指定sort相关事件
 61     $("table").bind("sortStart",function() { 
 62         $("#overlay").show(); 
 63     }).bind("sortEnd",function() { 
 64         $("#overlay").hide(); 
 65     }); 
 66 
 67     // demo10 : 动态添加数据
 68     $("table tbody").append(html); 
 69     // 通知插件需要更新 
 70     $("table").trigger("update"); 
 71     var sorting = [[2,1],[0,0]]; 
 72     // 触发排序事件
 73     $("table").trigger("sorton",[sorting]); 
 74 
 75     // demo11 : 修改默认参数
 76     $.tablesorter.defaults.sortList = [[0,0]]; 
 77 
 78     // demo12 : 自定义排序类型
 79     $.tablesorter.addParser({ 
 80         // set a unique id 
 81         id: 'grades', 
 82         is: function(s) { 
 83             // return false so this parser is not auto detected 
 84             return false; 
 85         }, 
 86         format: function(s) { 
 87             // format your data for normalization 
 88             return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0); 
 89         }, 
 90         // set type, either numeric or text 
 91         type: 'numeric' 
 92     }); 
 93      
 94     $(function() { 
 95         $("table").tablesorter({ 
 96             headers: { 
 97                 6: { 
 98                     sorter:'grades' 
 99                 } 
100             } 
101         }); 
102     }); 
103 
104 
105     // demo14 : 自定义组件
106     $.tablesorter.addWidget({ 
107         // give the widget a id 
108         id: "repeatHeaders", 
109         // format is called when the on init and when a sorting has finished 
110         format: function(table) { 
111             // cache and collect all TH headers 
112             if(!this.headers) { 
113                 var h = this.headers = [];  
114                 $("thead th",table).each(function() { 
115                     h.push( 
116                         "" + $(this).text() + "" 
117                     ); 
118                      
119                 }); 
120             } 
121              
122             // remove appended headers by classname. 
123             $("tr.repated-header",table).remove(); 
124              
125             // loop all tr elements and insert a copy of the "headers"     
126             for(var i=0; i < table.tBodies[0].rows.length; i++) { 
127                 // insert a copy of the table head every 10th row 
128                 if((i%5) == 4) { 
129                     $("tbody tr:eq(" + i + ")",table).before( 
130                         $("").html(this.headers.join("")) 
131                      
132                     );     
133                 } 
134             } 
135         } 
136     }); 
137      
138     // demo15 : 调用插件call the tablesorter plugin and assign widgets with id "zebra" (Default widget in the core) and the newly created "repeatHeaders" 
139     $("table").tablesorter({ 
140         widgets: ['zebra','repeatHeaders'] 
141     });         
142 
143 );

5. 注意事项

  依赖项:jquery

  meta数据插件:  jQuery Metadata 2.1

  分页插件:jQuery.tablesorter.pager.js

  css,image 在blue skin 文件夹中可以找到

  Demo 下载:https://pan.baidu.com/s/1hqwJpFQ

人生得意须尽欢,莫使金樽空对月.
原文地址:https://www.cnblogs.com/luojie-/p/7552619.html