26
APEX and AJAX – Where to Start Nov 2009 Tim St. Hilaire

St Hilaire Ajax Start Odtug Nov 2009

Embed Size (px)

DESCRIPTION

how to integrate ajax into apex and add more interactive effects

Citation preview

Page 1: St Hilaire   Ajax Start Odtug Nov 2009

APEX and AJAX – Where to StartNov 2009Tim St. Hilaire

Page 2: St Hilaire   Ajax Start Odtug Nov 2009

Introduction

We will build a series of examples that achieve the basic components of dynamic actions within APEX

• Partial Page Refresh reports• Calling On Demand Processes• Setting Session Variables with AJAX• Consuming Pages• Consuming Partial Pages• Debugging Notes

Page 3: St Hilaire   Ajax Start Odtug Nov 2009

Report Refresh – Current Behavior

• Every button press will submit the entire page• When a page is submitted, it re-draws the entire content

• NOTE – As APEX continues to improve, other components employ sophisticated partial page and dynamic refresh capabilities. Example – Pagination in PPR template reports and Interactive reports

Page 4: St Hilaire   Ajax Start Odtug Nov 2009

Demonstration

Page 5: St Hilaire   Ajax Start Odtug Nov 2009

Report Refresh – Basic Refresh

• Using a feature that is provided with APEX to provide partial page refresh capabilities for pagination.

• The example shows how a java script call can re-query a report region.• Two important pieces are required

1. Definition of the Refresh Capability

2. Initializing an Instance of the Object

Definition

Page 6: St Hilaire   Ajax Start Odtug Nov 2009

Report Refresh – Basic Refresh

• Note: The initialize is easiest to place in the region header

Why?• The Template Substitution Variable #REGION_ID# will only work within

the APEX region you are in.

Initialize the Object

Calling the Refresh

Page 7: St Hilaire   Ajax Start Odtug Nov 2009

Demonstration

Page 8: St Hilaire   Ajax Start Odtug Nov 2009

Report Refresh – Basic Refresh

• Issue: The report is refreshed, but the SEARCH box is not used in the report

Page 9: St Hilaire   Ajax Start Odtug Nov 2009

Report Refresh – Session Variable

• Understanding the values of session variables is very important with AJAX inside of APEX. The frameworks session variable construct is very powerful and flexible.

• Using an ON DEMAND process as a simple request to allow the session variable to be set

Page 10: St Hilaire   Ajax Start Odtug Nov 2009

On Demand Processes – Basic Example

• An On Demand Process are officially located under Application Processes and can be called as part of page processing, but are defined at the APPLICATION level as a Shared Component

Page 11: St Hilaire   Ajax Start Odtug Nov 2009

On Demand Processes – Basic Example

• The On Demand Process is an easy gateway for the PL/SQL developer to access their PL/SQL code

Page 12: St Hilaire   Ajax Start Odtug Nov 2009

Demonstration of On-Demand Usage

Page 13: St Hilaire   Ajax Start Odtug Nov 2009

Page, Application, or Global Session Variables?

You may have noticed, in the last example, that the On-Demand Process used an undeclared type of variable.

• In PL/SQL wwv_flow.g_x01 through wwv_flow.g_x10

• In JavaScript Parameter name x01 through x10

• Handy, but use carefully – generic placeholders with complex logic can get confusing

• Page or Application items serve the same purpose, but need to be declared. A little extra time up front, but clarity in where and how used.

Page 14: St Hilaire   Ajax Start Odtug Nov 2009

On Demand Processes – Naming

• As part of the design activity, keep in mind that you may have multiple On Demand Processes in an application. Take some time to name them in a way that is easy to understand and manage as your application grows.

• Example Process name = AJAX_MULTI_INSERT• Example Process name = AJAX_MULTI_DELETE

How could these names be improved?

Page 15: St Hilaire   Ajax Start Odtug Nov 2009

Note on JS Sources

• The sources for this code is available on the APEX installation media• Note: a_report actually calls htmldb_Get

Page 16: St Hilaire   Ajax Start Odtug Nov 2009

On Demand Processes

The flow used in the example shown is as follows:

• The JavaScript call initiates the On Demand Process call

• The On Demand process – contains the logic required to gather the session parameters and pass them to the PL/SQL layer

• The PL/SQL layer contains the business logic to interact with the table data

• After the On Demand process is completed, the previously shown feature to refresh the report in initiated by the JavaScript

JavaScript

On Demand Process

PL/SQL

Tables

AJAX – Report Refresh

Page 17: St Hilaire   Ajax Start Odtug Nov 2009

Debug Notes

On Demand processes are not the easiest thing to debug…

Using the NET feature of Firebug makes the job much easier:

Page 18: St Hilaire   Ajax Start Odtug Nov 2009

AJAX HTML Injection

• No – Not Hacking• Modification of the DOM to change the user interface is the primary goal• It is necessary to understand where you desire the HTML injected into

I want my HTML to go here

Page 19: St Hilaire   Ajax Start Odtug Nov 2009

Demonstration

Page 20: St Hilaire   Ajax Start Odtug Nov 2009

Consuming APEX Pages

• Using the HTMLDB_GET, it is possible to consume entire APEX pages.

• Although you can, doesn’t mean you should…. There are issues with branching and processing using this method

• Additional features inside the HTMLDB_GET package allows the partial retrieval of pages

Page 21: St Hilaire   Ajax Start Odtug Nov 2009

Consuming APEX Pages (partial)

Two settings helped make this example work

1. Custom Page Template

2. Option on the .get method

Page 22: St Hilaire   Ajax Start Odtug Nov 2009

On Demand Processes – Design Notes

• As with any programming, it is easy to get caught up in the multiple places to put variables and code• Where is JavaScript code placed?

• Per Page in the Header• Page 0• Region Source with no template• Region Header• Region Footer

• Where is business Logic?• PL/SQL Packages• Page Processes• On Demand Processes

• Where is UI Logic?

• Come up with a practice that works for you / your organization• Document it• Communicate it• Follow it

Page 23: St Hilaire   Ajax Start Odtug Nov 2009

Questions?

Page 24: St Hilaire   Ajax Start Odtug Nov 2009

References

Many thanks to all those that have come before me, and for those that take the time to help others grow and improve their skills by sharing their time an knowledge.

APEX• http://apex.oracle.com – Forum, Docs, Hosted Workspace• http://apex.oracle.com/pls/otn/f?p=38462 - Carl Backstrom JSON & AJAX• http://apex.oracle.com/pls/otn/f?p=11933 – Carl Backstrom Variety Examples • http://apex.oracle.com/pls/otn/f?p=31517 – Denes Kubicek – Everything!

Other Notes• Syntax Highlighter

• http://code.google.com/p/syntaxhighlighter/• ApexLib

• http://apexlib.oracleapex.info/AuthorUpdated and corrected presentation will be available on my blog

http://wphilltech.blogspot.com http://apex.oracle.com/pls/otn/f?p=226 – This example application

Tim St. Hilaire - [email protected]

Page 25: St Hilaire   Ajax Start Odtug Nov 2009
Page 26: St Hilaire   Ajax Start Odtug Nov 2009

Disclaimer• Marks and Brands are the property of their respective owners. Usage is for discussion

purposes only. No ownership assumed or implied.• The comments and opinions expressed here are sole responsibility of the author and not

of his employer or any other party• No trees were harmed during the creation of this presentation. However, a great number

of electrons were terribly inconvenienced.