Tuesday, 8 September 2009

Leaf - Super Easy Paging for Tapestry


Leaf is a Tapestry 5 component that makes paging in tapestry applications super easy by inserting fully customisable paging links at the top and bottom of the rendered items. You can loop through the items on the page in the same way as the loop component.
<t:leaf.leaf source="source" value="item">
<h4>${item.title}</h4>
<p>${item.description}</p>
</t:leaf.leaf>

All the component requires is a PageSource implementation and a local value to populate the current paged item.
public class MyPage {

private static final int PAGE_AMOUNT = 10;

@Property
private PageItem item;
@Property
private PageSource source;

@Inject
private PageItemService pageItemService;

private int page;

void onActivate(int page) {
this.page = page;
}

void setupRender() throws IOException {

source = new PageSource(page, PAGE_AMOUNT) {

@Override
public Page loadPage(int start, int limit) {
return pageItemService.getPageItems(start, limit);
}
@Override
public Object[] getContext(int page) {
return new Object[]{page};
}
};
}
}

Customise the paging with the format parameter. This is a comma separated list of elements that determines the order and elements that get rendered. Two more parameters; top and bottom can also be provided to prevent the paging from getting displayed at either the top of bottom of the list.
<t:leaf.leaf source="source" value="item" 
format="textb,first,previous,pages,next,last" bottom="false">

To start using leaf paging add the norobot repository and leaf dependency to your pom.xml
<repository>
<id>tapestry</id>
<url>http://norobot.googlecode.com/svn/maven2</url>
</repository>
...
<dependency>
<groupId>org.norobot</groupId>
<artifactId>leaf</artifactId>
<version>1.1</version>
</dependency>

No comments:

Post a Comment