Flexible AJAX Calls In APEX – Reports

Started to get fed up sticking the same old javascript in my HTML header on lots of pages so I decided to make my report refresh call a little more generic… now I can just have it in my general JS library and call it however I want…

This is used with a background report page that is fed details by the calling page and the report region is displayed in the current page with no page submit. I generally use it for a summary report, with a ‘Details’ column link that drills down to a further report with more info. The report’s actually in another page but is displayed on the same page with no refresh.

With this function you input the page number of your target report, the name of the div that you want it displayed in on the current page and then an array of the items (could include column substitutions etc.) on the current page and an array of the items of the target page that need to be populated.

Very flexible now – you can derive the target page and even where it should be displayed, not necessarily from static calls but even by item values (checkboxes, LOVs, whatever you want!).


<script type="text/javascript">
<!--
function get_tasty_report(pTargetPage,
                          pRegionDiv,
			  pSourceItems,
			  pTargetItems){

if (pTargetPage == null) {
alert('No target page is defined');
return;
}

if (pRegionDiv == null) {
alert('No region div name is defined');
return;
}

var get = new htmldb_Get(null,$x('pFlowId').value,null,pTargetPage);

if (pSourceItems !== undefined || pTargetItems !== undefined) {
	if (pSourceItems.length != pTargetItems.length) {
		alert('Wrong number of arguments in call to get_tasty_report');
		return;
	}

	for (var ii = 0; ii < pSourceItems.length; ii++) {
		get.add(pTargetItems[ii], pSourceItems[ii]);
	}
}

$x(pRegionDiv).innerHTML = get.get(null,'<ajax:TASTY_BODY>','</ajax:TASTY_BODY>');
}
//-->
</script>

You can see a little bit of rudimentary error handling in there that you can adapt if needed or simply remove – just there to throw things out to me when I was messing about with it.

Anyway, here’s a simple example using scott/tiger

http://apex.oracle.com/pls/otn/f?p=293:6

So all I’ve done is create a

SELECT *
FROM dept
WHERE deptno = : P5_DEPTNO

report region on page 5. I’ve also added a Display As Text(does not save state) item called P5_DEPTNO. Display condition is ‘Never’.

Next, I’ve copied the Printer Friendly template and called it AJAX Report. Then I replaced the ‘Body’ definition in my new template with…

<ajax:TASTY_BODY>
<div id="TASTY_BODY">#BOX_BODY#</div>
</ajax:TASTY_BODY>

and associated page 5 with the new AJAX Report template.

This (page 5) is the background page.

Now on page 6, I have a

SELECT *
FROM emp

report region.

I’ve also created a blank HTML region with a region source of…

<div id="my_div">
</div>

Then I’ve defined a ‘Department Details’ column with this as the link…

javascript:get_tasty_report('5','my_div',['#DEPTNO#'],['P5_DEPTNO']);

Note that the source items and target items are arrays – you can stick as many in there as you like, you can even target different pages and reports and display them in different regions on your page!

Advertisements

13 thoughts on “Flexible AJAX Calls In APEX – Reports

  1. Munky,

    Sorry for disturbing here .
    I am preparing IRR report. But i dont find any option for download in xls,rtf,xml format. I found only CSV or PDF format?

    Is there anything that i have to add option?

    Goiyala3

    • Hi

      Sounds like you don’t have a suitable print server set-up. This should really be addressed to the forum as it has nothing to do with this blog post.

      Cheers

      Ben

    • Shaun,

      Sorry about that – I was a little gung-ho whilst doing some pruning of the workspace!

      All is in order again now, so just try again using demo and demo.

      Cheers

      Ben

  2. Hi this looks really great and is just what I need for a project I am working on right now. I am new writing my own javascript in APEX and wondering wit the script that you posted does that go in the body of the Page 5 or Page 6 of the report template or page template?

    • Hi

      I’m not sure exactly what you’re looking for, I think that the description was pretty clear. Could you be more specific – or just e-mail me directly.

      Cheers

      Ben

      • Sure I was just wondering on this first piece of Code:

        <!–
        function get_tasty_report(pTargetPage,
        pRegionDiv,
        pSourceItems,
        pTargetItems){…..etc

        Where do you put that within APEX? Would that be placed on page 5 in the HTML header?

        Also I was wondering if the Details link could bring up a Tabular form? Would that be possible so that an end user could enter comments based on the details?

  3. Hi,
    Excellent useful bit of code.
    I use it to refresh read only sections of a page when they are updated via a popup window.
    Then I don’t need to refresh the whole page so the screen doesn’t flash.
    Cheers
    Michael

  4. Hi:

    This works great for one page report but when a Pagination Scheme is used for multiple page reports the Next Page icon is on Page 5. Clicking it takes you away from Page 6 to Page 5.

    How can we remain on Page 6 and get the next Page of Page 5 to display in ‘my_div’ of Page 6?

    Your reply needed urgently.

    Thanks.

    Okike2@yahoo.com

    • That is not an auto reply, if you look at the date of it – it was in response to a readers previous post in February.

      So, a human has now read your question and does not appreciate people demanding answers urgently and therefore does not feel inclined to help.

      Perhaps you could post in the forum?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s