JavaScript in OAF page

JavaScript in OAF page

Often we come across the requirements in OAF where we need to use JavaScript to handle the UI events at run time. For example if you want to know the count of characters you have filled in a Text box or a warning Alert to popup to user.

For a UI developer it is a child's play as they manipulate the HTML tags directly and provide desired UI functionality to the page with JS or Angular but for an OAF developer hardly they ever touch HTML or JS in OAF pages due to its closed box nature. So let's see how the Pages are formed in OAF.



In OAF view is Implemented using UIX technology i.e. we define the structure of a Pages in XML file, these XML files will be translated into HTML pages at run time by OA Framework. So we can never directly manipulate the HTML code of the page as we hardly have any control on it. Thus if we want to attach some JavaScript function to the page we need to define it in the Controller Class using given API.

Let's consider a scenario here where I have to display the count of characters entered in the text box. First we will create an HTML page for this and will test it, below is the code for the same. In the below code there are two elements, One is a text box and other is just a span element like Styled text in OAF. I am referring the text area HTML element and counting the characters in it. The fetched value is being set in span element. This method will be called on the keyup, keydown and mouseout events. (If you dont know the events on HTML Elements you can refer W3Schools )

<script type="text/javascript">
function cntRemaingChars() {
  var len = document.getElementById("description").value.length;
  document.getElementById("RemainingChars").innerHTML = len+" characters entered";
}

</script>

<textarea id="description" cols="40" rows="5"
onkeyup="cntRemaingChars();" onkeydown="cntRemaingChars();" onmouseout="cntRemaingChars();"></textarea>
<br>
<span id="RemainingChars">0</span>

Now once you have tested the Word count logic in HTML Browser we can Implement the same in our CO Method ProcessRequest() as given below. 3 steps are followed to achieve that.

  1. Declare the JavaScript in a String and embed same into page by calling method defined in Interface OAPageContext
  2. Find the webBean on which you want to put the event on which the function should be called. To put the events we will UIConstants, this Interface has all the HTML page events defined in it.
/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package xxcust.oracle.apps.amt.test;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.server.OADBTransactionImpl;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;

import oracle.cabo.ui.UIConstants;

/**
 * Controller for ...
 */
public class test extends OAControllerImpl {
    public static final String RCS_ID = "$Header$";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

    /**
     * Layout and page setup logic for a region.
     *
     * @param pageContext the current OA page context
     * @param webBean     the web bean corresponding to the region
     */
    public void processRequest(OAPageContext pageContext, OAWebBean webBean) {
        super.processRequest(pageContext, webBean);

        // Declare a JavaScript function in a String
        String customJSFunction = "function cntRemaingChars() {\n" +
                "  var len = document.getElementById(\"Description\").value.length;\n" +
                "  document.getElementById(\"CountChars\").innerHTML = len+\" characters left\";\n" +
                "}";

        pageContext.putJavaScriptFunction("cntRemaingChars", customJSFunction); // Embed JavaScript function in the page

        // Find the element on which you want to set the events for JavaScript function call.
        OAMessageTextInputBean log = (OAMessageTextInputBean) webBean.findIndexedChildRecursive("Description");
        log.setAttributeValue(UIConstants.ON_KEY_DOWN_ATTR, "cntRemaingChars();");
        log.setAttributeValue(UIConstants.ON_KEY_UP_ATTR, "cntRemaingChars();");
        log.setAttributeValue(UIConstants.ON_MOUSE_OUT_ATTR, "cntRemaingChars();");

    }

    /**
     * Procedure to handle form submissions for form elements in
     * a region.
     *
     * @param pageContext the current OA page context
     * @param webBean     the web bean corresponding to the region
     */
    public void processFormRequest(OAPageContext pageContext, OAWebBean webBean) {
        super.processFormRequest(pageContext, webBean);
    }
}

Now as we have set the JavaScript in the page and the event on which to call it. Let's run the page.

Now in another Scenario we want to show a popup to user when the GO button is clicked. This can be achieved in Multiple ways, we can create the JS String and assign it to the button's OnClick event. Instead of assigning string you can directly write it as well as shown in below code.

Note : We don't need to create function for alert as alert() is a JS function
String showPopup = "alert(\"Hello! I am an alert box!!\");"; // declare JS Alert

OASubmitButtonBean btn = (OASubmitButtonBean) webBean.findChildRecursive("GoBtn");
btn.setAttributeValue(UIConstants.ON_CLICK_ATTR, showPopup); // Give Alert

//btn.setAttributeValue(UIConstants.ON_CLICK_ATTR, "alert(\"Hello! I am an alert box!!\");"); // Give Alert


Or we can pass the JS String to setOnClick method which will take care of it.

String showPopup = "alert(\"Hello! I am an alert box!!\");"; // declare JS Alert

OASubmitButtonBean btn = (OASubmitButtonBean) webBean.findChildRecursive("GoBtn");
btn.setOnClick(showPopup); // Give Aler



Hi Amit, How do we do this for checkbox. I need to show an alert popup window when a checkbox is checked. Could you please help me in this regard. Thanks, Rhea.

Like
Reply

Since this post seems pretty recent, maybe you can help me. In my test case, I have an image with an area map on it, meaning, on the image I have different clickable parts. Each part has a different html ID, so in javascript, I can do the onclick event. But then I have checkboxes on the side of the image, and what I want to do is, if I click on "1." in the image, I want the checkbox "1." to become checked, and the value be ready to be sent to the database like it would if I would've just clicked the checkbox. Is this possible with OAF?

Like
Reply

To view or add a comment, sign in

More articles by Amit Maindola

  • Load data from Files in ODI

    Recently I have created a small video Tutorial series demonstating loading of files in ODI taking a real Production…

  • A quick Guide to Performance Tuning in Oracle

    Often, we come across situations where our query is taking too long to retrieve results than expected. Initially, when…

  • Power of Templates in NP++

    In the Oracle Applications we often need to build an Interface or an Extension in PL/SQL OR a search page in OAF. These…

  • Oracle Workflow - Block Activity

    Few days ago while developing the Oracle Workflow for one of my client I came across a scenario where my one workflow…

    1 Comment
  • Performance Tuning & Re-usability in OAF

    Multiple times I have come across the Question regarding Performance tuning in OAF to fetch results or to Perform any…

Others also viewed

Explore content categories