用jQuery进行表格排序 [英] Table sorting with jquery
本文介绍了用jQuery进行表格排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何实现表格排序根据Jquery提供的任何列。我不想要任何插件。
解决方案
我们可以使用jquery。
var $ tbody = $('table tbody'); $ tbody.find('tr')。sort(function(a,b){var tda = $(a).find('td:eq('+ ColumnIndex +')')。text(); // Use你希望的列索引var tdb = $(b).find('td:eq('+ ColumnIndex +')')。text(); //使用你希望的列索引//如果一个< b返回1 return tda > tdb?1 // else if a> b return -1:tda< tdb?-1 // else they are equal - return 0:0;})。appendTo($ tbody);
使用<而不是>降序。
How to implement table Sorting According to it's any column by Jquery. I don't want any plugin. Just by pure jquery.
解决方案
We can use jquery.
var $tbody = $('table tbody');
$tbody.find('tr').sort(function (a, b) {
var tda = $(a).find('td:eq(' + ColumnIndex + ')').text(); // Use your wished column index
var tdb = $(b).find('td:eq(' + ColumnIndex + ')').text(); // Use your wished column index
// if a < b return 1
return tda > tdb ? 1
// else if a > b return -1
: tda < tdb ? -1
// else they are equal - return 0
: 0;
}).appendTo($tbody);
Use < instead of >for descending.
这篇关于用jQuery进行表格排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文