2013-11-15 23 views
2

以下代碼是關於一個表格,我可以在其中添加(commandButton)或刪除(commandLink)行。這兩個按鈕都在工作並調用通訊錄bean方法。但是,對於添加按鈕中的每一次點擊都會立即更新表格添加一行,對於刪除按鈕,我必須點擊兩次才能刪除該行。即使第一次沒有刪除該行,也會調用bean方法。我該怎麼辦?謝謝!Primefaces commandLink僅在點擊兩次後才更新datable

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

嘗試從取出的commandButton'阿賈克斯=真'和'添加過程= @ this'到'commandButton' – SRy

回答

5

您必須使用action而不是actionListener。更新在操作完成後完成,因此,當您不指定任何操作時,立即完成更新。因此,該視圖會識別出您在執行另一次更新時該行被刪除;當您再次點擊鏈接時完成。
Btw ajax="true"反正是ajax-attribute的默認值。
即:

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

這是正確的感謝。我可能已經知道了這一點,但添加按鈕與動作偵聽器正常工作,我不明白爲什麼它沒有與刪除按鈕相同的行爲。 – qxlab

+0

我有同樣的確切問題,但我不知道爲什麼這個解決方案對我的情況沒有幫助(jsf 2.2)。 –

+0

可能還有其他一些原因,所以請檢查有關該問題的其他SO問題或發佈另一個問題並提供一些詳細信息。 – user1983983

相關問題