我想通过单击表标题对表列字符串值进行排序。在这里我做了字母表排序。请将此代码更改为字符串列排序。
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();
});
}发布于 2017-10-25 15:07:06
使用jquery datatable js,您将获得默认的排序选项,还可以使用更多名为过滤器、分页等的选项……reference
$(document).ready(function(){
$('#myTable').DataTable();
});发布于 2017-10-25 15:03:21
这是一个对表格进行排序的jQuery插件:http://tablesorter.com/
https://stackoverflow.com/questions/46925843
复制相似问题