2009-05-06

Pagination in Confluence plugin

2nd part in Confluence plugin development series..
Confluence provides items pagination out of the box but I couldn't find any reference in developer documentation. Standard page navigation bar looks like this:

Here are the easy steps to implement it for your own list of items:
  1. extend your action class from AbstractEntityPaginationAction
  2. public class ViewUsers 
    extends AbstractEntityPaginationAction {
    }
    AbstractEntityPaginationAction provides
    protected bucket.core.actions.PaginationSupport paginationSupport;
    which you can use in your action:
    public String execute() throws Exception {
    List users = ... // load users here
    paginationSupport.setItems(users);
    paginationSupport.setPageSize(preferredPageSize);
    return SUCCESS;
    }
  3. insert pagination macro in viewusers.vm page
  4. #pagination($action.paginationSupport "page.action?")
    where page.action is the action name which will be called when you select a page. Pagination macro then adds a parameter startIndex, that's why you need a question mark (?) at the end

  5. implement page.action
  6. public String page() throws Exception {
    HttpServletRequest req =
    ServletActionContext.getRequest();
    int startIndex =
    Integer.parseInt(req.getParameter("startIndex"));
    List users = ... // load users
    paginationSupport.setItems(users);
    paginationSupport.setPageSize(preferredPageSize);
    paginationSupport.setStartIndex(startIndex);
    return SUCCESS;
    }
  7. view current items in page
  8. #foreach( $user in 
    $action.paginationSupport.page.iterator())
    ...
    #end
    PaginationSupport allows to control page size (number of items per page) so you may provide a selector in web page for page size and save it in user session.
    That's it!

2 comments:

  1. Did you ever try to have pagination for a custom macro built in confluence ? Are you aware of any macro which does pagination ?

    ReplyDelete
  2. I didn't do this myself by maybe following way could help.
    if you need pagination in usual page, you inherit action from *AbstractEntityPaginationAction* which contains
    *protected bucket.core.actions.PaginationSupport paginationSupport*.

    In your plugin you could directly create *paginationSupport* and fill it with actual data.

    .vm page for macro could contain pagination bar as in ordinary page
    * #pagination($action.paginationSupport "page.action?") *

    last thing to do - pass selected page number to main page and then back to macro as an additional parameter.

    Sorry I don't have such concrete experience so maybe wrong in some points.
    Good luck!

    ReplyDelete