Reset Bean when using Ajax events in PrimeFaces

I have a dialog that contains a checkbox and one input field. When this check box is selected, the input must be disabled. The flag value is stored in the Bean, and the input value is one of the selected object values.

This dialog box appears when the user clicks on the link in the list. The dialog entries are filled with values ​​from the model.

I also have two buttons: save and cancel. When saving, depending on the value of the flag, I do different things with the model and update it. When canceling, I do not want to do anything.

The problem is this: when there are ajax events associated with the checkbox, the value in the Bean is automatically updated. Therefore, after clicking cancel and reopening the dialog, I got the last state, but I need the initial state.

Here is the code:

List

<h:form id="termPanelForm">

    <p:dataTable id="termPanelTable"
        value="#{termRightPanelController.terms}" var="term" emptyMessage="">

        <p:column>
            <p:commandLink value="#{term.subject}"
                action="#{termRightPanelController.setTerm(term)}"
                oncomplete="termRealizeDlg.show();"
                update=":termRealizeForm:termRealizeDialog" />
        </p:column>

        <p:column width="50">
            <h:outputText value="#{term.dateRealization}">
                <f:convertDateTime pattern="dd-MM-yy HH:mm" />
            </h:outputText>
        </p:column>
    </p:dataTable>

</h:form>

Dialog

<h:form id="termRealizeForm">
    <p:dialog id="termRealizeDialog" widgetVar="termRealizeDlg"
        modal="true" resizable="false" closeOnEscape="true"
        header="#{termRightPanelController.selectedTerm.subject}">

        <p:messages autoUpdate="true" showDetail="true"></p:messages>

        <p:panelGrid columns="2" styleClass="border-none">

            <p:outputLabel for="realized" value="#{i18n['Term.MarkRealized']}" />
            <p:selectBooleanCheckbox id="realized"
                value="#{termRightPanelController.termRealized}">
                <p:ajax process="@this" update="dateRealization" />
            </p:selectBooleanCheckbox>

            <p:outputLabel for="dateRealization"
                value="#{i18n['Term.ChangeDateRealization']}" />
            <p:inputText id="dateRealization"
                value="#{termRightPanelController.selectedTerm.dateRealization}"
                pattern="dd-MM-yyyy HH:mm"
                disabled="#{termRightPanelController.termRealized}" >
            </p:inputText>

        </p:panelGrid>

        <h:panelGroup layout="block" styleClass="buttons-group">
            <p:commandButton value="#{i18n['Common.Save']}"
                oncomplete="hideDialogWhenValid(termRealizeDlg, xhr, status, args);"
                action="#{termRightPanelController.editTerm()}"
                update=":termPanelForm:termPanelTable" icon="ui-icon-check"></p:commandButton>
            <p:commandButton onclick="termRealizeDlg.hide();"
                value="#{i18n['Common.cancel']}" 
                icon="ui-icon-cancel" update=":termRealizeForm:termRealizeDialog">
            </p:commandButton>
        </h:panelGroup>

    </p:dialog>
</h:form>

Bean

@SuppressWarnings("rawtypes")
@ManagedBean
@ViewScoped
public class TermRightPanelController extends MainController implements Serializable {

    private static final long serialVersionUID = 6283828584670862366L;

    private TermServiceLocal termService = ServiceLocator.locateService(
            TermService.class, TermServiceLocal.class);

    private List<Term> terms;

    private Term selectedTerm;

    private boolean termRealized;

    @PostConstruct
    public void init() {
        loadTerms();
    }

    public void loadTerms() {
        terms = termService.getTermsNotRealized(getLoggedUser().getId());
    }

    public String expiredTerm(Term term) {
        long time = new Date().getTime();
        if (term.getDateRealization().getTime() > time) {
            return "";
        }

        return "expired";
    }

    public void editTerm() {
        if (termRealized) {
            selectedTerm.setDateRead(new Date());
        } else {
            selectedTerm.setDateRead(null);
        }
        termService.merge(selectedTerm);
        loadTerms();
    }

    public void setTerm(Term term) {
        this.selectedTerm = term;
    }

    public List<Term> getTerms() {
        return terms;
    }

    public void setTerms(List<Term> terms) {
        this.terms = terms;
    }

    public Term getSelectedTerm() {
        return selectedTerm;
    }

    public void setSelectedTerm(Term selectedTerm) {
        this.selectedTerm = selectedTerm;
    }

    public boolean isTermRealized() {
        return termRealized;
    }

    public void setTermRealized(boolean termRealized) {
        this.termRealized = termRealized;
    }

    @Override
    public Class getModelClass() {
        return this.getClass();
    }
}

In this particular scenario, I can always set termRealizedto false when I click cancel, but in other scenarios I have values ​​loaded from the model. Therefore, after reopening the dialog box, I want to have the same values ​​as in the model, and not be stored in the Bean.

We discussed some possible solutions in the team, but for us everything seems unpleasant:

  • selectedTerm . , , .
  • JavaScript . JS, .
  • selectedTerm , terms. - ?
  • terms. , 3- .

reset ( resetInput ), . , , ?

+4
1

.

:

public class Term {
  private String dummy;

  public Term() {

  }
  //copy constructor
  public Term(Term another) {
     this.dummy = another.dummy; // and set the rest of the values  
   }
}

dataTable select.

<p:commandLink value="#{term.subject}"
            action="#{termRightPanelController.selectTerm(term)}"
            oncomplete="termRealizeDlg.show();"
            update=":termRealizeForm:termRealizeDialog" />

selectTerm (Term term)

public void selectTerm(Term term) { 
  selectedTerm = new Term(term);
}

onHide

 <p:dialog id="termRealizeDialog" widgetVar="termRealizeDlg"
    modal="true" resizable="false" closeOnEscape="true"
    header="#{termRightPanelController.selectedTerm.subject}" 
    onHide="restDialog()">

restDialog() remoteCommand

<p:remoteCommand name="restDialog"
actionListener="#{termRightPanelController.restDialogValues()}" />

restDialogValues ​​()

public void restDialogValues() {
  setTermRealized(false);
}

<p:commandButton value="#{i18n['Common.Save']}"
   action="#{termRightPanelController.editTerm()}" />

editTerm()

public void editTerm() {
   ....
   terms.remove(selectedTerm);//assuming equals and hashcode works on ID
   terms.add(selectedTerm);
   // Or loop over the terms and find your selectedTerm by id
   // if the equals and hashcode are not implemented on the ID, 
   //then remove it and add the new one
   ....
}

, .

0

All Articles