现在的位置: 首页 > 综合 > 正文

分页实现

2013年11月23日 ⁄ 综合 ⁄ 共 3794字 ⁄ 字号 评论关闭

package com.itbbs.model.info;

/***
 * @author chengchan.glun
 * @since itbbs1.3 2012/3/31
 */
public class PagesInfo {

	/* 资源一级分类 */
	private int parentType;

	/* 资源分类的二级参数 */
	private int childType;

	/* 页面总数 */
	private int totle;

	/* 每一页数目 */
	private int pageSize;

	/* 当前页面 */
	private int pageCurrent;

	/* 要检索开始页 */
	private int pageBegin;

	/* 最大页面数 */
	private int pageMaxResult;

	public int getChildType() {
		return childType;
	}

	public int getPageBegin() {

		setPageBegin((getPageCurrent() - 1) * getPageSize());

		return pageBegin;
	}

	public int getPageCurrent() {

		if (pageCurrent == 0)
			setPageCurrent(1);

		return pageCurrent;

	}

	public int getPageMaxResult() {

		if (0 == pageMaxResult)
			setPageMaxResult(10);

		return pageMaxResult;
	}

	public int getPageSize() {

		if (pageSize == 0)
			setPageSize(10);

		return pageSize;

	}

	public int getParentType() {
		return parentType;
	}

	public int getTotle() {
		return totle;
	}

	public void setChildType(int childType) {
		this.childType = childType;
	}

	public void setPageBegin(int pageBegin) {
		this.pageBegin = pageBegin;
	}

	public void setPageCurrent(int pageCurrent) {

		this.pageCurrent = pageCurrent;

	}

	public void setPageMaxResult(int pageMaxResult) {
		this.pageMaxResult = pageMaxResult;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setParentType(int parentType) {
		this.parentType = parentType;
	}

	public void setTotle(int totle) {
		this.totle = totle;
	}

}

action:

package com.itbbs.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.itbbs.base.BaseAction;
import com.itbbs.model.User;
import com.itbbs.model.info.PagesInfo;
import com.itbbs.service.SrcManagerService;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author chengchang.lun
 * @category pages
 * @see PagesInfo
 */
@Component("paging")
@Scope("prototype")
public class PagingAction extends BaseAction implements ModelDriven<Object> {

	private PagesInfo info = new PagesInfo();

	private List<com.itbbs.model.Resource> resources;

	private SrcManagerService service;

	private static final long serialVersionUID = -7907305202778167199L;

	@Override
	public String execute() throws Exception {

		User user = (User) getSession().getAttribute("user");

		if (null == user)
			return "login";

		setResources(service.sort(info.getChildType(), info.getPageBegin(),
				info.getPageMaxResult()));

		return SUCCESS;
	}

	public String getTotal() {

		info.setTotle(service.getTotal(info.getChildType()));

		try {
			// String json = "{\"total\":" + info.getTotle() + "}";
			getResponse().getWriter().print("<!--" + info.getTotle() + "-->");

		} catch (IOException e) {
			e.printStackTrace();
		}

		return NONE;

	}

	public Object getModel() {

		return info;
	}

	public List<com.itbbs.model.Resource> getResources() {
		return resources;
	}

	public SrcManagerService getService() {
		return service;
	}

	public void setResources(List<com.itbbs.model.Resource> list) {
		this.resources = list;
	}

	@Resource(name = "srcManagerService")
	public void setService(SrcManagerService service) {
		this.service = service;
	}
}

js:

 

$(function() {
	/** *********************Cheng.changlun********* */

	var total = 0;// 总条数
	var childType = $("#for_childType").val();// 要查询的 类别ID

	$.ajax( {

		url : "paging!getTotal?childType=" + childType,
		type : "GET",
		success : function(data) {

			total = data;

			var pageNum = 0;
			if (total == 0)
				return;
			else if (total % 10 == 0) {
				pageNum = total / 10;
			} else {
				pageNum = parseInt(total / 10 + 1);
			}// 多少页=pageNum
		var $div = $("#paging");// div
		var $tr = $("#paging table tr");// tr

		var $td = "";// td
		if (pageNum == 1) {

			$td = "<td><a href='paging?pageCurrent=1&childType=" + childType
					+ "'>首页</a></td>";
			$tr.append($td);

			return;
		}

		$td = "<td><a href='paging?pageCurrent=1&childType=" + childType
				+ "'>首页</a></td>";

		var index = 0;
		for (index = 2; index < pageNum; index = index + 1) {

			if (index > 10) {
				$td += "<td>...</td>";
				index = pageNum;
				break;
			}

			$td += "<td><a href='paging?pageCurrent=" + index + "&childType="
					+ childType + "'>第" + index + "页</a></td>";
		}
		$td += "<td><a href='paging?pageCurrent=" + index + "&childType="
				+ childType + "'>尾页</a></td>";

		$tr.append($td);// 追加到jsp
	}// end success
	});// end $.ajax()
	/** ****************************************************************end********** */
});

简单吧,不过耗了我一个晚上。

抱歉!评论已关闭.