JSF- Primefaces 5.0 roweditor does not recognize the new value

1.3k Views Asked by At

I'm trying to use the roweditor of PrimeFaces datatable and I have the problem that changing the value in the datatable the managed bean does not recognize the new value and the old remains.

I almost "cloned" the showcase of RowEditing and surprisingly not work me. I searched more posts and some say it may be a bug, but I replicated exactly the showcase example and it works. My version is PrimeFaces 5.0.

*.xhtml

<h:form id="frmExclos">
   <p:growl id="mensajeGeneral3" sticky="false" showDetail="true"/>
   <p:panel id="pnlCriteriExclusio" style="width: 425px" header="Criteris d'exclusió del pacient" widgetVar="pnlCriterisE">
   <p:dataTable id="tblCriterisExclusioNia" var="itemCriterisExclusio" value="#{mbRCriteriExclusio.getCriterisExclusioNia(mbVMalignitatNia.personaAmbMalignitatNia.id)}" editable="true">
     <p:ajax event="rowEdit" listener="#{mbRCriteriExclusio.onRowEdit}" update=":frmExclos:mensajeGeneral3" />
     <p:ajax event="rowEditCancel" listener="#{mbRCriteriExclusio.onRowCancel}" update=":frmExclos:mensajeGeneral3"  />
             <p:column headerText="Observacions">
               <p:cellEditor>
                  <f:facet name="output"><h:outputText value="#{itemCriterisExclusio.comentaris}"></h:outputText></f:facet>
                  <f:facet name="input"><p:inputText value="#{itemCriterisExclusio.comentaris}" label="Observacions"></p:inputText></f:facet>
               </p:cellEditor>
             </p:column>
             <p:column style="width:32px">
               <p:rowEditor />
             </p:column>
             <f:facet name="footer" ><p:commandButton update="@this" icon="ui-icon-plusthick" value="Afegir criteri" oncomplete="PF('dlgAddCriterisExclusio').show()"/>
              </f:facet>
       </p:dataTable>
       </p:panel>
</h:form> 

Then, the fragment of the managed bean MbRCriteriExclusio.java :

@Named(value = "mbRCriteriExclusio")
@ViewScoped
public class MbRCriteriExclusio {
public void onRowCancel(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Edit Cancelled", "" + ((MalignitatPersonaCriterisExclusioNia) event.getObject()).getComentaris());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

In the Msg getComentaris returns the initial value loaded in the datatable not the new value that I've edited! The code of the showcase is the same ..

thank you very much for your help

3

There are 3 best solutions below

0
On

In RowEditEvent same thing happens.

I did not write the function so that not too long the message

The msg shows me the initial value and not the updated in the RowEditEvent.

0
On

In case of rowCancel,it wont reflect the new value since you are cancelling out the changes. in this function public void onRowEdit(RowEditEvent event) you will be able to get modified value. Try this.

0
On

Assuming the intended method is similar to:

public void onRowEdit(RowEditEvent event) {
    Object o = event.getObject();
    persist(o);
}

Credit for the fix goes to: http://jwsn.blogspot.com/2013/05/issue-with-rowedit-event-in-primefaces.html

If your datatable data model is backed by a getter with a query to the database, you may have to put a null check around the database call so that it only refreshes values when null. This was overwriting my "new" value at event.getObject() to the "old" value.

Guessing your getter is like this:

listOfX List<X>;

public List<X> getCriterisExclusioNia(idType id) {
    listOfX = getFacade().getListofX(id);
    return listOfX;
}

Make it like this:

public List<X> getCriterisExclusioNia(idType id) {
    if(listOfX == null) {
        listOfX = getFacade().getListofX(id);
    }
    return listOfX;
}