【问题标题】:Primefaces commandLink only updates datable after clicking it twicePrimefaces commandLink 仅在单击两次后才更新数据表
【发布时间】:2013-11-29 09:52:23
【问题描述】:

以下代码是关于一个表的,我可以在其中添加(commandButton)或删除(commandLink)行。两个按钮都在工作并调用相应的 bean 方法。但是,虽然每次单击添加按钮都会更新表格并立即添加一行,但对于删除按钮,我必须单击它两次才能删除该行。即使第一次没有删除该行,也会调用 bean 方法。我该怎么办?谢谢!

    <h:form id="form">
        <table>
            <tr>
                <td>
                    <h:panelGrid columns="2" width="100%">
                        <p:dataTable id="univertitiesTable" value="#{universityBean.universityList}" var="university"
                            editable="true" editMode="cell" style="align:center;" >

                            <p:column headerText="Name" style="width:80px" >
                                <p:inputText value="#{university.name}" style="width:25px;" id="nameField"  label="name"  />
                            </p:column>

                            <p:column headerText="" style="width:20px; ">
                                <p:commandLink actionListener="#{universityBean.deleteUniversity}" update="univertitiesTable" id="removeButton" ajax="true">
                                    <h:graphicImage value="/resources/icones/delete.gif" />
                                    <f:setPropertyActionListener value="#{university}"
                                        target="#{universityBean.university}" />
                                </p:commandLink> 
                            </p:column>
                        </p:dataTable>

                        <p:commandButton value="+" update="univertitiesTable" id="addButton" ajax="true"
                            actionListener="#{universityBean.addUniversity}"
                            styleClass="ui-priority-primary"  />

                    </h:panelGrid>                  
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <h:commandButton id="save" value="Save"
                        action="#{universityBean.save}" binding="#{save}" />
                </td>
            </tr>
        </table>    
    </h:form>

【问题讨论】:

  • 尝试从commandButton中删除ajax=true并将process=@this添加到commandButton

标签: jsf-2 primefaces datatable commandlink


【解决方案1】:

您必须使用action 而不是actionListener。更新是在操作完成后完成的,因此当您不指定任何操作时,更新会立即完成。因此,当您进行另一次更新时,视图会识别出该行已被删除;当您再次单击该链接时完成。
顺便说一句,ajax="true" 无论如何都是ajax 属性的默认值。
即:

<p:commandLink action="#{universityBean.deleteUniversity}" update="univertitiesTable" id="removeButton">
  <h:graphicImage value="/resources/icones/delete.gif" />
  <f:setPropertyActionListener value="#{university}"
    target="#{universityBean.university}" />
</p:commandLink> 

【讨论】:

  • 非常感谢。我本可以想到这一点,但是添加按钮与动作侦听器一起正常工作,我不明白为什么它与删除按钮的行为不同。
  • 我有同样的问题,但我不知道为什么这个解决方案对我的情况没有帮助(jsf 2.2)。
  • 可能还有其他一些原因,因此请检查有关该问题的其他 SO 问题或发布另一个问题并提供一些详细信息。
猜你喜欢
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 2019-09-08
  • 2020-02-14
  • 2022-12-16
  • 2014-04-01
相关资源
最近更新 更多