首页  >  文章  >  web前端  >  sortElements实现table排序步骤详解

sortElements实现table排序步骤详解

php中世界最好的语言
php中世界最好的语言原创
2018-04-26 09:23:161803浏览

这次给大家带来sortElements实现table排序步骤详解,sortElements实现table排序的注意事项有哪些,下面就是实战案例,一起来看一下。

jquery.tablesorter,大小17KB,不过他的首页在ie10下兼容性有点问题。 

DataTables,大小75KB,功能强大,带分页,搜索等功能。
还有插件叫sortElements,很小巧,只有3KB,兼容性也不错,而且在Github上有818个星。
最后我选择用sortElements,实现很简单:
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 = $(&#39;#mytable&#39;);//table的id 
$(&#39;#sort_header&#39;)//要排序的headerid 
.each(function(){ 
var th = $(this), 
thIndex = th.index(), 
inverse = false; 
th.click(function(){ 
table.find(&#39;td&#39;).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>

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

jquery实现表格排序+实时搜索功能

jQuery做出圆形图标菜单轮流切换功能

以上是sortElements实现table排序步骤详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn