Wednesday, August 29, 2012

Reset/Cancel a Form

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.


  1. <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.


  1. public void resetActionListener(ActionEvent actionEvent){
  2. UIComponent myForm = actionEvent.getComponent();
  3. oracle.adf.view.rich.util.ResetUtils.reset(myForm);
  4. }

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 .

  1. <af:commandButton text="Cancel" id="cb1" action="#{bindings.Rollback.execute}" inlineStyle="font-weight:bolder;" immediate="true" >
  2. <af:resetActionListener/>
  3. </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.


  1. public void resetActionUsingQueueListener(ActionEvent aEvent){
  2. //Queue the binded button's action to this button's action event.
  3. ActionEvent actionEvent = new ActionEvent(this.getResetButton());
  4. actionEvent.queue();
  5. }

6. Create instance of resetActionListener in bean and call processAction ()

  1. public void resetAct(ActionEvent actionEvent) {
  2. ResetActionListener ral = new ResetActionListener();
  3. ral.processAction(actionEvent);
  4. }

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.


  1. public String getRowStatus(Row row){
  2. DepartmentsViewRowImpl rwImpl = (DepartmentsViewRowImpl)row;
  3. String rwStatus = translateStatusToString(rwImpl.getEntity(0).getEntityState());
  4. return rwStatus;
  5. }
  6. private String translateStatusToString(byte b) {
  7. String ret = null; switch (b) {
  8. case Entity.STATUS_INITIALIZED: {
  9. ret = "Initialized"; break;
  10. }
  11. case Entity.STATUS_MODIFIED: {
  12. ret = "Modified"; break;
  13. }
  14. case Entity.STATUS_UNMODIFIED: {
  15. ret = "Unmodified"; break;
  16. }
  17. case Entity.STATUS_NEW: {
  18. ret = "New"; break;
  19. } }
  20. return ret;
  21. }

Provide the ActionListener ('onCancel') for Cancel Button on the page  -


  1. public void onCancel(ActionEvent actionEvent) {
  2. FacesContext fctx = FacesContext.getCurrentInstance();
  3. DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
  4. DCIteratorBinding iter = bindings.findIteratorBinding("DepartmentsView1Iterator");
  5. Row rw = iter.getCurrentRow();
  6. OperationBinding getRowStatusBinding = bindings.getOperationBinding("getRowStatus");
  7. String rwStatus = (String)getRowStatusBinding.execute();
  8. if ("NEW".equalsIgnoreCase(rwStatus)){
  9. iter.removeCurrentRow();
  10. iter.refreshIfNeeded();
  11. } else{
  12. rw.refresh(Row.REFRESH_UNDO_CHANGES);
  13. }
  14. fctx.renderResponse();
  15. }

8. Custom bean method to reset the UIcomponent (form) :


  1. public void resetAction(ActionEvent aE) {
  2. AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
  3. resetValueInputItems(adfFacesContext,searchForm);
  4. }
  5. private void resetValueInputItems(AdfFacesContext adfFacesContext, UIComponent component){
  6. List<UIComponent> items = component.getChildren();
  7. for ( UIComponent item : items ) {
  8. //resetValueInputItems(adfFacesContext,item); //uncomment in case of nested UIComponents
  9. if ( item instanceof RichInputText ) {
  10. RichInputText input = (RichInputText)item;
  11. if ( !input.isDisabled() ) {
  12. input.resetValue() ;
  13. adfFacesContext.addPartialTarget(input);
  14. }
  15. } else if ( item instanceof RichInputDate ) {
  16. RichInputDate input = (RichInputDate)item;
  17. if ( !input.isDisabled() ) {
  18. input.resetValue() ;
  19. input.setValue("");
  20. adfFacesContext.addPartialTarget(input);
  21. }
  22. } else if (item instanceof RichSelectOneChoice){
  23. RichSelectOneChoice input = (RichSelectOneChoice)item;
  24. if ( !input.isDisabled() ) {
  25. input.resetValue() ;
  26. input.setValue("");
  27. adfFacesContext.addPartialTarget(input);
  28. }
  29. }
  30. } //end -for each loop
  31. }

1 comment: