jQuery表格排序組件-tablesorter使用示例
更新時間:2014年05月26日 09:47:50 作者:
這篇文章主要介紹了jQuery表格排序組件-tablesorter的使用,需要的朋友可以參考下
一、引入文件
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.js"></script>
<!-- 引入以下樣式則表頭出現(xiàn)排序圖標(biāo),同時引入圖片 -->
<link href="css/style.css" 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(){
//第一列不進行排序(索引從0開始)
$.tablesorter.defaults.headers = {0: {sorter: false}};
$(".tablesorter").tablesorter();
});
官方文檔:http://tablesorter.com/docs/
補充說明:
在使用過程遇到的一個問題,我的表格數(shù)據(jù)是通過ajax獲取的,首頁進行排序的時候沒問題
當(dāng)進行下一頁排序的時候,會把上頁的數(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路徑下。
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.js"></script>
<!-- 引入以下樣式則表頭出現(xiàn)排序圖標(biāo),同時引入圖片 -->
<link href="css/style.css" rel="stylesheet" type="text/css" >
效果如圖:

二、標(biāo)準(zhǔn)的HTML表格,必須包括thead和tbody標(biāo)簽
復(fù)制代碼 代碼如下:
<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可排序
復(fù)制代碼 代碼如下:
$(document).ready(function(){
//第一列不進行排序(索引從0開始)
$.tablesorter.defaults.headers = {0: {sorter: false}};
$(".tablesorter").tablesorter();
});
官方文檔:http://tablesorter.com/docs/
補充說明:
在使用過程遇到的一個問題,我的表格數(shù)據(jù)是通過ajax獲取的,首頁進行排序的時候沒問題
當(dāng)進行下一頁排序的時候,會把上頁的數(shù)據(jù)也重新顯示出來,解決這個問題可以在你ajax獲取數(shù)據(jù)之后
觸發(fā)"update"事件,代碼如下:
復(fù)制代碼 代碼如下:
$(".tablesorter").trigger("update");
以上問題著實頭疼了很久,剛開始用的官網(wǎng)上的Pager plugin,發(fā)現(xiàn)這個不太合適。
又網(wǎng)上查資料 整理以下代碼:
復(fù)制代碼 代碼如下:
$(".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路徑下。
相關(guān)文章
Jquery遍歷checkbox獲取選中項value值的方法
這篇文章主要介紹了Jquery遍歷checkbox獲取選中項的value值,需要的朋友可以參考下2014-02-02

深入理解Jquery表單驗證(使用formValidator)
表單驗證在web中中的應(yīng)用很廣,本篇文章主要介紹了Jquery表單驗證(使用formValidator),非常具有實用價值,需要的朋友可以參考下。
2017-01-01 
使用jQuery mobile NuGet讓你的網(wǎng)站在移動設(shè)備上同樣精彩
這篇文章主要介紹了使用jQuery mobile NuGet讓你的網(wǎng)站在移動設(shè)備上同樣精彩,使用jQuery mobile NuGet包,改變共享的布局和視圖,并使你的網(wǎng)站在傳統(tǒng)的瀏覽器和大部分手機展現(xiàn)的都很好。,需要的朋友可以參考下
2019-06-06