2012-08-12 115 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"); 

}