Vous êtes sur la page 1sur 45

Building Dynamic Actions in Oracle Application Express 4.

0
This tutorial contains the following sections:

Purpose
Time to Complete
Overview
Prerequisites
Creating a Database Application
Making Some Changes to Items on Your Page
Creating an Enable/Disable Declarative Dynamic Action
Creating a Set Value Dynamic Action With a SQL Statement
Create a Set Value Dynamic Action Using PL/SQL
Using a Plug-in Event in a Dynamic Action
Summary

Purpose
This tutorial shows you how to using the built-in declarative dynamic actions in Oracle Application Express 4.0.

Time to Complete
Approximately 60 minutes.

Overview
What are Dynamic Actions?

Dynamic actions provide developers with a way to define client-side behavior declaratively without the need to know
JavaScript. Using a simple wizard, developers can select a page item, a condition, enter a value, and select an action (for
example, Show, Hide, Enableand Disable). Previously this involved hand crafting JavaScript and AJAX but with APEX 4.0
many Dynamic Actions require zero coding using the built-in wizards.

Prerequisites
Before starting this tutorial, you should:

Install Oracle Database 11g.


Install Oracle Application Express Release 4.0.
Download and unzip the apexdynactions.zip file into your working directory.
Create a workspace and user to perform this tutorial.

Creating a Database Application


To create a database application with a few initial pages, perform the following steps:

1 . Enter the following URL in your browser to log in to Oracle Application Express.

http://localhost:8080/apex

2 . To log in to Oracle Application Express, enter the following details, and click Login.

Workspace: <your workspace name>


Username: <your username>
Password: <your password>
3 . From the Oracle Application Express home page, click the down arrow next to Application Builder tab and select
Applications > Database Applications.

4 . Click Create.

5 . Select Database and click Next >.


6 . Select From Scratch and click Next >.

7 . For Name, enter Human Resources Application. Keep the default value of From Scratch for the Create
Application, and click Next >.

8 . Enter Home for Page Name and click Add Page.


9 . You want to create an Interactive Report and a Form on the EMP table. Select Report and Form for Page Type.
Select Home(1) for Subordinate to Page, EMP for Table Name, and click Add Page.

10 . The Report and Form pages were created. You want to change the Name of the Report page. Select the Emp link
for the Report.
11 . Change the Page Name to Employee List and click Apply Changes.

12 . You also want to change the Name of the Form page. Select the Emp link for the Form.

13 . Change the Page Name to Employee Details and click Apply Changes.
14 . You are now ready to continue to the next wizard page, click Next.

15 . Accept the default and click Next.


Making Some Changes to Items on Your Page
Before you create some Dynamic Actions, you want to make some changes to the Items on your page. Perform the following
steps:

1 . In the Page Rendering area under Regions, locate the P3_JOB item under the Employee Details region. Right click
P3_JOB and select Edit.

2 . Select Select List for Display As and click List of Values tab.
3 . Select No for Display Extra Values and enter the following SQL statement in the List of values definition area and
click Apply Changes.

select distinct job d, job r from emp order by 1

4 . To test the change you just made, click Run.

5 . Notice that Job is now a select list. You want to make some additional changes. Click Edit Page 3 from the developer
toolbar.
6 . You want to change many of the item labels. Right click Items and select Edit All.

7 . Change the following Prompts for each of the items below and click Apply Changes.

Item Prompt
P3_ENAME Name
P3_MGR Manager
P3_HIREDATE Hire Date
P3_SAL Salary
P3_COMM Commission
P3_DEPTNO Department

8 . Click the Edit Page 3 icon.

9 . You also want to change the Manager (currently a number) to a list of Manager Names. Right click P3_MGR and
select Edit.
10 . Select Select List for Display As and click List of Values tab.
11 . Select No for Display Extra Values, select Yes for Display Null Value, enter - Select Manager - for Null Display Value
and enter the following SQL statement in the List of values definition area and click Apply Changes.

select ename d, empno r from emp where job in ('MANAGER', 'PRESIDENT')

12 . You want to change the Deptno to a list of Department Names. Right click P3_DEPTNO and select Edit.
13 . Select Select List for Display As and click List of Values tab.
14 . Select Yes for Display Extra Values, select Yes for Display Null Value, enter - Select Department - for Null Display
Value and enter the following SQL statement in the List of values definition area and click Apply Changes.

select dname d, deptno r from dept

15 . To see all the changes you made, click Run.


16 . Notice the changes to the labels, and Job, Manager and Department and now select lists. In the next section, you will
create a Dynamic Action to enable the Commission item only when the Job is equal to SALESMAN. Select the Edit
Page link in the developer toolbar.

Creating an Enable/Disable Declarative Dynamic Action


You want to create Dynamic Action that will enable the Commission item when the Job equals SALESMAN. All other Jobs will
set the Commssion item to Disable. Perform the following steps:

1 . Right click P3_JOB and select Create Dynamic Action.


2 . Select Standard and click Next. Note that a Standard dynamic action allows you to create a dynamic action that will
fire when a field's value changes, and will subsequently 'Show', 'Hide', 'Enable', or 'Disable' other page items.
3 . Enter JOB EQUALS SALESMAN for the Name and click Next.

4 . You want the Dynamic Action to be fired when the Job item is equal to SALESMAN. Because you created a dynamic
action on the P3_JOB item, the values for Selection Type, Item(s) and Condition should already be specified. Enter
SALESMAN for Value. Then click Next.

5 . Select Enable for Specify the True Action. This action will be invoked when the value of P3_JOB is equal to
'SALESMAN'. Note that the Create Opposite False Action is checked which will create a Disable action that will be
invoked when the value of P3_JOB is not equal to SALESMAN. Click Next.
6 . The item that will be enabled or disabled is Commission. Select Item(s) for Selection Type and select P3_COMM
from the list of items and click the Move icon to move it to the right and click Create.

7 . You can run the page. Click Run.


8 . Notice that the Commission item is enabled because the Job is equal to SALESMAN.

9 . Change the Job to CLERK.

10 . Notice that the Commission item is now disabled.


Creating a Set Value Dynamic Action With a SQL Statement
In this section, you will create a set value Dynamic Action that will show the location of a Department when it is changed.
Perform the following steps:

1 . Select the Edit Page 3 link in the developer toolbar.

2 . You first need to create a new item for Location. Under Employee Details, right click Items and select Create Page
Item.
3 . Select the Display Only Item Type and click Next.
4 . Enter P3_LOCATION for Item Name and click Next.

5 . Accept the defaults and click Next.

6 . Select Yes for Save Session State and No for Show Line Breaks and click Next.

7 . Accept the defaults and click Create Item.


8 . Now you can create the Dynamic Action to set the value of the new P3_LOCATION item. You want the location to be
displayed when the value of the Department changes. Right click P3_DEPTNO and select Create Dynamic Action.
9 . Select the Advanced dynamic action type and click Next. Note that the Advanced option allows you to create a
more complex native dynamic action such as 'Add Class' or 'Set Value', where you can specify different event types
such as 'Click' or 'After Refresh' and also utilize plug-in dynamic actions, that are installed in your application.
10 . Enter GET DEPARTMENT LOCATION for Name and click Next.

11 . The Event, Selection Type and Item(s) fields are prepopulated. Click Next. Note that you don't want to specify a
condition because you want the dynamic action to always be invoked.

12 . Select Set Value for Action, select SQL Statement for Set Type, enter the following SQL Statement in the area
provided and enter P3_DEPTNO for Page Items to Submit and click Next.

select loc from dept where deptno=:P3_DEPTNO


13 . This page contains the page elements the dynamic action should control. In this case, you want the P3_LOCATION
item to change. Select Item(s) for Selection Type and select P3_LOCATION for Item(s) and click Create.

14 . Now you can run the page, click Run.


15 . The location is displayed based on the Department specified.

16 . Change the Department to ACCOUNTING.


17 . Notice that the Location changes automatically. In the next section, you will set a value using a dynamic action but
this time you will use PL/SQL instead of a SQL statement.

Create a Set Value Dynamic Action Using PL/SQL


You want to calculate the bonus that a particular employee gets based on their job and salary. In this section, you will create
a new Bonus item and then a dynamic action that will calculate the bonus based on both the job and salary specified.
Perform the following steps:

1 . Select Edit Page 3 in the developer toolbar.


2 . You want to first create the Bonus item. Under the Employee Details region, right click Items and select Create
Page Item.
3 . Select Number Field and click Next.

4 . Enter P3_BONUS for Item Name and click Next.

5 . Accept the defaults and click Next.


6 . Select the up arrow.

7 . Select the $5,234.10 link.

8 . Click Next.
9 . Accept the defaults and click Create Item.

10 . Now you are ready to create your dynamic action. Right click Dynamic Actions and select Create.
11 . Select Advanced and click Next.
12 . Enter CALCULATE BONUS for Name and click Next.

13 . The Event and the Selection Type are prepopulated. Enter P3_JOB, P3_SAL for Item(s) and click Next.

14 . Select Set Value for Action and select PL/SQL Function Body for Set Type. Enter the following PL/SQL code into
the PL/SQL Function Body area, enter P3_JOB,P3_SAL in the Page Items to Submit field and click Next.

declare
l_multiplier number;
begin
-- determine multiplier based on job
case :P3_JOB
when 'CLERK' then l_multiplier := .1;
when 'ANALYST' then l_multiplier := .2;
when 'SALESMAN' then l_multiplier := .3;
when 'MANAGER' then l_multiplier := .4;
when 'PRESIDENT' then l_multiplier := .5;
else l_multiplier := 0;
end case;
-- return bonus which is calculated by multiplying salary my multiplier
return :P3_SAL * l_multiplier;
END;
15 . Select Items for Select Type and enter P3_BONUS for Item(s). Then click Create.

16 . You can now run the page to test the dynamic action. Click Run.
Using a Plug-in Event in a Dynamic Action
In this section you use a plug-in event in a dynamic action. Perform the following steps:

1 . You want to import the slider plugin. Click the Shared Components icon.

2 . Under User Interface, select Plug-ins.

3 . The slider plugin has been provided for you in the files directory. Click Import.

4 . Click the Browse... button to locate the file. Select the item_type_plugin_com_oracle_slider.sql file in the files
directory and click Open.
5 . Click Next.

6 . The file was successfully imported. Click Next.


7 . Make sure that your Application is select from the drop down list box and click Install Plug-in.

8 . You want to use this Slider Plugin for the P3_SAL item.. Click the Edit Page icon.

9 . You want to change the P3_SAL item to use the slider plugin you just imported. Right-click the P3_SAL item and
select Edit.
10 . Select Slider [Plug-in] for Display As and enter 50000 for Maximum Value and 100 for Step. These settings
configure the slider for this item. Then click Apply Changes.
11 . Now you are ready to create the dynamic action. Right-click the P3_SAL item and select Create Dynamic Action.
12 . Select Advanced and click Next.

13 . Enter SALARY HIGH for Name and click Next.


14 . Select Change [Slider] for Event, make sure P3_SAL is select for Item(s), select greater than for Condition and
enter 25000 for Value. These settings will tell the dynamic action when to be invoked. Then click Next. .

15 . Select Set Style for Action, enter color for Style Name, and red for Value. These 'True' action settings will be
invoked when the salary value is greater than 25000. Then click Next.

16 . You also need to select the action that will take place when the action is false. Select Set Style for False Action,
enter color for Style Name and black for Value. These 'False' action settings will be invoked when the salary value is
not greater than 25000. Then click Next.
17 . Select DOM Object for Selection Type and enter P3_SAL_display for DOM Object and click Create. The DOM
Object is case sensitive so make sure you enter it exactly as specified in the screenshot below.

Note: If you want to change the appearance of the displayed value of the Slider you have to access it using
P3_SAL_display which is the ID for that object in the DOM object tree generated by the Slider plug-in. This naming
convention is specific to the Slider plug-in and could be completely different for another plug-in.

18 . You are now ready to test the slider plug-in and dynamic action. Click the Run Page icon.

19 . Notice that the Salary field is a slider type. Drag the bar to the right so that it is larger than 25000 and release your
mouse.
20 . Notice that the display value is now in red because the dynamic action was fired. You can also drag it to less than
25000 and release and you will see that it changes back to black.

Summary
In this tutorial, you have learned how to:

Create an enable/disable standard dynamic action


Create a Set Value dynamic action using a SQL Statement
Create a Set Value dynamic action using PL/SQL
Use a Plugin in a dynamic action
About Oracle |Oracle and Sun | | Careers | Contact Us | Site Maps |
Legal Notices | Terms of Use | Your Privacy Rights

Vous aimerez peut-être aussi