smartGWT ListGrid分页页面组件GridPager

2 篇文章 0 订阅

smartGWT没有分页组件,参考网上一个分页组件GridPager。

GridPager必须和ListGrid结合使用。
原理 :隐藏ListGrid的滚动条,将原来的鼠标滚动改成由按钮来一次性滚动指定固定条数,将剩下的记录隐藏。数据的获取有ListGrid自动向后台异步获取,每次获取的记录数由分页的页面大小决定。
优点 :能很好保存查询条件,不需要重复创建ListGrid对象,使用简单。
使用方式:

GridPager gridPager = new GridPager(groupListGrid,10); //新建一个分页面板,第一个参数是ListGrid对象,第二个参数为默认页面大小
VLayout gridPanel=new VLayout();
gridPanel.addMember(groupListGrid);
gridPanel.addMember(gridPager) ;

 


附:GridPager源码

GridPager.java

 

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class GridPager extends ToolStrip
{

boolean deselect=true;

public boolean isDeselect()
{
return deselect;
}

public void setDeselect(boolean deselect)
{
this.deselect = deselect;
}

// ---------------------- interal state
/**
* How many row in each pager.
*/
private int pageSize = 50;
/**
* Current page.
*/
private int pageNum;
/**
* The actual grid.
*/
private final ListGrid grid;

// visual component
protected Label firstLabel;
protected Label nextLabel;
DynamicForm pageForm;
protected TextItem pageText;
protected Label previousLabel;
protected Label lastLabel;
protected Label totalLabel;



public GridPager(ListGrid listGrid, int pageSize)
{
this(listGrid, pageSize, 20, 22);
}

/**
*
* @param listGrid
* the listGrid
* @param pageSize
* how many rows in each page.
* @param cellHeight
* cellHeight for the listGrid.
* @param headerHeight
* headerHeight for the listGrid.
*/
public GridPager(ListGrid listGrid, int pageSize, int cellHeight, int headerHeight)
{
this.grid = listGrid;
this.pageSize = pageSize;

firstLabel = new Label();
firstLabel.setWidth(40);
firstLabel.setStyleName("fakelink");
firstLabel.setContents("First");
firstLabel.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
goToPage(1);
}
});

previousLabel = new Label();
previousLabel.setWidth(20);
previousLabel.setContents("<<");
previousLabel.setStyleName("fakelink");
previousLabel.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
goToPage(pageNum - 1);
}
});

pageText = new TextItem();
pageText.setWidth(30);
pageText.setShowTitle(false);
pageText.addChangedHandler(new ChangedHandler()
{

public void onChanged(ChangedEvent event)
{
if (event.getValue() != null)
{
int value = Integer.parseInt(event.getValue().toString());
goToPage(value);
}
}
});

nextLabel = new Label();
nextLabel.setContents(">>");
nextLabel.setWidth(20);
nextLabel.setStyleName("fakelink");
nextLabel.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
goToPage(pageNum + 1);
}
});

lastLabel = new Label();
lastLabel.setWidth(40);
lastLabel.setStyleName("fakelink");
lastLabel.setContents("Last");
lastLabel.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
goToPage(getTotalPages());
}
});

totalLabel = new Label();
totalLabel.setWrap(false);
totalLabel.setWidth(50);

setHeight(20);
setOverflow(Overflow.VISIBLE);
setStyleName("normal");

pageForm = new DynamicForm();
pageForm.setNumCols(1);
pageForm.setFields(pageText);
pageForm.setWidth(30);

addMember(firstLabel);
addMember(previousLabel);

addMember(pageForm);
addMember(nextLabel);
addMember(lastLabel);
addMember(totalLabel);
setAlign(Alignment.RIGHT);

// --------------set grid height
grid.setCellHeight(cellHeight);
grid.setHeaderHeight(headerHeight);
grid.setHeight(cellHeight * pageSize + headerHeight);
grid.setBodyOverflow(Overflow.HIDDEN);
grid.setDataPageSize(pageSize);
grid.setDrawAheadRatio(2.0f);
// listen grid events
grid.addDataArrivedHandler(new DataArrivedHandler()
{
public void onDataArrived(DataArrivedEvent event)
{
GWT.log("onDataArrived is called", null);
goToPage(pageNum);
}
});
// grid.addDrawHandler(new DrawHandler()
// {
//
// public void onDraw(DrawEvent event)
// {
// goToPage(1);
//
// }
// });

}

