2017-10-25 47 views
1

테이블 머리글을 클릭하여 테이블 열 문자열 값을 정렬하려고합니다. 여기 알파벳 정렬 작업을 수행했습니다. 이 코드를 문자열 열 정렬로 변경하십시오.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(); 
    }); 
} 
+0

당신이 "DataTable을"구현하려고 했습니까? 좋은 라이브러리이고 그 기능은 이미 쉽고 재미 있습니다 : https://datatables.net/ –

답변

0

여기에 테이블을 정렬 할 수있는 jQuery 플러그인입니다 : http://tablesorter.com/

+0

고맙습니다. 일하고있어! – Kuppu

+0

위아래 화살표가 보이지 않습니다. 어떻게해야할까요? – Kuppu

+0

화살표가 css -> url (path/to/arrow.png)에 링크되어있을 수 있습니다. 콘솔에서 404를 확인하고 링크를 확인하십시오. –

1

는 기본적으로 정렬 옵션을 얻을 것이다 거기 JQuery와 데이터 테이블 JS를 사용하고 또한 등 필터, 페이지 매김라는 더 많은 옵션을 사용할 수 있습니다 ... reference

$(document).ready(function(){ 
    $('#myTable').DataTable(); 
}); 
+0

고맙습니다. 일하고있어! – Kuppu