Primefaces DataTable,延迟加载和每行CommandButton

Mic*_*tti 6 lazy-loading datatables primefaces jsf-2

我有这个简单的页面:

<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>
Run Code Online (Sandbox Code Playgroud)

并且CommandButton内部DataTable不起作用,只刷新页面.但外面的人正在工作.

如果我改变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>
Run Code Online (Sandbox Code Playgroud)

CommanButton里面DataTable就像一个魅力.

谁知道为什么?

这是一个错误吗?

我上线了

  • Glassfish 3.1.2
  • JSF 2.1.11(Mojarra)
  • PrimeFaces 3.4-SNAPSHOT

Mic*_*tti 7

发现延迟数据模型必须是回发请求中的相同实例,即使具有相同值的新实例也不起作用.所以它必须至少从一个@ViewScopedbean提供.


Ser*_*gio 5

自从这个问题发布以来已经过去了四年,但问题仍然存在于 PrimeFaces 6.0 中。

我将向那些不想(或不能)使用 ViewScoped beans 的人发布一个解决方法。

前提是:“您不能将任何“ajaxified”项目放入绑定到 RequestScoped 内容的惰性数据表中”。绝不。请记住,任何引发 ajax 调用的方法都不会起作用。

所以第一步就是在数据表之外进行ajax调用。我们将使用 RemoteCommand 来完成此操作。您可以将此 RemoteCommand 放置在 DataTable 之外的任何位置(当然,在表单内)

<p:remoteCommand name="remoteCall" action="#{bean.doStuff()}">
</p:remoteCommand>
Run Code Online (Sandbox Code Playgroud)

现在,我们所要做的就是从 DataTable 内部调用此 RemoteCommand。我使用链接来执行 javascript 调用,但您可以使用按钮或任何您喜欢的内容:

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

此链接提供了一种调用 javascript 函数“remoteCall”的方法,该函数将对“bean.doStuff()”执行 ajax 调用。

请注意,onClick 事件不仅包含对“remoteCall”的 javascript 调用,还包含一个只有一个参数的参数数组,名为“id”,值为“#{item.id}”。这将允许 RemoteCommand 将名为“id”的参数发送到支持 bean。

在“doStuff”方法中,您必须检索“id”参数值:

public void doStuff () {

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

}
Run Code Online (Sandbox Code Playgroud)