Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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