您的位置:首页 > Web前端 > JavaScript

extjs的grid实现分页查看功能

2012-02-02 13:04 603 查看
1.定义store

//-------------学生grid--------------
var studentStore = new Ext.data.JsonStore({
url : 'studentInfo_student.action',
root: 'studentList',
fields: [
'id',
'name'
],
totalProperty : 'page.totalRows',
baseParams : {
limit : pageSize
}
})
2.定义grid

var grid = new Ext.grid.EditorGridPanel({
id : 'gird',
title : '学生信息列表',
region : 'center',
stripeRows : true,
autoScroll : true,
loadMask : true,
store : studentStore,
clicksToEdit : 1,
frame : true,
cm : new Ext.grid.ColumnModel({
defaults : {
sortable : true
// columns are not sortable by default
},
columns : [ new Ext.grid.RowNumberer(),

{
header : '姓名',
dataIndex : 'name',
width : 100
}, {
header : '学号',
dataIndex : 'id',
width : 100
}
]
}),

bbar : new Ext.PagingToolbar({
pageSize : pageSize,
store : studentStore,
displayInfo : true,
displayMsg : '当前{0}到{1}条,共{2}条',
emptyMsg : '没有符合条件的数据'
})
})


3. store执行load()操作时,传入参数

{
xtype : 'button',
text : '查询',
listeners : {
click : function () {
studentStore.load({
params : {
start : 0
}
});
}
}


4.在后台的action类里写上如下对象,注意要添加getter and setter。student()是struts2对应的method

private int limit;
private int start;
private JSONObject page;
public String student(){
page = this.studentInfoService.getPageService(start, limit);
studentList=studentInfoService.getStudent( start, limit);
success=true;
failure=false;
return SUCCESS;
}


5.service层里的getPageService()返回查询值反馈到page

public 	JSONObject getPageService(int start,int limit){
JSONObject json = new JSONObject();
long totalRows = this.studentInfoDao.countStuInfo();
int currentPage = start/limit+1;
json.put("currentPage", currentPage);
if(currentPage==1){
json.put("firstPage", true);
}else{
json.put("firstPage", false);
}
json.put("getCount", true);
long totalPages = totalRows/limit;
if(totalRows%limit!=0){
totalPages += 1;
}
if(currentPage==totalPages){
json.put("lastPage", true);
}else{
json.put("lastPage", false);
}
json.put("pageSize", limit);
json.put("startRow", start);
json.put("totalPages", totalPages);
json.put("totalRows", totalRows);
return json;
}


6.dao层代码

public List<Student> getStudentByPage(int start,int limit){
Student s=new Student();
List<Student> list=this.getHibernateTemplate().findByExample(s, start, limit);
return list;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: