java displaytag 外部分页/排序并获得真正的行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/965198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:36:36  来源:igfitidea点击:

displaytag external paging/sorting and getting true row number

javapaginationpagingjsp-tagsdisplaytag

提问by Michael C. Rosenstein

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

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

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").

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

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

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

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

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

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():

是的,可以通过向分页 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();
}

Thanks.

谢谢。

/mcr

/mcr

采纳答案by Marcus Leon

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 TableDecoratorclass should work:

像这样的代码你的TableDecorator班级应该可以工作:

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

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