Tip: Click lines to highlight, hold ctrl/cmd to multi-select
pager (13-Dec @ 16:20)
Syntax Highlighted Code
- /*
- * Pager - jQuery plugin to format serch result pagers
- */
- ;(function($) {
- function assertHasClass(element, c, errormsg) {
- if(!$(element).hasClass(c)) {throw errormsg;}
- }
- $.extend($.fn, {
- showPager: function(callback, size) {
- var PAGER_SIZE = parseInt(size) || 14;
- $(this).each(function() {
- assertHasClass(this, 'pager', "element must be a pager");
- var pages = jQuery('.page:not(.nextPage, .previousPage)', this);
- if(pages.length <= PAGER_SIZE) {
- pages.show();
- } else {
- var current = pages.index(pages.filter('.currentPage'));
- if(current<=PAGER_SIZE-4) {
- pages.each(function(i) {
- var page = jQuery(this);
- if(i === PAGER_SIZE - 2){
- page.replaceWith('<span>...</span>');
- }else if(i<PAGER_SIZE - 1) {
- page.show();
- } else if(i === pages.length - 1) {
- page.show();
- }
- });
- } else if(current > pages.length-(PAGER_SIZE-2)) {
- pages.each(function(i) {
- var page = jQuery(this);
- if(i === 0){
- page.show();
- }else if(i === 1){
- page.replaceWith('<span>...</span>');
- }else if(i > pages.length - (PAGER_SIZE-1)) {
- page.show();
- }
- });
- } else {
- pages.each(function(i) {
- var page = jQuery(this);
- if(i === 0){
- page.show();
- }else if(i === 1){
- page.replaceWith('<span>...</span>');
- }else if(i === pages.length -1) {
- page.show();
- }else if(i === pages.length - 2){
- page.replaceWith('<span>...</span>');
- }else if(i > (current - Math.floor(PAGER_SIZE/2)+2) && (i < (current + Math.floor(PAGER_SIZE/2) - 1))) {
- page.show();
- }
- });
- }
- }
- if(typeof callback == 'function') {
- jQuery('a.page', this).click(function() {
- callback(this.href.slice(this.href.lastIndexOf('#')+1, this.href.length));
- });
- }
- return this;
- });
- }
- });
- })(jQuery);
- <%@ attribute name="current" required="true" rtexprvalue="true" type="java.lang.Integer" %>
- <%@ attribute name="pages" required="true" rtexprvalue="true" type="java.util.Collection" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
- <span class="pager">
- <c:choose>
- <c:when test="${current > 1}"><a class="page previousPage" href="#${current - 1}"><fmt:message key="general.previous"/></a></c:when>
- <c:otherwise><span class="page previousPage"><fmt:message key="general.previous"/></span></c:otherwise>
- </c:choose>
- <c:forEach var="page" items="${pages}">
- <c:choose>
- <c:when test="${page == current}">
- <span class="page currentPage" style="display: none">${page}</span>
- </c:when>
- <c:when test="${page == -1}">
- <span class="page break">...</span>
- </c:when>
- <c:otherwise>
- <a class="page" href="#${page}" style="display: none">${page}</a>
- </c:otherwise>
- </c:choose>
- </c:forEach>
- <c:choose>
- <c:when test="${current < fn:length(pages)}"><a class="page nextPage" href="#${current + 1}"><fmt:message key="general.next"/></a></c:when>
- <c:otherwise><span class="page nextPage"><fmt:message key="general.next"/></span></c:otherwise>
- </c:choose>
- </span>
Plain Code
/*
* Pager - jQuery plugin to format serch result pagers
*/
;(function($) {
function assertHasClass(element, c, errormsg) {
if(!$(element).hasClass(c)) {throw errormsg;}
}
$.extend($.fn, {
showPager: function(callback, size) {
var PAGER_SIZE = parseInt(size) || 14;
$(this).each(function() {
assertHasClass(this, 'pager', "element must be a pager");
var pages = jQuery('.page:not(.nextPage, .previousPage)', this);
if(pages.length <= PAGER_SIZE) {
pages.show();
} else {
var current = pages.index(pages.filter('.currentPage'));
if(current<=PAGER_SIZE-4) {
pages.each(function(i) {
var page = jQuery(this);
if(i === PAGER_SIZE - 2){
page.replaceWith('<span>...</span>');
}else if(i<PAGER_SIZE - 1) {
page.show();
} else if(i === pages.length - 1) {
page.show();
}
});
} else if(current > pages.length-(PAGER_SIZE-2)) {
pages.each(function(i) {
var page = jQuery(this);
if(i === 0){
page.show();
}else if(i === 1){
page.replaceWith('<span>...</span>');
}else if(i > pages.length - (PAGER_SIZE-1)) {
page.show();
}
});
} else {
pages.each(function(i) {
var page = jQuery(this);
if(i === 0){
page.show();
}else if(i === 1){
page.replaceWith('<span>...</span>');
}else if(i === pages.length -1) {
page.show();
}else if(i === pages.length - 2){
page.replaceWith('<span>...</span>');
}else if(i > (current - Math.floor(PAGER_SIZE/2)+2) && (i < (current + Math.floor(PAGER_SIZE/2) - 1))) {
page.show();
}
});
}
}
if(typeof callback == 'function') {
jQuery('a.page', this).click(function() {
callback(this.href.slice(this.href.lastIndexOf('#')+1, this.href.length));
});
}
return this;
});
}
});
})(jQuery);
<%@ attribute name="current" required="true" rtexprvalue="true" type="java.lang.Integer" %>
<%@ attribute name="pages" required="true" rtexprvalue="true" type="java.util.Collection" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<span class="pager">
<c:choose>
<c:when test="${current > 1}"><a class="page previousPage" href="#${current - 1}"><fmt:message key="general.previous"/></a></c:when>
<c:otherwise><span class="page previousPage"><fmt:message key="general.previous"/></span></c:otherwise>
</c:choose>
<c:forEach var="page" items="${pages}">
<c:choose>
<c:when test="${page == current}">
<span class="page currentPage" style="display: none">${page}</span>
</c:when>
<c:when test="${page == -1}">
<span class="page break">...</span>
</c:when>
<c:otherwise>
<a class="page" href="#${page}" style="display: none">${page}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${current < fn:length(pages)}"><a class="page nextPage" href="#${current + 1}"><fmt:message key="general.next"/></a></c:when>
<c:otherwise><span class="page nextPage"><fmt:message key="general.next"/></span></c:otherwise>
</c:choose>
</span>