GWT例子中的分页研究.

GWT demo中的分页研究.

 

Mail 类是主要的模块.

 

 private MailList mailList; 是分页所需要的主要的类.

 

public class MailList extends Composite implements ClickHandler {
 

 

MailList下面有一个

private FlexTable table = new FlexTable ();

是用来动态显示分页数据的类.

 

private HorizontalPanel navBar = new HorizontalPanel ();

navBar是用来显示 [上一页][下一页] 这样的信息的.

 

 public MailList() {
    // Setup the table.
    table.setCellSpacing(0);
    table.setCellPadding(0);
    table.setWidth("100%");

    // Hook up events.
    table.addClickHandler(this);
    newerButton.addClickHandler(this);
    olderButton.addClickHandler(this);

    // Create the 'navigation' bar at the upper-right.
    HorizontalPanel innerNavBar = new HorizontalPanel();
    navBar.setStyleName("mail-ListNavBar");
    innerNavBar.add(newerButton);
    innerNavBar.add(countLabel);
    innerNavBar.add(olderButton);

    navBar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
    navBar.add(innerNavBar);
    navBar.setWidth("100%");

    initWidget(table);
    setStyleName("mail-List");

    initTable();
    update();
  }

 在构造函数里面进行数据的初始化.

 

因为MailList实现了ClickHander.所以属性添加监听的时候可以添加this

 

table.addClickHandler(this);
    newerButton.addClickHandler(this);
    olderButton.addClickHandler(this);

 

其实在

