分页
目前页面展现了所有的用户任务。然而,随着时间的推移会出现更多的任务,单个表中很快就会出现大量任务。这种情形下非常适合使用多页面表。
Tapestry 提供方便的 Table 组件及其低级别的支持,比如来自 Contrib Library 模块的 TableView,如清单 17、18 所示:
清单 17. Tapestry 页面说明
<component id="receivedItemsView" type="contrib:TableView">
<binding name="source" value="ognl:itemsTableModel"/>
<binding name="columns" value="itemId, subject, creator, recipient"/>
<binding name="pageSize" value="10"/>
</component>
<component id="receivedItemsColumns" type="contrib:TableColumns" />
<component id="receivedItemsRows" type="contrib:TableRows" />
<component id="receivedItemsValues" type="contrib:TableValues" />
<component id="receivedItemsPages" type="contrib:TablePages">
<binding name="pagesDisplayed" value="10"/>
</component>
<binding name="source" value="ognl:itemsTableModel"/>
<binding name="columns" value="itemId, subject, creator, recipient"/>
<binding name="pageSize" value="10"/>
</component>
<component id="receivedItemsColumns" type="contrib:TableColumns" />
<component id="receivedItemsRows" type="contrib:TableRows" />
<component id="receivedItemsValues" type="contrib:TableValues" />
<component id="receivedItemsPages" type="contrib:TablePages">
<binding name="pagesDisplayed" value="10"/>
</component>
清单 18. Tapestry 中的 Java 类
public IBasicTableModel getItemsTableModel() {
return new IBasicTableModel() {
public int getRowCount() {
return getActionItemManager().getActionItemsCountByRecipient(uid);
}
public Iterator getCurrentPageRows(int nFirst, int nPageSize,
ITableColumn objSortColumn, boolean bSortOrder) {
return getActionItemManager()
.getActionItemsListByRecipient(uid, nFirst, nPageSize);
}
};
}
return new IBasicTableModel() {
public int getRowCount() {
return getActionItemManager().getActionItemsCountByRecipient(uid);
}
public Iterator getCurrentPageRows(int nFirst, int nPageSize,
ITableColumn objSortColumn, boolean bSortOrder) {
return getActionItemManager()
.getActionItemsListByRecipient(uid, nFirst, nPageSize);
}
};
}
清单 19. Tapestry HTML 标记
<span jwcid="table">
<table>
<tr><span jwcid="receivedItemsColumns" class="title"/></tr>
<tr jwcid="receivedItemsRows">
<td jwcid="receivedItemsValues"/>
</tr>
</table>
<span jwcid="receivedItemsPages"/>
</span>
<table>
<tr><span jwcid="receivedItemsColumns" class="title"/></tr>
<tr jwcid="receivedItemsRows">
<td jwcid="receivedItemsValues"/>
</tr>
</table>
<span jwcid="receivedItemsPages"/>
</span>
Wicket 一同提供了一个 DataView 类和一个 IDataProvider 实现,如下所示:
清单 20. Wicket 的 DataView 类
public class ItemsDataProvider implements IdataProvider {
public Iterator iterator(int first, int count) {
return getActionItemManager().getActionItemsListByRecipient(uid, first, count);
}
public int size() {
return getActionItemManager().getActionItemsCountByRecipient(uid);
}
public IModel model(Object object) {
return new LoadableDetachableModel(object);
}
}
public ListActionItems extends WebPage {
public ListActionItems() {
DataView dataView = new DataView("receivedItemsView", new ItemsDataProvider()) {
protected void populateItem(final Item item) {
ActionItem todo = (ActionItem) item.getModelObject();
item.add(new Label("itemId", String.valueOf(todo.getItemId()));
item.add(new Label("subject", todo.getSubject()));
item.add(new Label("creator", todo.getCreator()));
item.add(new Label("recipient", todo.getRecipient()));
}
};
dataView.setItemsPerPage(10);
add(dataView);
add(new PagingNavigator("receivedItemsPages", dataView));
}
}
public Iterator iterator(int first, int count) {
return getActionItemManager().getActionItemsListByRecipient(uid, first, count);
}
public int size() {
return getActionItemManager().getActionItemsCountByRecipient(uid);
}
public IModel model(Object object) {
return new LoadableDetachableModel(object);
}
}
public ListActionItems extends WebPage {
public ListActionItems() {
DataView dataView = new DataView("receivedItemsView", new ItemsDataProvider()) {
protected void populateItem(final Item item) {
ActionItem todo = (ActionItem) item.getModelObject();
item.add(new Label("itemId", String.valueOf(todo.getItemId()));
item.add(new Label("subject", todo.getSubject()));
item.add(new Label("creator", todo.getCreator()));
item.add(new Label("recipient", todo.getRecipient()));
}
};
dataView.setItemsPerPage(10);
add(dataView);
add(new PagingNavigator("receivedItemsPages", dataView));
}
}
清单 21. Wicket 的 DataView HTML 标记
<table>
<tr>
<th>ID</th>
<th>Subject</th>
<th>Creator</th>
<th>Recipient</th>
</tr>
<tr wicket:id="receivedItemsView">
<td><span wicket:id="itemId">ID</span></td>
<td><span wicket:id="subject">Subject</span></td>
<td><span wicket:id="creator">Creator</span></td>
<td><span wicket:id="creationDate">Creation Date</span></td>
<td><span wicket:id="deadline">Deadline</span></td>
</tr>
</table>
<span wicket:id="receivedItemsPages">Paging navigator links</span>
<tr>
<th>ID</th>
<th>Subject</th>
<th>Creator</th>
<th>Recipient</th>
</tr>
<tr wicket:id="receivedItemsView">
<td><span wicket:id="itemId">ID</span></td>
<td><span wicket:id="subject">Subject</span></td>
<td><span wicket:id="creator">Creator</span></td>
<td><span wicket:id="creationDate">Creation Date</span></td>
<td><span wicket:id="deadline">Deadline</span></td>
</tr>
</table>
<span wicket:id="receivedItemsPages">Paging navigator links</span>