Saving changed values from PrimeFaces inplace

10,575

Solution 1

There the valueChangeListener is for.

E.g.

<p:inputText ... valueChangeListener="#{employeeController.firstNameChanged}" />

with

public void firstNameChanged(ValueChangeEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();
    // ...
}

You could set a boolean there or collect the values in some other property and handle it further in the real command action method.

The method will only be invoked when the value is really changed. Even if it's just from null to empty string. It will not be invoked when the old value equals() the new value.

Solution 2

General solution: store the original data object, provide the copy from modifications.

You will there be always able to say what fields have changed since last save. In the case of value change listeners, you'll know only if the value has changed since last submit, which not always is the same.

Remembering the old values is a good practise, because you can easily revert the changes made by user in UI, check if update to database is really necessary etc.

Solution 3

In mi case I can not use event="save", so I use p:event event="valueChange". In the managedBean I have properties that it will be modified and the object owner this properties. I see many people use event="save" but in my case this event throw render view error, for this reason I use valueChange

code view

<p:column>
<p:inplace id="inplaceNombre" emptyLabel="#{registro.usuario.nombre}" editor="true">
    <p:inputText id="nombre" required="true" 
              requiredMessage="#{msg['editar.nombre.required']}"
               alt="#{msg['editar.alt.nombre']}" title="#{msg['editar.title.nombre']}"
               tabindex="1" value="#{registro.nombre}" 
                styleClass="#{component.valid ? '' : 'invalid'}" maxlength="30">

                 <f:validateBean for="nombre" />
            <p:ajax event="valueChange" update="@this messageNombre" />
    </p:inputText>                        
    </p:inplace>                                        
</p:column>

          <p:row>
    <p:column>
    <p:commandButton value="#{msg['editar.value.enviar']}"
                    title="#{msg['editar.title.enviar']}" alt="#{msg['editar.alt.enviar']}"
                     actionListener="#{registro.actualizarUsuario}" tabindex="7"  />
    </p:column>
       </p:row>

managedBean code

            //property that can be modified in the User
            @Size(min=5 ,max=30 ,message="Este campo no es correcto: tamaño máximo [30]")
        @Pattern(regexp="^[a-zA-Z]+[a-zA-Z ]+",message="Este campo no es correcto")
        private String nombre;

            //User get by database with f:event type=preRenderView in xhtml view
            private Usuario usuario;

Then, I check the changes and update user.

Kind Regards.

Share:
10,575
Klaasvaak
Author by

Klaasvaak

Updated on November 21, 2022

Comments

  • Klaasvaak
    Klaasvaak over 1 year

    I Want to save the value of an inplace if it was changed.

    The methode employeeController.save() is called when I click on the save button. But how can I pass along the new and old value? I want to do this so I can know if the value was changed without asking the database.

    <h:panelGrid id="display" columns="2" cellpadding="4"  
                                 style="width:300px;"  
                                 styleClass="ui-widget-content"  
                                 columnClasses="label, value">  
    
                        <h:outputText value="ID:" />  
                        <h:outputText id="ID" value="#{emp.id}" />  
    
                        <h:outputText value="Voornaam:"/>
                        <p:inplace id="firstnam" editor="true">
                            <p:ajax event="save" onsuccess="#{employeeController.saveName()}">
                            </p:ajax>
                            <p:inputText id ="firstName" value="#{emp.firstName}"  
                                         required="true" label="text"/>
                        </p:inplace>
    
  • BalusC
    BalusC almost 12 years
    The event="valueChange" is by the way the default event for all UIInput components. You can safely omit the attribute altogether.
  • Rafael Ruiz Tabares
    Rafael Ruiz Tabares almost 12 years
    I unknown this behavior. And why I can not use event="save"? I have see code with this behavior in tag p:ajax but in my case the view doesnt render because it dont acknowledge this event.
  • BalusC
    BalusC almost 12 years
    I have no idea. Perhaps the OP tried random event names and accidently left a bogus one in his question.
  • Rafael Ruiz Tabares
    Rafael Ruiz Tabares almost 12 years
    BalusC the event="save" or "cancel" is used into a tag p:inplace.