【问题标题】:p:commandButton not working within a p:dataTable while using filterp:commandButton 在使用过滤器时不能在 p:dataTable 中工作
【发布时间】:2015-07-03 03:13:47
【问题描述】:

我正在使用 JSF、Primefaces 5.2 和 GlassFish 4.1。

我已经建立了一个简单的项目来测试和复制我的较大问题,并发现它在测试项目中是可重复的。

我有一个简单的页面,其中包含一个简单的对象列表。我正在使用<p:dataTable> 并使用全局搜索功能和分页。

表格代码

<ui:define name="content">
    <h:form id="carsForm" >
        <p:dataTable id="singleDT" var="c" value="#{testFlow.cars}" widgetVar="carsTable" rows="10" paginator="true"
                     paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                     rowsPerPageTemplate="5,10,15" emptyMessage="No cars found with the given criteria" reflow="true"
                     filteredValue="#{testFlow.filteredCars}">

            <f:facet name="header">
                <p:outputPanel class="fright">
                    <h:outputText value="Search all fields: " />
                    <p:inputText id="globalFilter" onkeyup="PF('carsTable').filter()" style="width:200px" placeholder="Enter keyword" />
                </p:outputPanel>
                <p:outputPanel class="Fleft">
                    <p:commandButton value="New Car" action="addCar" />
                </p:outputPanel>
                <div class="EmptyBox5"></div>
            </f:facet>

            <p:column filterBy="#{c.id}" headerText="ID" sortBy="#{c.id}" >
                <h:outputText value="#{c.id}"/>
            </p:column>

            <p:column filterBy="#{c.m}" headerText="Model Year" sortBy="#{c.modelYear}" >
                <h:outputText value="#{c.modelYear}"/>
            </p:column>

            <p:column filterBy="#{c.make}" headerText="Make" sortBy="#{c.make}" >
                <h:outputText value="#{c.make}"/>
            </p:column>

            <p:column filterBy="#{c.model}" headerText="Model" sortBy="#{c.model}" >
                <h:outputText value="#{c.model}"/>
            </p:column>

            <p:column filterBy="#{c.color}" headerText="Color" sortBy="#{c.color}" >
                <h:outputText value="#{c.color}"/>
            </p:column>

            <p:column style="width: 200px; text-align: center">
                <p:commandButton actionListener="#{testFlow.removeCar(c)}" value="Delete" update="singleDT messages" ajax="true" >
                    <p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
                </p:commandButton>
            </p:column>
        </p:dataTable>

        <div class="EmptyBox5"></div>
        <p:panel>
            <p:commandButton action="returnFromTestFlow" value="Exit the Flow" />
        </p:panel>
    </h:form>

    <p:messages id="messages" autoUpdate="true" closable="true" />

    <p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
    </p:confirmDialog>
</ui:define>

我的控制器有一个删除对象的简单方法。

方法

public String removeCar(Inventory c) {
    String redirectTo = "/testFlow/testFlow";

    try {
        this.cars.remove(c);
        iFacade.remove(c);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return redirectTo;
}

当我删除所有过滤和排序时,单击是后调用该方法并删除该行。然而,虽然过滤和排序已经到位,但整个表只是重新显示为空,但从未调用任何方法。不会出现错误,并且在使用调试器时,它永远不会在相关方法中遇到断点。

任何帮助或指导将不胜感激。

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    我认为问题在于首先使用您提供的列表呈现数据表,但是当您按下按钮时,它会搜索过滤后的列表,并且由于起初它是空的,因此无法找到该按钮。另一方面,请注意,如果您输入搜索条件并再次将其删除,按钮将起作用。这是因为过滤后的列表已被填充,现在可以找到该按钮。要解决此问题,您需要使用预渲染的所有值填充过滤列表。在你的情况下:

    在您的支持 bean 中:

    public void init()
    {
        if(!FacesContext.getCurrentInstance().isPostback())
        {
            filteredCars = cars;
        }
    }
    

    在您的 xhtml 文件中:

    <f:event type="preRenderView" listener="#{testFlow.init}"/>;
    

    另一种方法是在 init() 函数上使用 @PostConstruct 注释而不是 &lt;f:event type="preRenderView /&gt;,这是更清晰的代码,但在我看来,在调用函数时有点模糊。

    【讨论】:

    • 谢谢!我在这个问题上被困了 3 个小时。
    【解决方案2】:

    遇到了同样的问题,@djst 解决方案对我不起作用,但我最终通过使用 Omnifaces 表单 (o:form) 而不是标准的h:form 解决了这个问题。虽然不知道为什么,但可能值得一试。

    【讨论】:

      【解决方案3】:

      当您使用过滤器时,primefaces 会创建一个列表并将其显示在结果中,当您从主列表中删除一个项目时,它与显示列表无关,这可能会导致类似发生的问题给你。

      【讨论】:

      • 但是应该怎么做才能解决它呢?现在更多的是评论而不是答案
      • 我应该补充一点,这发生在未过滤列表和过滤列表上。此外,当使用 action= 或 actionListener= 时,结果是相同的。
      猜你喜欢
      • 2011-05-31
      • 2012-12-29
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多