Last active
September 30, 2015 09:39
-
-
Save mlanin/8841e7e626cdd288c3c2 to your computer and use it in GitHub Desktop.
Simple bootstrap "previous / next" table pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table class="table with-pager"> | |
<thead> | |
<tr> | |
<th>Order</th> | |
<th>Name</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td>1</td> | |
<td>Foo</td> | |
</tr> | |
<tr> | |
<td>2</td> | |
<td>Bar</td> | |
</tr> | |
<tr> | |
<td>3</td> | |
<td>Baz</td> | |
</tr> | |
</tbody> | |
</table> | |
<nav> | |
<ul class="pager"> | |
<li class="previous"><a href="#"><span aria-hidden="true">←</span> Older</a></li> | |
<li class="next"><a href="#">Newer <span aria-hidden="true">→</span></a></li> | |
</ul> | |
</nav> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
$('table.with-pager').each(function() { | |
var $table = $(this); | |
var $nextPage = $('.pager .next'); | |
var $previousPage = $('.pager .previous'); | |
var currentPage = 0; | |
var numPerPage = 2; | |
var numRows = 0; | |
var numPages = 0; | |
$table.bind('repaginate', function() { | |
numRows = $table.find('tbody tr').length; | |
numPages = Math.ceil(numRows / numPerPage); | |
$table.find('tbody tr').hide().slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show(); | |
if (currentPage == 0) { | |
$previousPage.addClass('disabled'); | |
} else { | |
$previousPage.removeClass('disabled'); | |
} | |
if (currentPage == numPages-1) { | |
$nextPage.addClass('disabled'); | |
} else { | |
$nextPage.removeClass('disabled'); | |
} | |
}); | |
$table.trigger('repaginate'); | |
$previousPage.bind('click', function(event) { | |
if (currentPage != 0) { | |
currentPage--; | |
$table.trigger('repaginate'); | |
} | |
}); | |
$nextPage.bind('click', function(event) { | |
if (currentPage != numPages-1) { | |
currentPage++; | |
$table.trigger('repaginate'); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment