Skip to Main Content

APEX

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TIP: Building Modal Popup Windows

fzdevMay 23 2006 — edited Jul 21 2010
Build Modal Popup Pages

Introduction
The following is based on the 'How To Document' - 'Build Custom Popup Pages'.
This How To will show the developer how to create Modal Popup windows with the ability to pass information back to the parent window.

Example Scenario
The example described in this how to adds a custom modal popup LOV to a form on the SCOTT.EMP table. Clicking the popup LOV link on the form page will popup a Modal LOV page that allows users to search by ENAME, JOB, and SAL. Selecting a value from this LOV page will close that popup LOV page and populate the ENAME, JOB, and SAL fields on the form page with the selected values. Additionally, had the user entered some data into the ENAME, JOB, and/or SAL fields on the form page, that data would be used in the initial search when the popup LOV page is first shown.


Step 1 - Create a simple form page on SCOTT.EMP
To create the form page, simply step through the HTML DB "Form on a Table or View" wizard against the EMP table accepting the defaults. For this example, create the form with a page number of one, and make sure to allow the ename, job, and sal fields to be editable. When the wizard completes, page 1 of the application should have the items P1_ENAME, P1_JOB, and P1_SAL on it as text fields.


Step 2 - Create a popup page with search fields
Next, create a page that's to be used as the popup window: Page 2. Ultimately, this page will have javascript, a report region, and some buttons. For now, though, just create a page 2 with the items P2_ENAME, P2_JOB, and P2_SAL on it as text fields.


Step 3 - Set Modal popup page header requirements
A) Modal windows by default cache the information they display. When creating a popup window which displays different information each time it is called or to allow searching the caching default must be switched off.
Add the following meta-tag to the "HTML Header" field of the page-level attributes screen for page 2 to accomplish this:
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
B) Modal windows by default open a new window when running any redirect calls. When creating a modal popup which passes back parameters to the parent window. The modal window has to be forced to fire redirect calls within its own window rather than open a new window for the redirect.
Add the following tag to the "HTML Header" field of the page-level attributes screen for page 2 to accomplish this:
<base target="_self">
Step 4 - Add Javascript call to the popup page
Because the popup page should filter its result set based on any values that the user might have entered onto the form page, you need to add a javascript function that would pass those values off. Add the following function to the "HTML Header" field of the page-level attributes screen for page 1 to accomplish this:
<script language="JavaScript" type="text/javascript">
  function callMyPopup (formItem1,formItem2,formItem3) {
    var formVal1 = document.getElementById(formItem1).value;
    var formVal2 = document.getElementById(formItem2).value;
    var formVal3 = document.getElementById(formItem3).value;
    var url;
  url = 'f?p=&APP_ID.:2:&APP_SESSION.::::P2_ENAME,P2_JOB,P2_SAL:' + formVal1 + ',' + formVal2 + ',' + formVal3 ;
  // IE Browsers modal popup window
  if (window.showModalDialog) {
    w = showModalDialog(url, window.self,"status:0; scroll:1; resizable:1; dialogWidth:25; dialogHeight:43");
    // w is an array returned from the modal popup containing the passback values
    if (w) {
      document.getElementById("P1_ENAME").value = w[0];
      document.getElementById("P1_JOB").value = w[1];
      document.getElementById("P1_SAL").value = w[2];
      document.getElementById("P1_SAL").focus();
    }
  }
  else {
  // mozilla based browsers modal popup window
  w = open(url,"winLov","Scrollbars=1,resizable=1,width=800,height=600", modal="yes");
  if (w.opener == null)
    w.opener = self;
  w.focus();
  }
</script>
Though you don't need to know how to read javascript for this example, it helps to understand that this function only does one important thing: it opens a modal popup window of page two in our application while passing values to P2_ENAME, P2_JOB, and P2_SAL. It gathers those values to pass from the names of the HTML DB items provided to it. The call to this function will be added in the next step below.


Step 5 - Add a popup link next to the P1_SAL field on the form page
Add a link to the form page that will call the callMyPopup function above and pass it the values it needs. To do so, place the following HTML in the "Post Element Text" field attribute of the P1_SAL item:
<~a href="javascript:callMyPopup('P1_ENAME','P1_JOB','P1_SAL');">Click for LOV<~/a>
NB. remove ~ from above when deploying

Step 6 - Add the LOV report to the popup page

The next step is to create a report on the popup page based on the values passed from the form on page one. The tricky part of this report is providing a means for the selected values to be passed back to the form page. This can be achieved by having a column of the report render as a javascript link that would close the popup window and pass the ename, job and sal values for that row back. Start by adding a simple report region to our Modal popup page that uses a query such as:
      select ename, job, sal , 'placeholder' the_link
        from emp
       where ename like '%'||:P2_ENAME||'%'
         and (job = :P2_JOB or :P2_JOB is null)
         and (sal = :P2_SAL or :P2_SAL is null)
Note that the last column in this query is just a placeholder. once the region is created, turn that placeholder into a link by doing the following:
Navigate to the Page Definition for page 2
Next to the name of the report region created in step 6, Click Q
Next to the column THE_LINK, click the edit icon
In the "Link Text" field enter the string "select"
In the URL field enter:
 javascript:passBack('#ENAME#','#JOB#','#SAL#'); 
Click the "Apply Changes" button


Step 7 - Add the javascript function to the Modal popup page to pass selected values to the (parent) form page.
In the previous step you added a call to a javascript function, passBack. Now add that function to the top of Modal popup page 2. In the same manner as step 4 above, add that passBack function to the page 2 by putting it in the "HTML Header" field of the page-level attributes screen. The function should look similar to the following:
<script language="JavaScript">
   function passBack(passVal1, passVal2, passVal3)
   {
     // IE Browser return the passback values in an array
     if (window.showModalDialog) {
       var retVal = new Array(passVal1, passVal2, passVal3);
       window.returnValue = retVal;
       window.close();
     }
     // Mozilla based browsers right the passback values directly into the parent window
     else {
       opener.document.getElementById("P1_ENAME").value = passVal1;
       opener.document.getElementById("P1_JOB").value = passVal2;
       opener.document.getElementById("P1_SAL").value = passVal3;
       opener.document.getElementById("P1_SAL").focus();
       close();
     }
   }
</script>
This function simply sets the values of P1_ENAME, P1_JOB, and P1_SAL with the values of the whatever's stored to the three HTML DB item names passed to it. It also closes the current window and puts the cursor back into the P1_SAL field.


Step 8 - Polishing
The basic functionality of this custom modal popup window has been created in steps 1 though 7. To make its usage a little more friendly, it's advisable to add Cancel and Search buttons to the popup window page. The Search button should just be added as a regular button that branches back to the page 2. Adding this button allows users to re-query for LOV options without having to return to our form page. The Cancel button should be created with an "Action" of "Redirect to URL". The "URL Target" of the button should be javascript:window.close(). As the code suggests, the Cancel button would just close the popup window (with no values returned).

Hope this is of use....
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 18 2010
Added on May 23 2006
1 comment
2,395 views