/    Sign up×
Community /Pin to ProfileBookmark

Paging for large content

To page my large content i am using JQuery with the script in the example below set to display 3 paragraphs per page:

[QUOTE]

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.1 Transitional//EN”>
<html>
<head>
<script type=”text/javascript” src=”jquery-1.3.2.min.js”></script>
<style type=”text/css”>
#pagingControls ul { display: inline; padding-left: 0.10em; }
#pagingControls li { display: inline; padding: 0 0.5em; font-size: 10pt; }
</style>
<script type=”text/javascript”>
var Imtech = {};
Imtech.Pager = function() {
this.paragraphsPerPage = 3;
this.currentPage = 1;
this.pagingControlsContainer = “#pagingControls”;
this.pagingContainerPath = “#content”;

this.numPages = function() {
var numPages = 0;
if (this.paragraphs != null && this.paragraphsPerPage != null) {
numPages = Math.ceil(this.paragraphs.length / this.paragraphsPerPage);
}

return numPages;
};

this.showPage = function(page) {
this.currentPage = page;
var html = “”;
for (var i = (page-1)*this.paragraphsPerPage; i < ((page-1)*this.paragraphsPerPage) + this.paragraphsPerPage; i++) {
if (i < this.paragraphs.length) {
var elem = this.paragraphs.get(i);
html += “<” + elem.tagName + “>” + elem.innerHTML + “</” + elem.tagName + “>”;
}
}

$(this.pagingContainerPath).html(html);

renderControls(this.pagingControlsContainer, this.currentPage, this.numPages());
}

var renderControls = function(container, currentPage, numPages) {
var pagingControls = “Pages: <ul>”;
for (var i = 1; i <= numPages; i++) {
if (i != currentPage) {
pagingControls += “<li><a href=’#’ onclick=’pager.showPage(” + i + “); return false;’>” + i + “</a></li>”;
} else {
pagingControls += “<li>” + i + “</li>”;
}
}

pagingControls += “</ul>”;

$(container).html(pagingControls);
}
}

var pager = new Imtech.Pager();

$(document).ready(function() {
pager.pagingContainer = $(“#content”);
pager.paragraphs = $(“p”, pager.pagingContainer);
pager.showPage(1);
});

</script>
</head>
<body>
<div id=”pagingControls”></div>
<div id=”content”>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>Paragraph 4</p>
<p>Paragraph 5</p>
<p>Paragraph 6</p>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 9</p>
<p>Paragraph 10</p>
</div>
</body>
</html>

[/QUOTE]

My problem is my content will be up to 100 pages. Now if for example a visitor is on page eleven (11), i want the script to display 5 previous pages and 5 next pages numbers in this order:

[QUOTE]

Pages: [COLOR=”RoyalBlue”]5 6 7 8 9 10[/COLOR] 11 [COLOR=”royalblue”]12 13 14 15 16[/COLOR]

[/QUOTE]

Can someone help to modify the script to achieve this. Thanks.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @akluch2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ,
analytics: Fullres
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Marika,
tipped: article
amount: 1000 SATS,

tipper: @hatem774,
tipped: article
amount: 1 SATS,

tipper: @nearjob,
tipped: article
amount: 1000 SATS,
)...