public void goToPage(int pageNum)
{
GWT.log("go to page " + pageNum, null);
// clamp to the end of the possible set of pages
int pages = getTotalPages();
if (pageNum > pages)
pageNum = pages;
if (pageNum < 1)
pageNum = 1;

if (pageNum == this.pageNum)
{
updatePagerControls(pages);
return;
}
// update the buttons
this.pageNum = pageNum;
updatePagerControls(pages);
if (deselect)
grid.deselectAllRecords();
int cellHeight = grid.getCellHeight();
int rowNum = (pageNum - 1) * pageSize;

// here: give extra 2 pixes. This is a hack. Otherwise,
// listGrid think the last row from previous page is visible although it
// is not visible
grid.scrollBodyTo(null, rowNum * cellHeight + 2);

}

private int getTotalPages()
{
int total = this.grid.getTotalRows();
int pages = (int) Math.ceil(((float) total) / ((float) pageSize));
// never return zero pages
if (pages == 0)
pages = 1;
return pages;
}

protected void updatePagerControls(int total)
{
// pageText.setValue(pageNum);
if (pageNum == 1)
{
if (firstLabel.isVisible())
firstLabel.hide();
if (previousLabel.isVisible())
previousLabel.hide();
} else
{
if (!firstLabel.isVisible())
firstLabel.show();
if (!previousLabel.isVisible())
previousLabel.show();
}
if (pageNum == total)
{
if (lastLabel.isVisible())

lastLabel.hide();
if (nextLabel.isVisible())
nextLabel.hide();
} else
{
if (!lastLabel.isVisible())
lastLabel.show();
if (!nextLabel.isVisible())
nextLabel.show();
}
if (total >= 4)
{
if (!pageForm.isVisible())
pageForm.show();
} else
{
if (pageForm.isVisible())
pageForm.hide();
}

totalLabel.setContents("(" + pageNum + "/" + total + ")");

}

}

功能: 实现数据分页查询显示 特点: 全是免费工具及开源技术 开发框架: gwt1.4.6 + spring2.5 + mysql5.0 开发工具: Eclipse3.4 Cypal Studio for GWT (Eclipse 的一个插件) 实现原理:通过GWT的RPC来调用Spring 传过来的服务器端数据 注意:需要的jar包 * gwt-user.jar * gwt-servlet.jar * mysql-connectot-java-3.1.12.jar * spring.jar * commons-dbcp.1.2.1.jar * coommons-pool-1.4.jar Sql语句: create database booksearch; create table book ( id int not null auto_increment primary key, title varchar(100), isbn varchar(100), edition varchar(100), msrp varchar(100) ) insert into book(title,isbn,edition,msrp) values('title_1','i-sbn-1','editon-1','msrp-1'); insert into book(title,isbn,edition,msrp) values('title_2','i-sbn-2','editon-2','msrp-2'); insert into book(title,isbn,edition,msrp) values('title_3','i-sbn-3','editon-3','msrp-3'); insert into book(title,isbn,edition,msrp) values('title_4','i-sbn-4','editon-4','msrp-4'); insert into book(title,isbn,edition,msrp) values('title_5','i-sbn-5','editon-5','msrp-5'); insert into book(title,isbn,edition,msrp) values('title_6','i-sbn-6','editon-6','msrp-6'); insert into book(title,isbn,edition,msrp) values('title_7','i-sbn-7','editon-7','msrp-7'); insert into book(title,isbn,edition,msrp) values('title_8','i-sbn-8','editon-8','msrp-8'); insert into book(title,isbn,edition,msrp) values('title_9','i-sbn-9','editon-9','msrp-9'); insert into book(title,isbn,edition,msrp) values('title_10','i-sbn-10','editon-10','msrp-10');
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值