我一直在我的應用程序中使用JSF 1.2
。我的大部分應用程序頁面都由h:datatable
組成。我碰到this美妙的文章,它解釋了關於數據表的一切。如上面的文章所示,我通過將我的表綁定到HtmlDataTable
並使用會話作用域bean來實現數據表分頁。使用viewscoped bean時的數據表分頁
現在我正在轉向JSF 2.0
版本。我想將我的sessionscoped
beans轉換爲viewscoped
,因爲我的大多數應用程序頁面都是相互獨立的。
我遇到了this這篇文章解釋了關於Viewscoped
豆。它告訴我們不能使用數據表的binding
屬性。還使用Datamodel
。
我現在已經對如何使用Datamodel
和viewscoped
bean實現數據表分頁進行了嘗試。
我有做分頁
public String pageFirst() {
dataTable.setFirst(0);
return "";
}
public String pagePrevious() {
dataTable.setFirst(dataTable.getFirst() - dataTable.getRows());
return "";
}
public String pageNext() {
dataTable.setFirst(dataTable.getFirst() + dataTable.getRows());
return "";
}
public String pageLast() {
try {
int count = dataTable.getRowCount();
int rows = dataTable.getRows();
LOGGER.info("rowcount:" + count);
LOGGER.info("rows:" + rows);
dataTable.setFirst(count - ((count % rows != 0) ? count % rows : rows));
}catch(ArithmeticException e){
LOGGER.info("no rows to display: ",e);
}
return "";
}
並在視圖下面的方法我用他們喜歡這個
<h:commandButton value="#{msgs.footerbutton1}"
action="#{bean.pageFirst}"
disabled="#{bean.dataTable.first == 0}" />
<h:commandButton value="#{msgs.footerbutton2}"
action="#{bean.pagePrevious}"
disabled="#{bean.dataTable.first == 0}" />
<h:commandButton value="#{msgs.footerbutton3}"
action="#{bean.pageNext}"
disabled="#{bean.dataTable.first + bean.dataTable.rows
>= bean.dataTable.rowCount}" />
<h:commandButton value="#{msgs.footerbutton4}"
action="#{bean.pageLast}"
disabled="#{bean.dataTable.first + bean.dataTable.rows
>= bean.dataTable.rowCount}" />
請幫助。
感謝您的答覆卡斯特羅
private int first
和private int rows
屬性替換dataTable.setFirst(...)
和dataTable.getRows()
。但我使用Apache trinidad組件。我喜歡將解決方案擴展到特立尼達表 – Sreeram@Sreeram如果trinidad組件只會給你很多工作和時間浪費,只需使用具有很多內置功能(如分頁,排序和過濾)的PrimeFaces組件(對於數據表)。如果你仍然想重新發明輪子,玩得開心,咖啡很多。 –