2017-10-16 3 views
0

내 html 페이지에 테이블을 동적으로 생성하는 데 도움이되는 ajax가 포함되어 있습니다.자바 스크립트를 사용하여 동적 HTML 테이블의 페이지 매김 및 정렬을 소개하는 방법은 무엇입니까?

<title>Clients</title> 
</head> 
<body> 
<table style="width:100%" id="clients_data"> 
<caption>Clients</caption> 
    <tr> 
    <th>Clients</th> 
    <th>Number of Sites</th> 
    <th>Reset the Processing</th> 
    </tr> 
    </table> 
<script> 
var myTable; 

$(document).ready(function() { 
    loadCustomers(); 
}); 


function loadCustomers() { 
    $.ajax({ 
     type: 'GET', 
     url: 'http://localhost:8080/cache/getCustomers', 
     dataType: 'json', 
     success: function(data) { 
      var rows = [];  
      $.each(data,function(id,value) { 
         rows.push('<tr><td><a href="clientSiteInfo.html?client='+id+'">'+id+'</td><td>'+value+'</td><td><button type="button" onclick="reset(\''+id+'\')">Reset</td></tr>'); 
        }); 
      $('#clients_data').append(rows.join('')); 
     } 
    }); 
}; 
</script> 
</body> 
</html> 

런타임에 나는 테이블에 100s의 행을 채울 수 있습니다. jquery를 사용하여이 테이블을 정렬하여 페이지 매김을 추가하는 방법은 무엇입니까?

답변

2

당신은 당신의 목적을 위해 Datatables를 사용할 수 있습니다 또한 tablesorter에 사용할 수 있습니다 https://datatables.net/examples/basic_init/alt_pagination.html

: 여기 http://tablesorter.com/docs/

+0

감사합니다. 언급 된 가이드에서 datable의 버그에 직면하고 있습니다. 나는 여기에 새로운 질문을 게시했습니다. 확인해 주실 수 있습니까? https : //stackoverflow.com/questions/46780285/datatable-pagination-is-not-working – Ratha

1

jQueryeach(), index(), toggle()Anonymous function를 사용하여 매우 기본적인 예이다. HTML 5data-* attributes을 활용하여 내 위치를 추적하고 증가/감소 할 항목 수를 설정합니다.

플러그인을 사용하거나 자신 만의 코드를 작성하여 페이지 매김을 원할 경우 간단하거나 복잡하게 만들 수 있습니다. AJAX을 사용하여 결과를 채울 것을 강력히 권장하지만 숨기기/표시 할 결과를 1000 개로드하면 시스템 속도가 느려질 수 있습니다. 튜토리얼은 매우 도움이 될 것으로

/* Variable Defaults */ 
 
var total = $('tbody > tr').length; 
 
var position = $('tbody').data('position'); 
 
var jump  = $('tbody').data('jump'); 
 
var paginate = function(position, jump) { 
 
    /* Show Default Items */ 
 
    $('tbody > tr').each(function() { 
 
     /* Variable Defaults */ 
 
     var index = $(this).index(); 
 

 
     /* Condition */ 
 
     var condition = (index >= position) && (index < position + jump); 
 

 
     /* Hide/Show Item */ 
 
     $(this).toggle(condition); 
 

 
     /* Set Disabled Status */ 
 
     $('.less').prop('disabled', (position - jump) < 0); 
 
     $('.more').prop('disabled', (position + jump) >= total); 
 
    }); 
 
}; 
 

 
/* Set Default Text */ 
 
$('.count').text(jump); 
 

 
/* Init Paginate */ 
 
paginate(position, jump); 
 

 
/* Bind Click Events to "Less" and "More" Button */ 
 
$('.less, .more').on('click', function() { 
 
    /* Decrease/Increase Position */ 
 
    position = $(this).hasClass('less') ? $('tbody').data('position') - jump : $('tbody').data('position') + jump; 
 

 
    /* Paginate */ 
 
    paginate(position, jump); 
 

 
    /* Update Position */ 
 
    $('tbody').data('position', position); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <thead> 
 
    <tr> 
 
     <td>ID</td> 
 
     <td>Name</td> 
 
     <td>Email</td> 
 
    </tr> 
 
    </thead> 
 
    <tbody data-position="0" data-jump="2"> 
 
    <tr> 
 
     <td>1</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>2</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>3</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>4</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>5</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>6</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>7</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>8</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>9</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    <tr> 
 
     <td>10</td> 
 
     <td>Test McTester</td> 
 
     <td>[email protected]</td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 

 
<button class="less">Back <span class="count">0</span></button> 
 
<button class="more">Forwards <span class="count">0</span></button>