如何引入使用javascript动态html表的分页和排序?
问题描述:
我的html页面包含ajax,它有助于动态创建表格。如何引入使用javascript动态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>
在运行时,我可能有100行填充在表中。我怎样才能添加分页,使用jQuery对这张表进行排序?
答
您可以使用数据表你的目的: https://datatables.net/examples/basic_init/alt_pagination.html
您还可以使用的tablesorter: http://tablesorter.com/docs/
答
下面是使用jQuery
each(),index(),toggle()和Anonymous function一个非常简单的例子。我利用HTML 5
data-* 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>
由于该教程是非常有帮助的。在上面的指南中,我正在面对一个数据库中的bug。我在这里发布了新的问题。可以吗?请检查?https://stackoverflow.com/questions/46780285/datatable-pagination-is-not-working – Ratha