If you want to perform table pagination using jQuery instead of server side script like PHP, ASP.NET and JSP, then You can use the below code for paginatng a table:
<table class="data">
<tr>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
</tr>
<tr>
<td>Ayush</td>
<td>ayush.kamra@yahoo.com</td>
<td>8767234123</td>
</tr>
<tr>
<td>Ashish</td>
<td>Ashish.chauhan23@gmail.com</td>
<td>9219264078</td>
</tr>
<tr>
<td>Rahul</td>
<td>rahulnegi13@gmail.com</td>
<td>9087654234</td>
</tr>
<tr>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
</tr>
<tr>
<td>pankaj</td>
<td>pant.pankaj19@gmail.com</td>
<td>01352532997</td>
</tr>
</table>
CSS to decorate table:
<style>
table, th, td {
cellpadding:1px;
cellspacing:1px;
}
th{
background:pink;
}
.active {
background:red;
}
</style>
HTML table pagination using JS code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#data').after('<div id="nav-menu"></div>');
var rowsShown = 4;
var rowsTotal = $('#data tbody tr').length;
var numPages = rowsTotal/rowsShown;
for(i = 0;i < numPages;i++) {
var pageNum = i + 1;
$('#nav').append('<a href="#" rel="'+i+'">'+pageNum+'</a> ');
}
$('#data tbody tr').hide();
$('#data tbody tr').slice(0, rowsShown).show();
$('#nav a:first').addClass('active');
$('#nav a').bind('click', function(){
$('#nav a').removeClass('active');
$(this).addClass('active');
var currPage = $(this).attr('rel');
var startItem = currPage * rowsShown;
var endItem = startItem + rowsShown;
$('#data tbody tr').css('opacity','0.0').hide().slice(startItem, endItem).
css('display','table-row').animate({opacity:1}, 300);
});
});
</script>
0 Comment(s)