2012-08-12 49 views
6

命令按鈕,我有這個簡單的頁面:Primefaces數據表,延遲加載和每

<h:form id="form"> 

    <p:dataTable value="#{testBean.unitTypeModel}" var="elem" lazy="true" rows="10"> 
     <p:column headerText="class">#{elem.class.simpleName}</p:column> 
     <p:column headerText="code">#{elem.code}</p:column> 
     <p:column headerText="description">#{elem.description}</p:column> 
     <p:column headerText="action"> 
      <p:commandButton action="test2" icon="ui-icon ui-icon-wrench" value="edit"> 
       <f:setPropertyActionListener target="#{testBean.selection}" value="#{elem}"/> 
      </p:commandButton> 
     </p:column> 
    </p:dataTable> 

    <p:commandButton action="test2" icon="ui-icon ui-icon-wrench"/> 

</h:form> 

和內部DataTableCommandButton不工作,只是刷新頁面。 但外面正在工作。

如果我改變valuelazy這樣:

<h:form id="form"> 

    <p:dataTable value="#{testBean.unitTypeModel.load(0, 10, null, null, null)}" var="elem" lazy="false" rows="10"> 
     <p:column headerText="class">#{elem.class.simpleName}</p:column> 
     <p:column headerText="code">#{elem.code}</p:column> 
     <p:column headerText="description">#{elem.description}</p:column> 
     <p:column headerText="action"> 
      <p:commandButton action="test2" icon="ui-icon ui-icon-wrench" value="edit"> 
       <f:setPropertyActionListener target="#{testBean.selection}" value="#{elem}"/> 
      </p:commandButton> 
     </p:column> 
    </p:dataTable> 

    <p:commandButton action="test2" icon="ui-icon ui-icon-wrench"/> 

</h:form> 

CommanButtonDataTable的作品就像一個魅力。

有人知道爲什麼嗎?

它是一個錯誤?

我是在

  • Glassfish的3.1.2
  • JSF 2.1.11(鑽嘴魚科)
  • PrimeFaces 3.4快照

回答

7

發現,懶惰的數據模型必須是回發請求上的相同實例,即使具有相同值的新實例也不起作用。所以它必須至少由一個@ViewScoped bean提供。

+1

這是不完全正確會話通過使用'@ ViewScoped',但您也可以使用'@ RequestScoped'。重點在於,當在'APPLY_REQUEST_VALUES'中計算'isRowAvailable()'方法並且'pageSize'字段必須保存大於零的值時,它必須返回true。我通過在重載兩個方法的同時擴展'LazyDataModel'來實現這一點:'isRowAvailable()',在這裏我調用'load(...)'並將結果應用於'setWrappedData()',第二個方法'setRowIndex(int rowIndex) '我把'pageSize'設置爲我的默認值 – uvo 2018-01-16 10:14:28

1

自發布此問題以來已經過去了四年,但問題仍然存在於PrimeFaces 6.0中。

我打算向那些不想(或不能)使用ViewScoped bean的人發佈解決方法。

前提是:「你不能在綁定到RequestScoped的懶數據表中放置任何」ajaxified「項目」。決不。請記住,任何引發ajax調用的東西都將無法工作。

所以第一步是在數據表之外進行ajax調用。我們將使用RemoteConmand進行此操作。您可以將數據表以外的任何地方把這個RemoteCommand(一個形式裏面,當然)

<p:remoteCommand name="remoteCall" action="#{bean.doStuff()}"> 
</p:remoteCommand> 

現在,我們需要做的是從數據表中調用這個RemoteCommand。我使用的鏈接來執行的JavaScript調用,但你可以使用一個按鈕或任何你喜歡:

<p:column> 
    <p:link value="#{item.id}" href="#" onclick="remoteCall([{name:'id', value:#{item.id}}])"> 
    </p:link> 
</p:column> 

此鏈接提供了一種方法來調用JavaScript函數「爲RemoteCall」將執行的Ajax調用「bean.doStuff()」。

請注意,onClick事件不僅包含對「remoteCall」的JavaScript調用,而且還包含只有一個參數的參數數組,名爲「id」,值爲「#{item.id}」。這將允許RemoteCommand將一個名爲「id」的參數發送給支持bean。

裏面的「doStuff」方法,你必須檢索「ID」參數值:如果LazyDataModel`的'同一個實例中發現它是有用的:

public void doStuff() { 

    String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"); 

}