 public void onClick(ClickEvent event) {
    Object sender = event.getSource();
    if (sender == olderButton) {

 方法里面进行判断.是那个按钮发出的相应然后在做处理.

可以从Event中得到发出响应的部件的类.

 

然后就可以进行事件处理了..

 

仿照这个自己写了一个类:

 

private Button firestButton = new Button("首页");
	private Button previousButton = new Button("上一页");
	private Button nextButton = new Button("下一页");
	private Button lastButton = new Button("末页");
	private Button jumpButton1 = new Button();
	private Button jumpButton2 = new Button();
	private Button jumpButton3 = new Button();
	private Button jumpButton4 = new Button();
	private Button jumpButton5 = new Button();
	private int startRow, endRow = 0;
	private int totalRows = 100;
	private int maxRows = 10;
	private HTML showPageInfo = new HTML("", true);
	/* 设置分页显示的信息. */
	private FlexTable dateTable = new FlexTable();
	/* 数据存放的table */
	private VerticalPanel mainPanel = new VerticalPanel();
	/* 主要窗体. */
	private HorizontalPanel jumpPanel = new HorizontalPanel();

	public UserPageList() {

		mainPanel.setWidth("100%");/* 设置显示100% */
		dateTable.setCellSpacing(0);
		dateTable.setCellPadding(0);
		dateTable.setWidth("100%");
		dateTable.setBorderWidth(1);/* 添加一个border=0 */

		/* 为按钮添加时间相应. */
		dateTable.addClickHandler(this);
		firestButton.addClickHandler(this);
		previousButton.addClickHandler(this);
		nextButton.addClickHandler(this);
		lastButton.addClickHandler(this);
		jumpButton1.addClickHandler(this);
		jumpButton2.addClickHandler(this);
		jumpButton4.addClickHandler(this);
		jumpButton5.addClickHandler(this);

		/* 设置按钮条. */
		HorizontalPanel navBar = new HorizontalPanel();
		HorizontalPanel innerNavBar = new HorizontalPanel();
		navBar.setStyleName("mail-ListNavBar");
		innerNavBar.add(showPageInfo);
		innerNavBar.add(firestButton);
		innerNavBar.add(previousButton);
		innerNavBar.add(jumpPanel);
		innerNavBar.add(nextButton);
		innerNavBar.add(lastButton);
		/* 添加按钮. */

		navBar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		navBar.add(innerNavBar);
		navBar.setWidth("100%");

		mainPanel.add(dateTable);
		mainPanel.add(navBar);
		setWidget(mainPanel);
		setWidth("700px");
		setText("用户分页演示.");// 并不是setTitle设置标题.
		initTable();
	}

	public void onClick(ClickEvent event) {/* 分页事件添加. */
		Object obj = event.getSource();
		if (firestButton == obj) {/* 第一页. */
			startRow = 0;
			endRow = startRow + maxRows;
			initTable();
		} else if (previousButton == obj) {/* 前一页 */
			startRow -= maxRows;
			if (startRow < 0) {
				startRow = 0;
			}
			endRow = startRow + maxRows;
			initTable();
		} else if (nextButton == obj) {/* 下一页 */
			if (!(startRow + maxRows >= totalRows)) {
				startRow += maxRows;
				endRow = startRow + maxRows;
			}
			initTable();
		} else if (lastButton == obj) {/* 末页. */
			endRow = totalRows;
			startRow = totalRows - maxRows;
			if (startRow < 0) {
				startRow = 0;
			}
			initTable();
		} else if (jumpButton1 == obj) {
			jumpPage(-2);
		} else if (jumpButton2 == obj) {
			jumpPage(-1);
		} else if (jumpButton4 == obj) {
			jumpPage(1);
		} else if (jumpButton5 == obj) {
			jumpPage(2);
		}
	}

	private void initTable() {
		// Create the header row.
		if (startRow == 0 && endRow == 0) {
			endRow = maxRows;
		}
		/* 设置标题. */
		dateTable.setText(0, 0, "ID");
		dateTable.setText(0, 1, "用户名");
		dateTable.setText(0, 2, "邮编");
		dateTable.setText(0, 3, "其他");

		int j = 1;
		for (int i = startRow; i < endRow; ++i) {/* 随机填入数据.将数据放到dataTable里面. */
			dateTable.setText(j, 0, "aa" + i);
			dateTable.setText(j, 1, "aa" + Random.nextInt());
			dateTable.setText(j, 2, "bb" + Random.nextInt());
			dateTable.setText(j, 3, "cc");
			j++;
		}
		showPageInfo.setHTML("开始:\t" + startRow + "记录\t结束:\t" + endRow + "记录");/*
																				 * 更新显示信息.
																				 */
		int currentPage = (startRow / maxRows) + 1;
		int maxPage = (totalRows / maxRows) ;
		System.out.println("currentPage:\t"+currentPage+"\tmaxPage:\t"+maxPage);
		for (int i = 0; i < jumpPanel.getWidgetCount(); i++) {
			jumpPanel.remove(i);
		}
		if ((currentPage - 2) >= 1) {
			jumpButton1.setText("" + (currentPage - 2));
			jumpPanel.add(jumpButton1);
			System.out.print("\t");
			System.out.print(currentPage - 2);
		}
		if ((currentPage - 1) > 1) {
			jumpButton2.setText("" + (currentPage - 1));
			jumpPanel.add(jumpButton2);
			System.out.print("\t");
			System.out.print(currentPage - 1);
		}
		jumpButton3.setText("" + currentPage);
		jumpPanel.add(new Button("" + currentPage));
		System.out.print("\t");
		System.out.print(currentPage);
		if ((currentPage + 1) < maxPage) {
			jumpButton4.setText("" + (currentPage + 1));
			jumpPanel.add(jumpButton4);
			System.out.print("\t");
			System.out.print(currentPage + 1);
		}
		if ((currentPage + 2) <= maxPage) {
			jumpButton5.setText("" + (currentPage + 2));
			jumpPanel.add(jumpButton5);
			System.out.print("\t");
			System.out.print(currentPage + 2);
		}
		System.out.println();
	}

	private void jumpPage(int pageNo) {
		int currentPage = (startRow / maxRows) + 1;
		System.out.println("pageNo:\t" + pageNo + "currentPage:\t"
				+ currentPage);
		startRow = startRow + (maxRows * pageNo);
		endRow = startRow + maxRows;
		initTable();
	}

 

 



 如图显示.目前这个还有一个小的bug.

 

在分页点击的时候.后台显示的是正确的.但是页面的组件有的时候会多出来几个button.

 

好像是没有清除掉该删除的东西.

 

还得仔细研究下呢.

 

附件是代码.

 

在添加上数据库操作就是一个ajax的分页了.这样开发一个分页.一个ajax的效率还是挺快的.

 

基本上是没有去弄那个麻烦的javascript.

 

速度也还是挺快的.想要看看什么类下面的方法都是很方便的呢.

 

  • 大小: 37.1 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
功能: 实现数据分页查询显示 特点: 全是免费工具及开源技术 开发框架: 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');

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值