first of all, to be clear i am a newbie with javascript and jquery.
now, i am using a javascript file to paginate inside a div. the script works fine, but if there is a lot of data then i need to limit the paginating links with a "next" and a "previous" tag. i can't find a way to do that. currently the pagination script is showing all the numbers like 1 2 3 4 5 6 7 8 9 etc depending on the data supplied. but, i want to show them like, 1 2 3 4 5 next>> or, <<prev 2 3 4 5 6 next>>
here is my pagination.js
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 = "<b>Pages</b>: <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);
}
}
to get the desired look, how do i edit this script?if anyone could help me with it, please do so.
You could change renderControls like this:
var renderControls = function(container, currentPage, numPages, pagesCutoff) {
var pagingControls = "<b>Pages</b>: <ul>";
var prevPosition = currentPage - pagesCutoff;
var nextPosition = currentPage + pagesCutoff;
for (var i = 1; i <= numPages; i++) {
if (i != currentPage) {
if(i >= prevPosition && i <= nextPosition) {
var linkToPage = i == prevPosition ? currentPage - 1 : i == nextPosition ? currentPage + 1 : i;
var linkText = i == prevPosition ? "<< prev" : i == nextPosition ? "next >>" : i;
pagingControls += "<li><a href='#' onclick='pager.showPage(" + linkToPage + "); return false;'>" + linkText + "</a></li>";
}
} else {
pagingControls += "<li>" + i + "</li>";
}
}
pagingControls += "</ul>";
$(container).html(pagingControls);
}
And add this to Imtech.Pager: this.pageLinksEachSide = 3;
And change the renderControls call to this: renderControls(this.pagingControlsContainer, this.currentPage, this.numPages(), this.pageLinksEachSide);
This is assuming that you want the "next" link to navigate to currentPage + 1 and the "prev" link to navigate to currentPage - 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With