테이블 머리글을 클릭하여 테이블 열 문자열 값을 정렬하려고합니다. 여기 알파벳 정렬 작업을 수행했습니다. 이 코드를 문자열 열 정렬로 변경하십시오.Jquery에서 헤더를 클릭하여 테이블 열을 정렬하는 방법은 무엇입니까?
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();
});
}
당신이 "DataTable을"구현하려고 했습니까? 좋은 라이브러리이고 그 기능은 이미 쉽고 재미 있습니다 : https://datatables.net/ –