首页 > 代码库 > ext GridPanel分页

ext GridPanel分页

grid.js

Ext.onReady(function() {
	var gridStore = new Ext.data.Store({
		
		proxy: new Ext.data.HttpProxy({
			url: 'grid.asp'
		}),
		reader: new Ext.data.JsonReader({
			root: 'data',
			totalProperty: 'totalCount',
			idProperty: 'id'
		},[
		    {name: 'id',mapping:'id'},
			{name: 'name',mapping: 'name'},
			{name: 'email',mapping: 'email'},
			{name: 'age',mapping: 'age'},
		]),
		baseParams:{
			start:0,
			limit:5
		}
	});
	
	gridStore.load();
	var colModel = new Ext.grid.ColumnModel([
	       {id: 'userid',header: '用户号',dataIndex: 'id',sortable: true,hidden:true},
	       {header: '姓名',dataIndex: 'name',sortable: true,width: 80},
	       {header: '邮箱',dataIndex: 'email',width: 150},
	       {header: '年龄',dataIndex: 'age',width: 60},
	       ]);
	
	var gridPanel = new Ext.grid.GridPanel({
		id: 'oprPanel',
		title: '用户信息',
		frame: true,
		//iconCls: 'T104',
		border: true,
		columnLines: true,
		stripeRows: true,
		autoHeight: true,
		store: gridStore,
		sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
		cm: colModel,
		collapsible: true,
		//plugins: oprRowExpander,
		loadMask: {
			msg: '正在加载用户信息列表......'
		},
		bbar: new Ext.PagingToolbar({
			store: gridStore,
			pageSize: 5,
			displayInfo: true,
			displayMsg: '显示第{0}-{1}条记录,共{2}条记录',
			emptyMsg: '没有找到符合条件的记录'
		})
	});
	var viewport = new Ext.Viewport({
		 layout: "fit",
         items: gridPanel
    });
});


grid.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
	href=http://www.mamicode.com/"ext/resources/css/ext-all.css" />>
Data.java

package com.lin.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class Data {
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Id
	private int id;
	@Column(length=20)
	private String name;
	@Column(length=30)
	private String email;
	@Column
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Data(int id, String name, String email, int age) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.age = age;
	}

	public Data() {
		super();
	}

}
DataDao.java
package com.lin.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.lin.model.Data;

@Repository
public class DataDao {
	
	private SessionFactory sessionFactory;


	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Transactional
	public List<Data> getPageData(int start, int limit) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Data");
		query.setFirstResult(start);
		query.setMaxResults(limit);
		System.out.println("size ================ "+query.list().size());
		return query.list();
	}
	@Transactional
	public int getTotal(){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Data");
		return query.list().size();
	}
}

GridAction.java

package com.lin.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lin.dao.DataDao;
import com.lin.model.Data;
import com.opensymphony.xwork2.ActionSupport;

public class GridAction extends ActionSupport {
	@Autowired
	private DataDao dao;
	private int start;
	private int limit;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=utf-8");
		PrintWriter pw = response.getWriter();
		List list = dao.getPageData(start, limit);
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		map.put("totalCount", dao.getTotal());
		map.put("data",list);
		JSONObject json = JSONObject.fromObject(map);
		pw.append(json.toString());
		pw.flush();
		pw.close();
		return NONE;
	}
}

效果:


数据库: