[英]How to sort a table column by clicking on the header in Jquery?
I want to sort a table column strings values by clicking on table header.我想通过单击表标题对表列字符串值进行排序。 here I did alphabets sorting.在这里我做了字母排序。 please change this code to String column sorting.请将此代码更改为字符串列排序。
enter code here
var sortOrder = -1;
$(".sort").click(function () {
sortOrder *= -1;
sortTable($(this));
});
function sortTable(element) {
var tbody = element.parent();
var colIndex = element.index() -1;
tbody.find('tr').sort(function (a, b)
{
$('td', a).eq(colIndex).text() === "" ? $('td', a).eq(colIndex).text("0") : $('td', a).eq(colIndex).text();
if (sortOrder === 1)
{
return parseInt($('td', a).eq(colIndex).text(), 10) - parseInt($('td', b).eq(colIndex).text(), 10);
}
else
{
return parseInt($('td', b).eq(colIndex).text(), 10) - parseInt($('td', a).eq(colIndex).text(), 10);
}
}).appendTo(tbody);
tbody.find('tr').each(function () {
$(this).removeClass();
zebra = zebra === 'odd' ? 'even' : 'odd';
$(this).addClass(zebra);
$(this).find("td").eq(colIndex).text() === 0 ? $(this).find("td").eq(colIndex).text(""):$(this).find("td").eq(colIndex).text();
});
}
这是一个用于对表格进行排序的 jQuery 插件: http : //tablesorter.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.