代碼如下:
<script type="text/javascript" src="js/jquery.js?1.1.9"></script>
<script type="text/javascript" src="js/jquery.tablesorter.js?1.1.9"></script>
<!-- 引入以下樣式則表頭出現(xiàn)排序圖標(biāo),同時引入圖片 -->
<link href="css/style.css?1.1.9" rel="stylesheet" type="text/css" >
效果如圖:
二、標(biāo)準(zhǔn)的HTML表格,必須包括thead和tbody標(biāo)簽
代碼如下:
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Name</th>
<th>Sex</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>zhangsan</td>
<td>boy</td>
<td>beijing</td>
</tr>
<tr>
<td>lisi</td>
<td>girl</td>
<td>shanghai</td>
</tr>
<tr>
...略
</tr>
</tbody>
</table>
三、設(shè)置table可排序
代碼如下:
$(document).ready(function(){
//第一列不進(jìn)行排序(索引從0開始)
$.tablesorter.defaults.headers = {0: {sorter: false}};
$(".tablesorter").tablesorter();
});
官方文檔:http://tablesorter.com/docs/
補充說明:
在使用過程遇到的一個問題,我的表格數(shù)據(jù)是通過ajax獲取的,首頁進(jìn)行排序的時候沒問題
當(dāng)進(jìn)行下一頁排序的時候,會把上頁的數(shù)據(jù)也重新顯示出來,解決這個問題可以在你ajax獲取數(shù)據(jù)之后
觸發(fā)"update"事件,代碼如下:
代碼如下:
$(".tablesorter").trigger("update");
以上問題著實頭疼了很久,剛開始用的官網(wǎng)上的Pager plugin,發(fā)現(xiàn)這個不太合適。
又網(wǎng)上查資料 整理以下代碼:
代碼如下:
$(".tablesorter tbody tr").addClass("delete");
$(".tablesorter tbody tr.delete").remove();
$("table tbody").append(html);
$(".tablesorter").trigger("appendCache");
$(".tablesorter").trigger("update");
$(".tablesorter").trigger("sorton",[[[2,1],[0,0]]]);
于是都用上了,久經(jīng)測試 發(fā)現(xiàn)只有$(".tablesorter").trigger("update");這一句能解決問題
其他的不知道是什么東東。
所需文件下載地址:http://xiazai.jb51.net/201405/yuanma/jquery.tablesorter.zip
style.css及圖片在themes\blue路徑下。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com