2015-06-20 31 views
1

我正在調用rowEdit事件,並且僅當事件成功執行數據庫更新時纔想刷新表。以下是我的代碼。primefaces rowEdit僅當事件成功時才更新表的事件

XHTML

<p:dataTable id="xobjTable" var="xobj" value="#{xListView.xListResponseObject}" 
           scrollRows="20" scrollable="true" liveScroll="true" scrollHeight="600" 
           rowKey="#{xobj.X}" 
           selection="#{xListView.selectedObject}" selectionMode="single" 
           style="margin-top: 20px; margin-bottom:20px" editable="true"> 

         <f:facet name="header"> 
          Search Results 
          <p:spacer width="20"/> 
          <h:commandLink id="csv"> 
           <p:graphicImage value="csv.png" width="24"/> 
           <p:dataExporter type="csv" target="xobj" fileName="xListSearch" /> 
           <p:tooltip id="toolTipFade" for="csv" value="Click on CSV to download entire table as a csv file. " /> 
          </h:commandLink> 
         </f:facet> 
         <p:ajax event="rowEdit" listener="#{xListView.onRowEdit}" update=":xListform:xobjTable" /> 
         <p:column headerText="X" style="width:60px;text-align: center"> 
          <h:outputText value="#{xobj.X}" /> 
         </p:column> 
         <p:column headerText="Y" style="width:60px;text-align: center"> 
          <h:outputText value="#{xobj.Y}" /> 
         </p:column> 
         <p:column headerText="Modified Date" style="width:160px;text-align: center"> 
          <h:outputText value="#{xobj.modifiedDate}" /> 
         </p:column> 
         <p:column headerText="Active" style="width:160px;text-align: center"> 
          <p:cellEditor> 
           <f:facet name="output"> 
            <h:outputText value="#{xobj.active}"/> 
           </f:facet> 
           <f:facet name="input"> 
            <h:selectOneMenu value="#{xobj.active}"> 
             <f:selectItems value="#{xListView.activeList}" /> 
            </h:selectOneMenu> 
           </f:facet> 
          </p:cellEditor> 
         </p:column> 
         <p:column headerText="Location" style="width:160px;text-align: center"> 
          <p:cellEditor> 
           <f:facet name="output"> 
            <h:outputText value="#{xobj.location}"/> 
           </f:facet> 
           <f:facet name="input"> 
            <p:inputText id="location" value="#{xobj.location}" /> 
           </f:facet> 
          </p:cellEditor> 
         </p:column> 
         <p:column headerText="State" style="width:160px;text-align: center"> 
          <p:cellEditor> 
           <f:facet name="output"> 
            <h:outputText value="#{xobj.state}"/> 
           </f:facet> 
           <f:facet name="input"> 
            <p:inputText id="state" value="#{xobj.state}" /> 
           </f:facet> 
          </p:cellEditor> 
         </p:column> 
         <p:column headerText="x Country Code" style="width:160px;text-align: center"> 
          <p:cellEditor> 
           <f:facet name="output"> 
            <h:outputText value="#{xobj.xCountryCode}"/> 
           </f:facet> 
           <f:facet name="input"> 
            <p:inputText id="xCountryCode" value="#{xobj.xCountryCode}" /> 
           </f:facet> 
          </p:cellEditor> 
         </p:column> 
         <p:column headerText="xrarier" style="width:160px;text-align: center"> 
          <p:cellEditor> 
           <f:facet name="output"> 
            <h:outputText value="#{xobj.carrier}"/> 
           </f:facet> 
           <f:facet name="input"> 
            <h:selectOneMenu value="#{xobj.carrier}"> 
             <f:selectItems value="#{xListView.xarrierList}" /> 
            </h:selectOneMenu> 
           </f:facet> 
          </p:cellEditor> 
         </p:column> 
         <p:column style="width:32px" headerText="Edit" rendered="#{allTabs.isShow(xListView.title)}"> 
          <p:rowEditor /> 
         </p:column> 
         <p:column style="width:160px;text-align: center" rendered="#{allTabs.isShow(xListView.title)}" headerText="Delete"> 
          <p:commandButton id="deleteDid" actionListener="#{xListView.deletexy(xobj)}" 
              icon="ui-icon ui-icon-trash red" title="Delete" update="@form"> 
           <p:confirm header="Confirmation" message="#{xListView.finalDeleteMessage}" icon="ui-icon-alert" /> 
          </p:commandButton> 
         </p:column> 
        </p:dataTable> 

下面是事件方法

public void onRowEdit(RowEditEvent event) { 
     xListResponseObject editObject = (xListResponseObject) event.getObject(); 
     logger.debug("Coming to edit the row."); 
     String editXYStatus = xListDAO.editxy(editObject); 
     if (editxyStatus.equals(SUCCESS_RESPONSE)) { 
      FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "X/Y edit successful!"); 
      FacesContext.getCurrentInstance().addMessage(null, msg); 
     } else { 
      FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "INFO", "X/Y edit failed :" + editxyStatus); 
      FacesContext.getCurrentInstance().addMessage(null, msg); 
     } 

    } 

我想發生這樣的更新,只有當rowEdit事件更新數據庫。 我可以在我的方法中有一個布爾值集並訪問p:ajax中更新的數據嗎?

+0

在你的bean中使用'RequestContext',並且只有在你的dbaction成功時才從那裏更新組件 – Kukeltje

回答

1

添加到結束你SUCCESS_RESPONSE

RequestContext.getCurrentInstance().update("form:npaobjTable"); 

或任何你的表真正的ID是。

+0

我試過了上面的內容,並從p:ajax中刪除了更新,並將其添加到rowEdit事件中,但仍然更新了表無效的記錄。以下是我的更改。 hopeIsTheonlyWeapon

+0

if(editNpaNxxStatus.equals(SUCCESS_RESPONSE)){FailedMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,「INFO」,「NPA/NXX編輯成功!「); FacesContext.getCurrentInstance()。addMessage(null,msg); RequestContext.getCurrentInstance()。update(「:npaListform:npaobjTable」); } – hopeIsTheonlyWeapon

+0

您沒有處理新值

-2

我遇到了同樣的問題。 URL下方將解決您的問題。 。

http://forum.primefaces.org/viewtopic.php?f=3&t=38412&p=153402#p153402

實際響應:

public void onRowEdit(RowEditEvent event) { 
     NpaListResponseObject editObject = (NpaListResponseObject) event.getObject(); 
     logger.debug("Coming to edit the row."); 
     String editNpaNxxStatus = npaListDAO.editNpaNxx(editObject); 
     if (editNpaNxxStatus.equals(SUCCESS_RESPONSE)) { 
      FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "NPA/NXX edit successful!"); 
      FacesContext.getCurrentInstance().addMessage(null, msg); 
     } else { 
      FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "INFO", "NPA/NXX edit failed :" + editNpaNxxStatus); 
      FacesContext.getCurrentInstance().addMessage(null, msg); 
      FacesContext.getCurrentInstance().validationFailed(); 
     } 

    } 

加入FacesContext.getCurrentInstance()validationFailed();最後在失敗的情況下。

+0

請在這裏添加一個實際的解釋和迴應,而不只是一個鏈接到另一個迴應 – Pom12

+0

根據您的建議編輯的代碼。添加了FacesContext.getCurrentInstance()。validationFailed();在最後一行。 – umeshfadadu

+0

但是,您應該提供一個關於如何解決問題的真實**解釋**,例如解釋代碼的作用,在哪裏添加它等等。答案應該是描述性的,而不是一些複製粘貼! – Pom12

相關問題