displaytag外部分页/排序并获取真实行号

发布于 2024-07-22 18:25:51 字数 1679 浏览 3 评论 0原文

我在 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 页顶部返回“0”,而不是“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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2024-07-29 18:25:51

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

编写类似这样的代码,您的 TableDecorator 类应该可以工作:

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

   return ((page - 1) * numItemsPerPage) + index + 1;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文