2012-10-06 16 views
0

我一直在我的應用程序中使用JSF 1.2。我的大部分應用程序頁面都由h:datatable組成。我碰到this美妙的文章,它解釋了關於數據表的一切。如上面的文章所示,我通過將我的表綁定到HtmlDataTable並使用會話作用域bean來實現數據表分頁。使用viewscoped bean時的數據表分頁

現在我正在轉向JSF 2.0版本。我想將我的sessionscoped beans轉換爲viewscoped,因爲我的大多數應用程序頁面都是相互獨立的。

我遇到了this這篇文章解釋了關於Viewscoped豆。它告訴我們不能使用數據表的binding屬性。還使用Datamodel

我現在已經對如何使用Datamodelviewscoped 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}" /> 

請幫助。

回答

1

由被綁定爲

<h:dataTable ... first="#{bean.first}" rows="#{bean.rows}"> 
0

我認爲,你應該使用primefaces datable他們有一個非常好的箱子數據表,包括lazy loading或許你可以使用richfaces或其他任何組件套件。 當然,你可以建立你的jsf分頁,但我認爲你會花費大量的時間來構建一些完成的工作。

+0

感謝您的答覆卡斯特羅private int firstprivate int rows屬性替換dataTable.setFirst(...)dataTable.getRows()。但我使用Apache trinidad組件。我喜歡將解決方案擴展到特立尼達表 – Sreeram

+0

@Sreeram如果trinidad組件只會給你很多工作和時間浪費,只需使用具有很多內置功能(如分頁,排序和過濾)的PrimeFaces組件(對於數據表)。如果你仍然想重新發明輪子,玩得開心,咖啡很多。 –

相關問題