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 ), . , , ?