2009-06-08 113 views
3

我使用外部分頁/使用自定義排序TableDecorator及以下DisplayTag表格不同的JSP: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 docs沒有解決這個問題,$ {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

回答

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; 
} 
+0

謝謝,馬庫斯。這是一個很好的方法,但有沒有辦法找到numItemsPerPage的動態?我一直在尋找一些方式來訪問傳遞到表標記的原始PaginatedList,但無濟於事(除非你硬編碼請求參數的名稱 - 但對我們來說不實用PaginatedList對象名稱變化)。再次感謝。 – 2009-06-10 14:29:14