Tuesday, September 13, 2011

OAF Custom Clear Button - Different Approaches


While working on Search Pages more often that not we cannot use the Result-Based or AutoCustomization Criteria, we go for Manual Search Pages. To implement "Clear" button is sometimes not mandatory, but nice to have feature there.

Before discussing various approaches, lets observe some features of standard Clear Button:
  • It doesn’t do form submit.
  • It doesn’t clear the results.
  • It doesn’t clear any other fields on the page apart from Search Criteria fields
  • Standard Clear Button is implemented with JavaScript.

Clear Button Scenarios and Approaches:-

Approach 1: Using Submit Button to clear the search criteria fields:

Following is the code snippet:

        if(pageContext.getParameter("ClearBtn") != null)
       {
              OAMessageTextInputBean searchCriteriaTxt1=
                 (OAMessageTextInputBean) webBean.findChildRecursive("SearchCriteriaTxt1") ;
              searchCriteriaTxt1.setText(pageContext, null);
              // searchCriteriaTxt1.setText(null); will through Developer Mode Exception
              // since it is processFormRequest.
         }

      Advantages:
  • You can achieve same functionality as standard Clear button by standard and supported behaviour.
       Disadvantages:
  • It needs a form submit.

Approach 2: Using Submit button and redirect to same page

Add following snippet in processFormRequest

         if (pageContext.getParameter("ClearBtn") != null)
         {
           pageContext.forwardImmediatelyToCurrentPage(null, false, null);
         }

      Advantages:
  • Usage is specific to a scenario.
       Disadvantages:
  • It needs a form submit.
  • AM is not retained.
  • Its much slower compared to other approaches.

Approach 3: Using Button (not submit button) with JavaScript to achieve exact functionality like the standard clear button:

Write following code in ProcessRequest and it'll do the trick !!

    String clearFunc = "function clearFunction()";
    clearFunc = clearFunc + "{";
    clearFunc = clearFunc + "document.getElementById('SearchItem1').value='';" ;
    clearFunc = clearFunc + "document.getElementById('SearchItem2').value='';" ;
    clearFunc = clearFunc + "}" ;
   
    pageContext.putJavaScriptFunction("clearFunction",clearFunc);
    OAButtonBean clearButton2 = (OAButtonBean) webBean.findChildRecursive("ClearButton2") ;
    String javaSClear = "javascript:clearFunction();";
    clearButton2.setOnClick(javaSClear);

     Advantages:
  • No form submit, so disadvantages from the first two approaches are overcome.
  • Exactly same behaviour as Standard Clear Button.

     Disadvantages:
  • Use of Java Script, which is not recommended by Oracle. But we can’t live with it in Consulting J.
Reset Button: An approach worth ignoring:

A "ResetButton" bean cannot be trusted to perform, Clear button’s job.
A Reset button only resets the values on Page to last submitted/saved values (which needn’t be blank).

For Example: You enter "Prince" in Employee Name Search Criteria on Employee Search Screen and click on Go.
Irrespective of whether any results came or not, these submitted values are saved.

Now if you change the value on Employee Name search criteria to "Arvind".
Clicking on Reset Button will reset the value to ‘Prince’ and not to the desired null value.