Posts Tagged ‘Javascript’

ApEx Select list with Redirect confirm change

Thursday, July 17th, 2008

We recently had a situation that required a little bit of head scratching.  We had a page with a Select List with Redirect that was used to populate some form fields based on the contents of the Select List.  The users could change the values in the form and save them, but if they changed the Select List before using the ‘Save’ button then the changes would be lost.  What we wanted to do was add a confirmation message to make sure the user saved the details before changing the Select List.

Unfortunately you can’t put an ‘onchange’ handler in the HTML Form Element Attributes as ApEx is using onchange to do the redirect. Any code we put in is simply ignored.

Instead, we need to mimic what ApEx does, and then put in our code.  Firstly define some JavaScript in the page header :

<script type="Text/JavaScript">
var startVal = &P68_PERSON_ID.;
function checkSave(p) {
    if (confirm('Have you saved details for this person?')) {
       location.href='f?p=&APP_ID.:68:&SESSION.::NO::P68_PERSON_ID:'+
                  p.options[p.selectedIndex].value;
    } else {
       for (i=0;i<p.options.length;i++) {
          if (p.options[i].value == startVal) {
             p.options[i].selected = true;
          } else {
             p.options[i].selected = false;
          }
       }
    }
}
</script> 

This code is from page ‘68′ in our application, and the Select List is called P68_PERSON_ID.   You’ll need to change these to something appropriate for your app. 

The JavaScript saves the initial value set when the page loads here :

var startVal = &P68_PERSON_ID.;

If the user confirms they have saved the details, then the window’s location is changed and the selected item used as a basis for the new page.  If the user cancels we need to reset the selected item back, as by now the browser has changed it for us (thanks!).

Next, change the Select List with Redirect to just a normal Select List, then, add an onchange handler to the the Form Element Attributes to call the JavaScript :

Now, when the user changes the value in the list box they get prompted to check they have saved the details, which prevents them changing the page data inadvertently.

Using Ajax to populate one field based on another in an ApEx form

Friday, June 20th, 2008

Often it can be useful to populate one field on a form based on the content of another in a live and dynamic fashion. Luckily this is easily achieved using the Javascript API supplied as part of ApEx 3.1.

This post illustrates an example where we wanted to lookup a value (called IMD Rank) based on the Postcode entered in another field. Users can either enter it manually, or click on a ‘Lookup’ button next to the field.

Firstly, define your process that will calculate the required value. This is most easily achieved by creating an Application Process (under Shared Components) which will run ‘on demand’.

This PL/SQL is just retrieving a value (RANKOFIMD) from a table based on a page item (P27_P9_POSTCODE). Note the ‘htp.prn()’ function call which will actually ’send’ the result to our page.

Next, define the JavaScript required to execute the process and return a value.  We’ll be displaying the returned value in the page item called P27_P16_IMDRANK. Add this script to the HTML Header section of your page.

Note : it is OK to append this script to any code which may already be in this field.

Next, add a button to invoke the JavaScript. I just added a standard HTML button after the element :

Clicking on the Lookup button now populates the field based on the value retrieved by the GetIMD application process.