Skip to content

Instantly share code, notes, and snippets.

@slackero
Last active June 29, 2018 13:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slackero/6444d258af4b4fdebf41 to your computer and use it in GitHub Desktop.
Save slackero/6444d258af4b4fdebf41 to your computer and use it in GitHub Desktop.
Auto paginate DOM elements and paginate through it using twbs-pagination http://esimakin.github.io/twbs-pagination/
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="jquery.twbsPagination.js" type="text/javascript"></script>
<!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<style>
.item-pages > blockquote,
.item-page {
display: none;
}
</style>
<title>jQuery Pagination plugin</title>
</head>
<body data-spy="scroll" data-target="#navigation">
<ul id="pagination"></ul>
<div id="item-pages" class="item-pages">
<blockquote>Eintrag erster</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag</blockquote>
<blockquote>Eintrag letzter</blockquote>
</div>
<script>
$(function(){
var paginate = $('#item-pages');
var paginateChildren = paginate.children();
var totalItems = paginateChildren.length;
var itemsPerPage = 5;
var totalPages = Math.ceil(totalItems / itemsPerPage);
if(totalItems > itemsPerPage) {
var tempPage, startItem, lastItemm, currentPage;
for(var page = 1; page <= totalPages; page++) {
tempPage = $('<div id="item-page-'+page+'" class="item-page'+(page === 1 ? ' active' : '')+'" />');
startItem = (page-1) * itemsPerPage;
lastItem = startItem + itemsPerPage;
if(lastItem >= totalItems) {
lastItem = totalItems;
}
while (startItem < lastItem) {
paginateChildren.eq(startItem).appendTo(tempPage);
startItem++;
}
paginate.append(tempPage);
if(page === 1) {
currentPage = $('#item-page-'+page, paginate);
}
}
$('#pagination').twbsPagination({
totalPages: totalPages,
visiblePages: 5,
first: 'Erste',
prev: 'Zurück',
next: 'Nächste',
last: 'Letzte',
onPageClick: function (event, page) {
currentPage.hide();
currentPage = $('#item-page-'+page, paginate);
currentPage.show();
}
});
}
});
</script>
</body>
</html>
@maliayan
Copy link

maliayan commented Mar 13, 2018

There is a typo @line103 (lastItemm), FYI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment