【问题标题】:displaytag external paging/sorting and getting true row numberdisplaytag 外部分页/排序并获取真实的行号
【发布时间】:2010-11-01 04:48:38
【问题描述】:

我在 JSP 中使用自定义 TableDecorator 和以下 DisplayTag 表进行外部分页/排序:

<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> 

在表格装饰器中,getListIndex() 只返回与当前页面相关的行号,而不是整个列表(即,如果我们每页显示 100 个对象,那么 getListIndex() 在第 2 页的顶部,而不是“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();
}

是否有可能在表装饰器中以某种方式获取反映正确偏移量的行号? Displaytag 知道某个地方的偏移量,因为它使用它来格式化分页链接。

displaytag 文档没有解决这个问题,并且 ${row_rowNum} 隐式对象的工作方式与装饰器中的 getListIndex() 相同。

是的,可以通过将行号列添加到分页 SQL 并让 TableDecorator 使用它(如果可用)来做到这一点,但我宁愿不依赖 DAO 来获取这种元数据。以下 TableDecorator 方法利用 rownum 列(如果存在),否则使用 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();
}

谢谢。

/mcr

【问题讨论】:

    标签: java pagination paging jsp-tags displaytag


    【解决方案1】:

    您应该能够通过引用请求中的页码来计算正确的总体索引值。

    这样的代码你的TableDecorator类应该可以工作:

    public String getIndex() {
       int numItemsPerPage = 100;
       int page = Integer.parseInt(getPageContext().getRequest().getParameter("page"));
       int index = getListIndex();
    
       return ((page - 1) * numItemsPerPage) + index + 1;
    }
    

    【讨论】:

    • 谢谢,马库斯。这是一个很好的方法,但是有没有办法动态找出 numItemsPerPage ?我一直在寻找某种方法来访问传递给 table 标记的原始 PaginatedList ,但无济于事(除非你硬编码请求参数的名称——但对我们来说不实用 b/c PaginatedList 对象名称会有所不同)。再次感谢。
    • 我能想到的唯一方法是从请求中访问 PaginatedList。也许创建另一个存储您的 PaginatedList 对象名称的请求参数?新参数可以在您的 servlet 类中动态设置。
    • 再次感谢。顺便说一句,我认为公式应该是:“return ((page - 1) * numItemsPerPage) + index + 1;”
    • 不客气。我按照您正确描述的方式更新了公式。
    猜你喜欢
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多