jQuery排序表数据 [英] Jquery sort table data
本文介绍了jQuery排序表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对使用jquery对表中的tds进行排序感到震惊。
I got struck in sorting tds in table using jquery.
我的演示小提琴
在项目中如何为具有id的任何表调用它?
How can I call it for any table with id in my project?
var $sort = this;
var $table = $('#mytable');
var $rows = $('tbody > tr',$table);
$rows.sort(function(a, b) {
var keyA = $('td:eq(0)',a).text();
var keyB = $('td:eq(0)',b).text();
if($($sort).hasClass('asc')) {
return (keyA > keyB) ? 1 : 0;
} else {
return (keyA < keyB) ? 1 : 0;
}
});
推荐答案
类似这样的东西
function sortTable(table, order) {
var asc = order === 'asc',
tbody = table.find('tbody');
tbody.find('tr').sort(function(a, b) {
if (asc) {
return $('td:first', a).text().localeCompare($('td:first', b).text());
} else {
return $('td:first', b).text().localeCompare($('td:first', a).text());
}
}).appendTo(tbody);
}
可以在任何这样的表上调用
could be called on any table like this
sortTable($('#mytable'),'asc');
这篇关于jQuery排序表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文