Sometimes we need to cancel a form or reset all the editable form fields. The following post provides few methods for doing so.
1. Rollback - the traditional way. It rollbacks to the previous state for the changes that are not committed yet.
It closes all open Row Iterators and executes the View Object.
Either use it on a buttons Action Listener or directly drag the Rollback operation from the Data Control.
Only problem is it rollbacks all the uncommitted transactions instead of only resetting the form field.
It invokes ROLLBACK from database and executes View Object to return latest data from DB for all rows.
This works well when we want to undo multiple changes at once.
2. <af:resetButton> - Resets the content of a form for all editable components, updating all editable components with the current values of the model on the server.
3. Using ResetUtils - It resets all editable value holders present inside UIComponent 'myForm' .The UIComponent is searched for within the component tree and the first one encountered is reset back to the original value.
4. Using af:resetActionListener. Drag the af:resetActionListener component onto the cancel button and set immediate=true on the cancel button so client validation is bypassed .
5. Queue an action on an existing button containing af:resetActionListener attached to it on the page.The button's binding is present in the bean.
6. Create instance of resetActionListener in bean and call processAction ()
7.Another way is Cancel by undoing changes - set "immediate=true" on the cancel button.
Open the ViewImpl class and add the following code which first determine whether or not the row is new or an existing row and accordingly either removes/refresh it.
Provide the ActionListener ('onCancel') for Cancel Button on the page -
8. Custom bean method to reset the UIcomponent (form) :
1. Rollback - the traditional way. It rollbacks to the previous state for the changes that are not committed yet.
It closes all open Row Iterators and executes the View Object.
Either use it on a buttons Action Listener or directly drag the Rollback operation from the Data Control.
Only problem is it rollbacks all the uncommitted transactions instead of only resetting the form field.
It invokes ROLLBACK from database and executes View Object to return latest data from DB for all rows.
This works well when we want to undo multiple changes at once.
2. <af:resetButton> - Resets the content of a form for all editable components, updating all editable components with the current values of the model on the server.
<af:resetButton text="resetButton" id="rb1"/>
3. Using ResetUtils - It resets all editable value holders present inside UIComponent 'myForm' .The UIComponent is searched for within the component tree and the first one encountered is reset back to the original value.
public void resetActionListener(ActionEvent actionEvent){
UIComponent myForm = actionEvent.getComponent();
oracle.adf.view.rich.util.ResetUtils.reset(myForm);
}
4. Using af:resetActionListener. Drag the af:resetActionListener component onto the cancel button and set immediate=true on the cancel button so client validation is bypassed .
<af:commandButton text="Cancel" id="cb1" action="#{bindings.Rollback.execute}" inlineStyle="font-weight:bolder;" immediate="true" >
<af:resetActionListener/>
</af:commandButton>
5. Queue an action on an existing button containing af:resetActionListener attached to it on the page.The button's binding is present in the bean.
public void resetActionUsingQueueListener(ActionEvent aEvent){
//Queue the binded button's action to this button's action event.
ActionEvent actionEvent = new ActionEvent(this.getResetButton());
actionEvent.queue();
}
6. Create instance of resetActionListener in bean and call processAction ()
public void resetAct(ActionEvent actionEvent) {
ResetActionListener ral = new ResetActionListener();
ral.processAction(actionEvent);
}
7.Another way is Cancel by undoing changes - set "immediate=true" on the cancel button.
Open the ViewImpl class and add the following code which first determine whether or not the row is new or an existing row and accordingly either removes/refresh it.
public String getRowStatus(Row row){
DepartmentsViewRowImpl rwImpl = (DepartmentsViewRowImpl)row;
String rwStatus = translateStatusToString(rwImpl.getEntity(0).getEntityState());
return rwStatus;
}
private String translateStatusToString(byte b) {
String ret = null; switch (b) {
case Entity.STATUS_INITIALIZED: {
ret = "Initialized"; break;
}
case Entity.STATUS_MODIFIED: {
ret = "Modified"; break;
}
case Entity.STATUS_UNMODIFIED: {
ret = "Unmodified"; break;
}
case Entity.STATUS_NEW: {
ret = "New"; break;
} }
return ret;
}
Provide the ActionListener ('onCancel') for Cancel Button on the page -
public void onCancel(ActionEvent actionEvent) {
FacesContext fctx = FacesContext.getCurrentInstance();
DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding iter = bindings.findIteratorBinding("DepartmentsView1Iterator");
Row rw = iter.getCurrentRow();
OperationBinding getRowStatusBinding = bindings.getOperationBinding("getRowStatus");
String rwStatus = (String)getRowStatusBinding.execute();
if ("NEW".equalsIgnoreCase(rwStatus)){
iter.removeCurrentRow();
iter.refreshIfNeeded();
} else{
rw.refresh(Row.REFRESH_UNDO_CHANGES);
}
fctx.renderResponse();
}
8. Custom bean method to reset the UIcomponent (form) :
public void resetAction(ActionEvent aE) {
AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
resetValueInputItems(adfFacesContext,searchForm);
}
private void resetValueInputItems(AdfFacesContext adfFacesContext, UIComponent component){
List<UIComponent> items = component.getChildren();
for ( UIComponent item : items ) {
//resetValueInputItems(adfFacesContext,item); //uncomment in case of nested UIComponents
if ( item instanceof RichInputText ) {
RichInputText input = (RichInputText)item;
if ( !input.isDisabled() ) {
input.resetValue() ;
adfFacesContext.addPartialTarget(input);
}
} else if ( item instanceof RichInputDate ) {
RichInputDate input = (RichInputDate)item;
if ( !input.isDisabled() ) {
input.resetValue() ;
input.setValue("");
adfFacesContext.addPartialTarget(input);
}
} else if (item instanceof RichSelectOneChoice){
RichSelectOneChoice input = (RichSelectOneChoice)item;
if ( !input.isDisabled() ) {
input.resetValue() ;
input.setValue("");
adfFacesContext.addPartialTarget(input);
}
}
} //end -for each loop
}
thank u so much :)
ReplyDelete