Wednesday, April 29, 2009

Returning a Value from a Popup Page

I just ran into a situation where I needed to pass back a value from a pop-up page to a tabular form item in the calling page. While I've done this in the past ny manually writing all of the JavaScript, I thought that I'd take a look at the new documented APEX JavaScript APIs.

I came across this function in the APEX 3.2 API Reference Guide (Part #E13369-01):

$v_PopupReturn(pValue, pThat)
Sets the value of the item in the parent window (pThat), with (pValue) and then
closes the popup window.
Return Value
Not applicable.
Parameters
pValue (string)
pThat (DOM node | string ID)

Looks perfect!  In addition to passing the value back, it will also close the pop-up page, even though that is not documented.

On the pop-up page, I created a link with an Optional Redirect to URL, and put the following in the link:

javascript:$v_PopupReturn(#EMPNO#, '&P3_NODE.')

P3_NODE is the DOM ID of the tabular form item that I wanted to return the value to.  You'll need to make sure that it's formatted properly for this to work.  Thus, I am passing in a "1", but returning "f03_0001".  A simple computation can do the trick.

Since we're using a built-in tabular form, we need to put the calling JavaScript in the Element Attributes of the item that we want the popup form to set.  I used this:

onclick="popUp2('f?p=' + $v('pFlowId') + ':3:' 
+ $v('pInstance') + '::::P3_NODE:#ROWNUM#',250,600);" readonly

This will pop open a new window, based on page 3 of my application.  It will also grab the Application ID and Session ID from the DOM and use those in the link, so that we preserve our session state.  Finally, it will pass along the corresponding ROWNUM of the item that we clicked on to the item P3_NODE - where I will make the proper transformation with the computation on Page 3.  Lastly, the readonly option prevents users from making direct changes to the item.

Point taken from this - have a look at the APEX API guide before you embark on writing your own code.  There are a number of helpful functions there than can save a lot of time and hassle.

9 comments:

Taj said...

Nice. Just 2 weeks back I was looking for this kind of functionality. Didn't realize it is available in 3.2 version. I had to write my code (we are on 3.1.2).

Guess, one more reason to update to the newest version.

Thanks -Scott- for sharing.

-Taj

OraDude said...

Docs:

"$v_PopupReturn(pValue, pThat)
Sets the value of the item in the parent window (pThat), with (pValue) and then
closes the popup window."

You wrote:

"it will also close the pop-up page, even though that is not documented."

Assuming the first is a copy-paste from the documentation, I think your statement needs revision :-)

Scott said...

Nah, I just need to drink more coffee before posting blog entries. :)

Thanks for the correction.

- Scott -

Sujay Dutta, PMP said...

You can also use JQuery modal dialogs to achieve the same result. It will probably look nicer :)

http://jqueryui.com/demos/dialog/

Sujay

Scott said...

Sure, you can use jQuery, but the solution that I outlined does not rely on any additional components, making it a lot more portable and simple. Sure, it may not look as good, but it took less than 10 minutes to implement from start to finish.

Thanks,

- Scott -

Anonymous said...

Nice Post.
Do you have any applications where I can see how this has been implemented?
I am using APEX 4.0.Can we use the same in 4.0 too?

Scott said...

Sorry, I don't have a working example. It should still work in 4.0, but there may be a better way to do it.

- Scott -

AI said...

Hi Scott, If you were to return value back to parent page where parent page item was a pop-up LOV, how would it work?

In my case its just blank. The page closes so I know it works.

Chhers.

SNIPERCAT said...

Hi, Thanks for this post :) It was so useful, I'm working on apex 4.2 and as you said in a comment, "there may be a better way to do it" But this one is working excellent, and combined with Dynamic actions, I can create a Popup report ¿? :P something like a Popup LOV but showing a report instead of a list of links, it looks so much better and takes not much time.