用jquery.sortElements實現(xiàn)table排序
更新時間:2014年05月04日 15:20:41 作者:
實現(xiàn)table排序,網(wǎng)上有很多解決方案,很多都基于jQuery,最后我選擇用sortElements,實現(xiàn)很簡單
項目中要實現(xiàn)table排序的功能。
網(wǎng)上有很多解決方案,很多都基于jQuery。
jquery.tablesorter,大小17KB,不過他的首頁在ie10下兼容性有點問題。
DataTables,大小75KB,功能強大,帶分頁,搜索等功能。
還有插件叫sortElements,很小巧,只有3KB,兼容性也不錯,而且在Github上有818個星。
最后我選擇用sortElements,實現(xiàn)很簡單:
1. 引入jQuery
<script type="text/javascript" src="jquery.js"></script>
2. 引入sortElements.js
<script type="text/javascript" src="jquery.sortElements.js"></script>
3. js 代碼
$(document).ready(function(){
var table = $('#mytable');//table的id
$('#sort_header')//要排序的headerid
.each(function(){
var th = $(this),
thIndex = th.index(),
inverse = false;
th.click(function(){
table.find('td').filter(function(){
return $(this).index() === thIndex;
}).sortElements(function(a, b){
return $.text([a]) > $.text([b]) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}, function(){
return this.parentNode;
});
inverse = !inverse;
});
});
});
4. html代碼
<table id="mytable">
<tr>
<th id="sort_header">Facility name</th>
<th>Phone #</th>
<th id="city_header">City</th>
<th>Speciality</th>
</tr>
<tr>
<td>CCC</td>
<td>00001111</td>
<td>Amsterdam</td>
<td>GGG</td>
</tr>
...
</table>
網(wǎng)上有很多解決方案,很多都基于jQuery。
jquery.tablesorter,大小17KB,不過他的首頁在ie10下兼容性有點問題。
DataTables,大小75KB,功能強大,帶分頁,搜索等功能。
還有插件叫sortElements,很小巧,只有3KB,兼容性也不錯,而且在Github上有818個星。
最后我選擇用sortElements,實現(xiàn)很簡單:
1. 引入jQuery
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="jquery.js"></script>
2. 引入sortElements.js
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="jquery.sortElements.js"></script>
3. js 代碼
復(fù)制代碼 代碼如下:
$(document).ready(function(){
var table = $('#mytable');//table的id
$('#sort_header')//要排序的headerid
.each(function(){
var th = $(this),
thIndex = th.index(),
inverse = false;
th.click(function(){
table.find('td').filter(function(){
return $(this).index() === thIndex;
}).sortElements(function(a, b){
return $.text([a]) > $.text([b]) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}, function(){
return this.parentNode;
});
inverse = !inverse;
});
});
});
4. html代碼
復(fù)制代碼 代碼如下:
<table id="mytable">
<tr>
<th id="sort_header">Facility name</th>
<th>Phone #</th>
<th id="city_header">City</th>
<th>Speciality</th>
</tr>
<tr>
<td>CCC</td>
<td>00001111</td>
<td>Amsterdam</td>
<td>GGG</td>
</tr>
...
</table>
相關(guān)文章
微信小程序?qū)崿F(xiàn)團(tuán)購或秒殺批量倒計時
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)團(tuán)購或秒殺批量倒計時,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
用JS寫一段判斷搜索引擎來路并且屏蔽PC的跳轉(zhuǎn)代碼
以下是用JS寫的判斷搜索引擎來路并屏蔽PC跳轉(zhuǎn)的代碼,需要的朋友可以參考下2023-12-12

