Primefaces DataTable,延迟加载和每行CommandButton

时间:2012-08-12 00:34:33

标签: jsf-2 primefaces lazy-loading datatables

我有这个简单的页面:

<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>

并且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>

CommanButton内的DataTable就像魅力一样。

有人知道为什么吗?

这是一个错误吗?

我在

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

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:1)

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

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

前提是:“你不能将任何”ajaxified“项放在绑定到RequestScoped内容的惰性数据表中”。决不。请记住,抛出ajax调用的任何内容都无法正常工作。

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

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

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

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

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

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

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

public void doStuff () {

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

}