only for me, not sample
1.pagination.js
ajax_pagination.PaginatedLists = {
setup: function()
{
jQuery( '#Body:not(.nojQuery) ul.listPaginated' ).each( function() { new ajax_pagination.PaginatedLists.Class( this ) } );
},
Class: function( element, noBackground )
{
var _element = element;
var _pagination;
var _itemsPerPage = 0;
var _listItems;
var _page = 0;
var _pages = 0;
function setup()
{
_itemsPerPage = _element.className.match( /listPaginated-ipp-([0-9]+)/ );
if ( _itemsPerPage )
{
_itemsPerPage = _itemsPerPage[1];
_listItems = jQuery( _element ).children( 'li' );
_pages = Math.ceil( _listItems.length / _itemsPerPage );
var firstItem = ( _page * _itemsPerPage );
var lastItem = parseInt( firstItem ) + parseInt( _itemsPerPage ) < _listItems.length ? parseInt( firstItem ) + parseInt( _itemsPerPage ) : _listItems.length;
updateListItems();
// build pagination
jQuery( _element ).siblings( '.commonPagination' ).remove();
_pagination = jQuery( '<div class="commonPagination clear" style="padding:0px"><ul class="leftPaging"><li class="first"><a/></li><li class="previous"><a/></li><li class="page"><input type="text" class="fieldText" disabled="true" value="0" size="4"/></li><li class="pageCount">of <strong>0</strong></li><li class="next"><a/></li><li class="last"><a/></li></ul><div class="listPaginationInfo">Displaying items 0 of 0</div></div>' );
_pagination.find( 'ul li.previous' ).bind( 'click', paginatePrevious );
_pagination.find( 'ul li.next' ).bind( 'click', paginateNext );
_pagination.find( 'ul li.first' ).bind( 'click', paginateFirst );
_pagination.find( 'ul li.last' ).bind( 'click', paginateLast );
// for ( var x = 0; x < _pages; x++ ) jQuery( '<li class="listPagination-page-' + x + '"><a href="#">' + ( x + 1 ) + '</a></li>' ).bind( 'click', paginate ).insertBefore( _pagination.find( 'ul li:last' ) );
updatePagination();
_pagination.insertAfter( jQuery( _element ) );
}
}
function paginate(e)
{
if ( jQuery( this ).find( 'a' ).length )
{
_page = this.className.match( /listPagination-page-([0-9]+)/ )[1];
updateListItems();
updatePagination();
}
e.preventDefault();
}
function paginatePrevious(e)
{
if ( --_page >= 0 )
{
updateListItems();
updatePagination();
}
else _page++;
e.preventDefault();
}
function paginateNext(e)
{
if ( ++_page < _pages )
{
updateListItems();
updatePagination();
}
else _page--;
e.preventDefault();
}
function paginateFirst(e)
{
_page = 0;
updateListItems();
updatePagination();
e.preventDefault();
}
function paginateLast(e)
{
if (_pages == 0 ) {return false}
_page = _pages-1;
updateListItems();
updatePagination();
e.preventDefault();
}
function updateListItems()
{
var firstItem = ( _page * _itemsPerPage );
var lastItem = parseInt( firstItem ) + parseInt( _itemsPerPage ) < _listItems.length ? parseInt( firstItem ) + parseInt( _itemsPerPage ) : _listItems.length;
// hide previous items
jQuery( _element ).find( 'li.paginatedVisible' ).removeClass( 'paginatedVisible' );
// show list items
for ( var x = firstItem; x < lastItem; x++ )
{
jQuery( _listItems[x] ).addClass( 'paginatedVisible' );
}
}
function updatePagination()
{
var firstItem = ( _page * _itemsPerPage );
var lastItem = parseInt( firstItem ) + parseInt( _itemsPerPage ) < _listItems.length ? parseInt( firstItem ) + parseInt( _itemsPerPage ) : _listItems.length;
if (_listItems.length) {
_pagination.find(".fieldText").val(_page + 1) ;
_pagination.find("li.pageCount strong").text(_pages);
} else {
_pagination.find(".fieldText").val(0) ;
_pagination.find("li.pageCount strong").text(0);
}
_pagination.find( '.listPaginationInfo' ).text( 'Displaying items ' + (lastItem==0 ? 0 : firstItem + 1 ) + '-' + lastItem + ' of ' + _listItems.length );
}
setup();
}
}
ajax_pagination.CommonPagination = {
fit: function(portlet){
var pgn = portlet.find('.commonPagination')
if(pgn.height()>30)
pgn.addClass('compactPagination')
},
setup: function(params){
var id = params.id
var viewmore = params.viewmore
var reload = params.reload
var portlet = $('#' + id)
if(window.frames.Main && $(window).index(portlet) == -1){
var portlet = $('#'+id,$(window.frames['Main'].document))
}
var pagingInfo = portlet.find('.pagingInfo')
if(pagingInfo.length==0)return
var intAttr = function(name){
return parseInt(pagingInfo.attr(name))
}
var startRow = intAttr('startRow')
var endRow = intAttr('endRow')
var startDisplayPage = intAttr('startDisplayPage')
var endDisplayPage = intAttr('endDisplayPage')
var totalPages = intAttr('totalPages')
var totalRows = intAttr('totalRows')
var currentPage = intAttr('currentPage')
//adjust pagination
//ajax_pagination.CommonPagination.fit(portlet)
//assign value
with(portlet){
find('li.page input').val(currentPage)
find('li.pageCount strong').text(totalPages)
if(viewmore && viewmore.title)
find('.inlineFooter a').html(viewmore.title + '<span>»</span>').click(viewmore.event)
else
find('.inlineFooter').remove()
find('.listPaginationInfo').text('Displaying items ' + startRow + '-' + endRow + ' of ' + totalRows)
}
//bind btn event
var roll = function(e){
var toPage = e.data?e.data.toPage:null
if(!toPage){
var btn = $(this)
if(btn.hasClass('next'))
toPage = currentPage+1
else if(btn.hasClass('previous'))
toPage = currentPage-1
else if(btn.hasClass('first'))
toPage = 1
else if (btn.hasClass('last'))
toPage = totalPages
else
return
}
if(toPage>0 && toPage<=totalPages && toPage!=currentPage && reload){
reload.params.start = (toPage-1) * reload.params.limit
reload.event(reload.params)
}
}
portlet.find( 'li.previous, li.next, li.first, li.last' ).click( roll )
//bind input event
var filterKey = function(e){
if ( ( e.keyCode < 48 || e.keyCode > 57 ) && ( e.keyCode < 96 || e.keyCode > 105 ) && ( e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 39 ) ) e.preventDefault()
}
var jump = function(e){
if ( e.keyCode == 13 ){
e.data = {toPage: $(this).val()}
roll(e)
}
}
portlet.find( 'li.page input' )
.keydown(filterKey)
.keyup(jump)
}
}
2. cvpaination.css
.commonPagination { position:relative; margin:0; padding:0px 10px 0px; border-top:1px solid #fff }
.commonPagination .commonPaginationInfo { float:left; font-size:1.1em; color:#999 }
.commonPagination .commonPaginationRPP { float:right; margin-top:-3px; font-size:1.1em; color:#999 }
.commonPagination .commonPaginationRPP .ui-dropdown { font-size:1em }
.commonPagination ul { font-size:1.1em; text-align:center }
.commonPagination ul li { float:left; height:20px; margin:0 5px 0 0; vertical-align:top }
.commonPagination ul li.first,
.commonPagination ul li.last,
.commonPagination ul li.previous,
.commonPagination ul li.next { width:13px; height:13px; overflow:hidden; margin:3px 5px 0 0; padding:1px 0; background:transparent url(../images/cvpaginationIcon.PNG) no-repeat 0 0 }
.commonPagination ul li.first { background-position:-135px -4px}
.commonPagination ul li.last { background-position:-150px -4px}
.commonPagination ul li.previous { background-position:-105px -4px }
.commonPagination ul li.next { background-position:-120px -4px }
.commonPagination ul li.page { margin-right:3px !important }
.commonPagination ul li.first a,
.commonPagination ul li.last a,
.commonPagination ul li.previous a,
.commonPagination ul li.next a { display:block; height:100% }
.commonPagination ul li.first a:hover,
.commonPagination ul li.last a:hover,
.commonPagination ul li.previous a:hover,
.commonPagination ul li.next a:hover { text-decoration:none }
.commonPagination ul li.pageCount { padding-top:3px; white-space:nowrap }
.commonPagination ul li.page input.fieldText { width:25px; padding:3px 0; text-align:center }
.commonPagination .listPaginationInfo { float:right; font-size:1.1em; color:#999; margin-top:2px }
.commonPagination .inlineFooter { float:left; margin-left:42px; margin-top: 2px;white-space:nowrap }
.compactPagination {padding: 10px 0 5px; }
.compactPagination .inlineFooter {margin-left: 0; float: right}
.compactPagination .listPaginationInfo {text-align: right}
3.cvpagination.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
var ajax_pagination = {};
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="application"/>
<link rel="stylesheet" href="${ctx}/css/cvpaination.css" />
<script type="text/javascript"
src="${ctx}/js/jquery-1.3.2.js"></script>
<script type="text/javascript"
src="${ctx}/js/pagination/Pagination.js"></script>
<title>CV Paging Jquery</title>
</head>
<body>
<!-- Include portlet content in file securitized_inventory_content.jsp -->
<div id="dataTableContent">
<jsp:include page="test/cvpageAction.action">
<jsp:param name="start" value="0" />
<jsp:param name="limit" value="10" />
</jsp:include>
</div>
<script type="text/javascript">
<!--
$(function() {
var params={
url: '${ctx}/ajax1/cvpageAction.action',
portletId: 'PORTLET_INSTANCE_ID',
start: 0,
limit: '10',
maxSize:'15',
minSize:'10'
};
CV.setup(params);
//CV.loadDatas(params);
});
CV = {
loadDatas:function(params){
var url = params.url + "?start=" + params.start + "&limit=" + params.limit;
$("#dataTableContent").load(url,function(){
CV.setup(params);
});
},
//dynamic modify pagination
setup:function(params){
ajax_pagination.CommonPagination.setup({
id: params.portletId,
//viewmore: {title: 'More' , event: function(){}},
reload:{event:CV.loadDatas,params:params}
});
//TEST.CommonPagination.fit($("#" + params.portletId));
}
};
//-->
</script>
</body>
</html>
4.cvpagination_content.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<s:iterator value="rows">
<s:property value="issueid" />
<s:property value="caption" />
<s:property value="maketop" />
<s:property value="issue" />
<br />
</s:iterator>
<jsp:include page="pagination.htm" />
<div class="pagingInfo hidden"
startRow='${pagingBean.startRow + 1}' endRow='${pagingBean.endRow}'
startDisplayPage='${pagingBean.startDisplayPage}'
endDisplayPage='${pagingBean.endDisplayPage}'
totalPages='${pagingBean.totalPages}'
totalRows='${pagingBean.totalRows}'
currentPage='${pagingBean.currentPage}' >
</div>
5.Action
package com.tian.web.action;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.googlecode.jsonplugin.annotations.JSON;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.tian.web.model.InfoIssue;
import com.tian.web.utils.AbstractAction;
/**
* @author xt40919
* @version Create Time:Jan 19, 2011 4:26:18 PM
*/
public class CVPaginationAction extends AbstractAction {
/**
*
*/
private static final long serialVersionUID = 7608948588506966683L;
List<InfoIssue> rows = new ArrayList<InfoIssue>();
@SuppressWarnings("static-access")
@Override
public String execute() {
Connection conn = null;
Statement stat = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root",
"");
String strSql = "select count(*) from ISSUE";
stat = (Statement) conn.createStatement();
ResultSet rs = (ResultSet) stat.executeQuery(strSql);
int totalRows =0;
while (rs.next()) {
// get total count
totalRows = rs.getInt(1);
}
// count total pages
String strPaing = "SELECT * FROM ISSUE order by maketop LIMIT "+ this.getStart()+ ","+ this.getLimit()+" ; ";
this.doSetPagingBean(totalRows);
rs = (ResultSet) stat.executeQuery(strPaing);
while (rs.next()) {
InfoIssue ii = new InfoIssue();
ii.setIssueid(rs.getInt("issueid"));
ii.setCaption(rs.getString("caption"));
ii.setMaketop(rs.getString("maketop"));
ii.setIssue(rs.getString("issued"));
rows.add(ii);
}
} catch (Exception e) {
return ERROR;
} finally {
try {
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
//System.out.print("error");
} catch (SQLException e) {
}
}
try {
Thread.currentThread().sleep(3 * 1000);
} catch (InterruptedException e) {
}
return SUCCESS;
}
@JSON(name = "rows")
public List<InfoIssue> getRows() {
return rows;
}
public void setRows(List<InfoIssue> rows) {
this.rows = rows;
}
}
6. abstract class
package com.tian.web.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
public abstract class AbstractAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
private static final long serialVersionUID = 1L;
private String isMax;
private String limit;
private boolean noPaging;
private PagingBean pagingBean;
private String portletId;
protected HttpServletRequest request;
protected HttpServletResponse response;
private String start;
protected void doSetPagingBean(int totalRows) throws Exception{
int iTotalRows = 0;
try {
iTotalRows = totalRows;//Integer.parseInt(totalRows);
} catch(NumberFormatException ex) {
}
int iStart = Integer.parseInt(this.getStart());
int iRowsPerPage = Integer.parseInt(this.getLimit());
int iTotalPages = iTotalRows%iRowsPerPage == 0?iTotalRows/iRowsPerPage:iTotalRows/iRowsPerPage+ 1;
int iCurrentPage = iStart/iRowsPerPage + 1;
if("true".equals(this.getIsMax())){
setPagingBean(new PagingBean(iTotalRows, iTotalPages, iRowsPerPage, iCurrentPage, 15));
}else{
setPagingBean(new PagingBean(iTotalRows, iTotalPages, iRowsPerPage, iCurrentPage, 5));
}
}
public String getIsMax() {
return isMax;
}
public String getLimit() {
return limit;
}
public PagingBean getPagingBean() {
return pagingBean;
}
public String getPortletId() {
return portletId;
}
public String getStart() {
return start;
}
public boolean isNoPaging() {
return noPaging;
}
public void setIsMax(String isMax) {
this.isMax = isMax;
}
public void setLimit(String limit) {
this.limit = limit;
}
public void setNoPaging(boolean noPaging) {
this.noPaging = noPaging;
}
public void setPagingBean(PagingBean pagingBean) {
this.pagingBean = pagingBean;
}
public void setPortletId(String portletId) {
this.portletId = portletId;
}
public void setServletRequest(HttpServletRequest req) {
this.request = req;
}
public void setServletResponse(HttpServletResponse resp) {
this.response = resp;
}
public void setStart(String start) {
this.start = start;
}
}
7.
package com.tian.web.utils;
public class PagingBean {
private Integer totalRows;
private Integer totalPages;
private Integer currentPage;
private Integer rowsPerPage;
private Integer startDisplayPage = 1;
private Integer endDisplayPage = 10;
private Integer allDisplayPagings = 10;
private Integer startRow;
private Integer endRow;
public PagingBean(Integer totalRows, Integer totalPages, Integer rowsPerPage, Integer currentPage, Integer allDisplayPagings) {
super();
this.allDisplayPagings = allDisplayPagings;
this.totalRows = totalRows;
this.currentPage = totalRows==0 ? 0 : currentPage;
this.rowsPerPage = rowsPerPage;
this.totalPages = totalPages;
startDisplayPage = (currentPage-allDisplayPagings/2) >0 ? currentPage-allDisplayPagings/2 : 1;
endDisplayPage = startDisplayPage+allDisplayPagings <=totalPages ? startDisplayPage+allDisplayPagings-1 : totalPages;
// if(totalPages <= endDisplayPage ){
// this.endDisplayPage = totalPages+1;
// }
setStartRow(startRow);
setEndRow(endRow);
//System.out.println("startRow: "+startRow);
//System.out.println("endRow: "+endRow);
}
public PagingBean(Integer totalRows, Integer totalPages, Integer rowsPerPage, Integer currentPage ) {
this(totalRows, totalPages, rowsPerPage, currentPage, 10 );
}
public Integer getTotalRows() {
return totalRows;
}
public void setTotalRows(Integer totalRows) {
this.totalRows = totalRows;
}
public Integer getStartRow() {
return startRow;
}
public void setStartRow(Integer startRow) {
int start = this.currentPage * rowsPerPage - rowsPerPage;
if (null != startRow && startRow > start) {
this.startRow = startRow;
} else {
this.startRow = this.totalRows == 0 ? 0 : this.currentPage
* rowsPerPage - rowsPerPage;
}
}
public Integer getEndRow() {
return endRow;
}
public void setEndRow(Integer endRow) {
this.endRow = totalPages>=1
?(this.currentPage*rowsPerPage>this.totalRows?this.totalRows : this.currentPage*rowsPerPage)
:this.totalRows;
}
public Integer getStartDisplayPage() {
return startDisplayPage;
}
public void setStartDisplayPage(Integer startDisplayPage) {
this.startDisplayPage = startDisplayPage;
}
public Integer getEndDisplayPage() {
return endDisplayPage;
}
public void setEndDisplayPage(Integer endDisplayPage) {
this.endDisplayPage = endDisplayPage;
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getRowsPerPage() {
return rowsPerPage;
}
public void setRowsPerPage(Integer rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}
@Override
public String toString() {
return "totalRows: "+totalRows+" totalPages: "+totalPages+ " currentPage: "+currentPage+" rowsPerPage: "+rowsPerPage+" startRow: "+startRow+" endRow: "+endRow+" startDisplayPage: "+this.startDisplayPage+" endDisplayPage: "+this.endDisplayPage;
}
}