Javascript jquery sortElement:按表数据的数值排序
这是我的建议 从小提琴中可以看到,Javascript jquery sortElement:按表数据的数值排序,javascript,jquery,sorting,Javascript,Jquery,Sorting,这是我的建议 从小提琴中可以看到,total列包含当前按文本排序的数字 Q:如何根据数值对total列进行排序 var table = $('table'); $('#facility_header, #city_header, #total') .wrapInner('<span title="sort this column"/>') .each(function(){ var th = $(this),
total
列包含当前按文本排序的数字
Q:如何根据数值对total
列进行排序
var table = $('table');
$('#facility_header, #city_header, #total')
.wrapInner('<span title="sort this column"/>')
.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(){
// parentNode is the element we want to move
return this.parentNode;
});
inverse = !inverse;
});
});
var table=$('table');
$(“#设施#标题,#城市#标题,#总计”)
.wrapInner(“”)
.each(函数({
var th=$(此),
thIndex=th.index(),
逆=假;
点击(函数(){
table.find('td').filter(函数(){
返回$(this).index()==thIndex;
}).分拣元件(功能(a、b){
返回$.text([a])>$.text([b])?
相反?-1:1
:逆?1:-1;
},函数(){
//parentNode是我们要移动的元素
返回此.parentNode;
});
逆=!逆;
});
});
在比较这些值之前,必须先解析它们
.sortElements(function(a, b){
var sa = $.text([a]);
var sb = $.text([b]);
var ia = parseInt(sa);
var ib = parseInt(sb);
if (!isNaN(ia) && !isNaN(ib)) {
return ia > ib ? (inverse ? -1 : 1) : (inverse ? 1 : -1);
}
return sa > sb ? (inverse ? -1 : 1) : (inverse ? 1 : -1);
}
这是你的最新消息