displaytag external paging/sorting and getting true row number

10,476

You should be able to calculate the correct overall index value by referencing the page number which is in the request.

Code something like this your TableDecorator class should work:

public String getIndex() {
   int numItemsPerPage = 100;
   int page = Integer.parseInt(getPageContext().getRequest().getParameter("page"));
   int index = getListIndex();

   return ((page - 1) * numItemsPerPage) + index + 1;
}
Share:
10,476
Michael C. Rosenstein
Author by

Michael C. Rosenstein

Full-stack programmer, data modeler, UI designer, and problem solver. I have mastered the full web stack, including back- and front-end development, requirements gathering, data modeling, data ETL, query optimization, content production, and WordPress customization. I also have a talent for clean, intuitive user-interface design, and am most proud of my work on regendbase.org. In addition, I have managed teams of developers in fast-paced startup environments, and am able to quickly understand the business context for a project. In past positions, I have learned about genetics, environmental toxicology, regenerative biology, laboratory animal management protocols, and the music and publishing industries. I also bring a strong set of professional values. I take great pride in my work and am a continuous learner. I believe in clean code, documentation, and leaving things better than I found them. I love getting things done and solving new problems. My favorite days are those that leave me with a puzzle to solve overnight.

Updated on June 04, 2022

Comments

  • Michael C. Rosenstein
    Michael C. Rosenstein almost 2 years

    I'm using external paging/sorting with a custom TableDecorator and the following DisplayTag table in a JSP:

    <display:table id="ixnlist" name="pageScope.itemList" sort="external"
      decorator="org.mdibl.ctd.pwa.displaytag.decorator.IxnTableWrapper">
    
       <display:column title="Row" property="rowNum" />
    
       ...more columns...
    </display:table> 
    

    In the table decorator, getListIndex() returns the row number relative only to the current page, not to the overall list (i.e., if we're displaying 100 objects per page, then getListIndex() returns "0" at the top of page 2, not "100").

    /**
     * Returns the row number data for the current row.
     *
     * @return String containing row number heading.
     */
    public String getRowNum() {
        final StringBuilder out = new StringBuilder(8);
        out.append(nf.format(getListIndex() + 1))
           .append('.');
        return out.toString();
    }
    

    Is it possible in the table decorator to somehow get the row number reflecting the correct offset? Displaytag is aware of the offset someplace, as it uses it to format the pagination links.

    The displaytag docs do not address this question, and the ${row_rowNum} implicit object works identically to getListIndex() in the decorator.

    Yes, it's possible to do this by adding a row-number column to the paginated SQL and having the TableDecorator use that if available, but I'd rather not rely on the DAO for that kind of metadata. The following TableDecorator method takes advantage of a rownum column if it exists, otherwise it uses getListIndex():

    /**
     * Returns the row number data for the current row.
     *
     * @return String containing row number heading.
     */
    public String getRowNum() {
        final StringBuilder out = new StringBuilder(8);
        final Map row = (Map) getCurrentRowObject();
    
        // Use 'rnum' column for external pagination if it exists.
        // Kludgy way of doing this.
        if (row.get("rnum") != null) {
            out.append(nf.format(row.get("rnum")));
        } else {
            out.append(nf.format(getListIndex() + 1));
        }
        out.append('.');
        return out.toString();
    }
    

    Thanks.

    /mcr

  • Michael C. Rosenstein
    Michael C. Rosenstein almost 15 years
    Thanks, Marcus. This is a great approach, but is there a way to find out the numItemsPerPage dynamically? I've been looking for some way to access the original PaginatedList passed into the table tag, but to no avail (unless you hard-code the name of the request param--but not practical for us b/c the PaginatedList object name varies). Thanks again.
  • Marcus Leon
    Marcus Leon almost 15 years
    The only way I could think of doing this is to access the PaginatedList from the request. Maybe create another request param that stores the name of your PaginatedList object?? The new param could be set dynamically in your servlet class.
  • Michael C. Rosenstein
    Michael C. Rosenstein almost 15 years
    Thanks again. BTW, I think the formula should be: "return ((page - 1) * numItemsPerPage) + index + 1;"
  • Marcus Leon
    Marcus Leon almost 15 years
    You're welcome. I updated the formula as you correctly described.