Chapter 7. Dynamic Actions

by Martin Giffy D'Souza

Dynamic Actions are one of the most popular new features in APEX 4.0. They allow developers to declaratively define actions based on browser events. This chapter will cover all aspects of Dynamic Actions including the Create wizard, modifying a Dynamic Action, and how Dynamic Actions can integrate with each other. The chapter is divided into three main sections. It will first compare the old manual way of client-side manipulating using custom JavaScript to a Dynamic Action. The second section will cover all the options and features available with Dynamic Actions. A detailed example using multiple Dynamic Actions is covered in the last section.

This chapter assumes that you are familiar with APEX and have a basic understanding of the following web technologies:

  • JavaScript

  • jQuery

  • CSS

  • HTML

If you are unfamiliar with any of these languages or need additional information, please go to http://www.w3schools.com. The JavaScript (JS) examples provided in this chapter will use jQuery, which is included as part of APEX 4.

Custom JavaScript vs. Dynamic Actions

The best way to highlight Dynamic Actions is to compare them to the old manual method. This section will examine a simple problem you may have faced when developing an APEX application and resolve it first manually and then using a Dynamic Action. The purpose is to demonstrate the manual (old) vs. Dynamic Action (new) way to handle browser-based events in APEX.

Suppose you have a select list for a department (P1_DEPTNO) and employee (P1_EMPNO) on Page 1. If no department is selected, the list of employees should not be displayed, as shown in Figure 7-1. Once a department is selected the list of employees should appear as shown in Figure 7-2. The two subsections that follow compare the old and new ways to implement the functionality shown in the figures.

No department selected

Figure 7-1. No department selected

Department selected

Figure 7-2. Department selected

Manual (Old Method)

Prior to APEX 4, if you wanted to perform an action based on a browser event you needed to write custom JavaScript (JS) code. Writing custom JavaScript code poses several issues:

  • The code can be stored in different locations such as an external file, HTML region, region header, as part of a page process, etc. This could make it hard to find the exact location of the code if you needed to debug or modify it.

  • If developing in a team environment, each developer may use different techniques and code might not be reusable.

  • Not all APEX developers understand JavaScript.

The following JavaScript example shows how to toggle a field based on a browser event prior to APEX 4. This code snippet needs to be included in your page. Since there are multiple ways to include it in your page, it can be hard for someone to track down where the code is stored.

/**
 * Toggle Empno based on P1_DEPTNO's value
 */
function showHideEmpno(){
  if ($('#P1_DEPTNO').val().length > 0)
    $('#P1_EMPNO').closest('tr').show();
  else
    $('#P1_EMPNO').closest('tr').hide();
}//showHideEmpno

//Wait for page to be ready
$(document).ready(function() {
  //Show/Hide Empno after page load
showHideEmpno();

  //Register browser event (onChange)
  $('#P1_DEPTNO').change(function(){showHideEmpno();});
});

Dynamic Action (New Method)

Starting in APEX 4.0 you can declaratively trigger actions based on browser events using Dynamic Actions. Dynamic Actions are a preferred method over custom JavaScript functions because:

  • They are declarative, which allows developers to easily identify where the code is stored.

  • They have a built-in framework to maintain consistency across an application.

  • They make it easy for non-JavaScript developers to apply event-based actions.

The following steps describe how to toggle the Employee field, as you did in the previous example, using a Dynamic Action. This example will not go into detail for each step since the goal is to quickly compare the manual method with the new method by creating Dynamic Actions. The second section of this chapter will describe each of the available options in detail.

  1. On the page, right-click on the Dynamic Actions entry in the tree as shown in Figure 7-3. This will bring you to the start of the Dynamic Action wizard.

    Create Dynamic Action

    Figure 7-3. Create Dynamic Action

  2. On the Implementation page, select Standard and click the Next button as shown in Figure 7-4. The Advanced wizard will be covered later on in this chapter.

    Create Dynamic Action: Implementation

    Figure 7-4. Create Dynamic Action: Implementation

  3. On the following page, enter Show Hide EmpNo as the Name, as shown in Figure 7-5. Click the Next button to continue.

    Create Dynamic Action: Identification

    Figure 7-5. Create Dynamic Action: Identification

  4. On the When page, set values as shown in Figure 7-6 and click the Next button.

    Create Dynamic Action: When

    Figure 7-6. Create Dynamic Action: When

  5. On the True/False Actions page, select Show under Specify the True Action and check Create Opposite False Action as shown in Figure 7-7. Click the Next button to go to the final step.

    Create Dynamic Action: True/False actions

    Figure 7-7. Create Dynamic Action: True/False actions

  6. On the Affected Elements page, choose Item(s) as the Selection Type and select P1_EMPNO as shown in Figure 7-8. Click the Create button to complete the wizard.

    Create Dynamic Action: Affected Elements

    Figure 7-8. Create Dynamic Action: Affected Elements

You should now see the Show Hide EmpNo Dynamic Action in the Page Rendering section as shown in Figure 7-9.

Show Hide EmpNo Dynamic Action

Figure 7-9. Show Hide EmpNo Dynamic Action

When you refresh Page 1 the Employee select list will only appear when the Department item is not null. 07_example-1.sql is the application for this demo.

Note

The Create Opposite False Action option, shown in Figure 7-7, is only available as part of the simple wizard because each of the available actions has a reciprocal action.

Dynamic Actions have a lot of benefits; however, there are some situations where you may want to do things the old (manual) way, primarily for performance and control issues. For example, if you have a client-side action that needs to be performed as quickly as possible, then manually coding it using JavaScript may be a better approach. Dynamic Actions can add a small amount of overhead compared to certain manual techniques. In most situations this additional overhead is not significant.

Dynamic Actions in Detail

This section will cover all the options available for Dynamic Actions and will reference the Show Hide EmpNo Dynamic Action that was created in the previous section.

An easy way to understand Dynamic Actions is to break down their components into two main parts: drivers and actions. The driver determines what causes a Dynamic Action to run and when an action is triggered. The actions contain the code to perform the tasks. In the previous example the driver is when the P1_DEPTNO select list is changed. The actions are to either show or hide the P1_EMPNO select list. Figure 7-10 highlights the driver and actions concept as part of a flow chart. In Figure 7-10, "DA" stands for Dynamic Action.

Dynamic Action flow chart

Figure 7-10. Dynamic Action flow chart

When modifying a Dynamic Action, the When and Advanced sections allow you to define the driver. The True Actions and False Actions define the actions to perform based on a real-time condition.

To edit a Dynamic Action, double-click (or right-click and select the Edit option) the Dynamic Action name from the Page Rendering tree as shown in Figure 7-9. Alternatively, you can expand the P1_DEPTNO item and double-click the Show Hide EmpNo Dynamic Action as shown in Figure 7-11. When a Dynamic Action is linked to a region or an item it will also appear in the region's or item's tree entry on the page edit screen.

Dynamic action under page item

Figure 7-11. Dynamic action under page item

Editing a Dynamic Action will bring you to a new page where you can modify the itsoptions. The following section will cover each of these options, except for the standard sections (Condition, Authorization, Configuration, Comments) that are found in most APEX objects.

Identification

The Identification section, as shown in Figure 7-12, is similar to other objects in APEX. You can define a name and a sequence. Although the name will not be displayed to the end user, a meaningful name is useful for other developers modifying your application.

Edit Dynamic Action: Identification

Figure 7-12. Edit Dynamic Action: Identification

The sequence number determines the order in which Dynamic Actions are performed. If an action is set to fire on page load the sequence number will determine the order of the actions, regardless of their triggering event, during the page load. After the page is loaded the sequence is only relevant if you have multiple Dynamic Actions that are executed by the same event.

When

APEX Dynamic Actions bind events to object(s) that you specify in the When section. When the defined event occurs on the object(s) APEX will run the appropriate actions. The When section for the Show Hide EmpNo Dynamic Action is shown in Figure 7-13.

Edit Dynamic Action: When

Figure 7-13. Edit Dynamic Action: When

There are three main components to the When section: Event, Selection Type, and Condition. If you click each of these labels, you'll get some excellent documentation about each of these components.

Event: Dynamic Actions are triggered by browser-based events. These events are invoked by user actions or by JavaScript. The event determines which event will trigger the Dynamic Action to run. In the example, you wanted the Dynamic Action to run when the user changed the value for P1_DEPTNO, so the Change event was selected.

The events are grouped into three main categories: Browser Events, Framework Events, and Component Events. Browser events are standard HTML events. (For more information about HTML event types please refer to the jQuery documentation on events: http://api.jquery.com/category/events.) Framework events are triggered by APEX specific events. For example, the After Refresh event is triggered after a report region is refreshed as part of report pagination. Component events are triggered by specific objects within APEX or by plug-ins.

Selection Type: The event option will determine what events APEX needs to "listen" for. The Selection Type and Selection determines the object(s) to listen on. In the example, Item(s) was selected. This means that APEX will wait for a change to occur on P1_DEPTNO. Once a user changes the P1_DEPTNO select list, the Show Hide EmpNo Dynamic Action will be triggered. There are four different selection types to choose from: Item(s), Region, DOM Object, and jQuery Selector.

If the selection type is set to Item(s), you need to specify a comma delimited list of items in the Item(s) field. APEX will listen to all the specified items and apply the same action(s), regardless of which item was selected. When the Dynamic Action is triggered, APEX passes the specific item that triggered it as part of a JavaScript object (more on this later).

The Region selection allows you to select a single region for APEX to listen on. A good example of this is if you wanted to display a custom wait message each time an Interactive Report is refreshed. In this case, you'd set the event to Before Refresh and set the selection type to Region and select the region that contains your Interactive Report.

When using a region as the selection type, it is important to ensure that the region's template contains an ID. If the region's template is set to No Template, APEX will not be able to register its listener on the region since the region does not contain an ID. For more information read the following blog post: http://www.talkapex.com/2011/01/missing-id-in-no-template-region.html

Instead of listening on APEX specific objects such as item(s) or a region, you can also specify a DOM object or a jQuery Selector. In most cases it will be easier to use the jQuery selector rather than referencing the DOM object. This is especially true if you need to select multiple elements with a common attribute. For example, if you wanted APEX to perform a Dynamic Action each time someone moved their mouse over a HTML element with a class of highlight-me you would select jQuery Selector and enter .highlight-me as shown in Figure 7-14. The jQuery Selector option uses jQuery notation. Detailed information about the jQuery selector notation can be found on the jQuery site: http://api.jquery.com/category/selectors.

When Selection Type: jQuery Selector

Figure 7-14. When Selection Type: jQuery Selector

Condition: Dynamic Actions allow you to perform different actions based on a condition. The condition is evaluated in real time against the triggering element each time the event occurs. This condition is not the same as a standard APEX object condition. If the condition is true or not defined, then the True action(s) are executed. If the condition is false, the False action(s) are performed.

In the example the condition (previously shown in Figure 7-13) is set to is not null. This means that each time the triggering element P1_DEPTNO is changed, APEX will evaluate it to see if it is null or not. If it is null then it will hide the list of employees (false action). If a department is selected, it will show the list of employees (true action).

A list of conditions is shown in Figure 7-15. All conditions, except for is not null and is null, require that you enter some additional information in the Value field.

List of conditions

Figure 7-15. List of conditions

The value required for the first six comparison conditions is a static value. You can reference an item value but you must use the substitution string &PX_ITEM_NAME. notation. If you use a substitution string it will use the value of the item when the page is loaded which may not be the same value that is currently on the page when the condition is evaluated.

The two list conditions compare static comma delimited lists. If referencing a substitution string, the value of the item at the time the page is loaded will be used.

The last condition, JavaScript expression, allows you to compare values in real time when the condition is evaluated. All the other conditions are evaluated against the triggering element. If you use a JavaScript expression, you can compare any set of values. In most cases part of the expression will include the triggering element.

The JavaScript expression condition type contains several objects. They are

  • this.triggeringElement: The DOM object that triggered the Dynamic Action. this.triggeringElement.value will give you the value of the triggering element. If you defined multiple objects in the Selection Type this will let you know which one was triggered.

  • this.browserEvent: The event object that caused the Dynamic Action to run. this.browserEvent.type will give you a string value of the type of event that occurred. If this was used in the example, this.browserEvent.type would have been "change."

  • this.data: Additional data that can be passed from the event. In most cases this value will be null. Dynamic Action plug-ins may populate this field to pass additional data.

If you want to quickly see all the additional information that is available in each of the attributes listed above set the condition to JavaScript expression and in the Value field enter console.log(this);, as shown in Figure 7-16. Refresh the page and trigger the event. If you look at the console you will see the output of the "this" object as shown in Figure 7-17.

Configure condition to display this object

Figure 7-16. Configure condition to display this object

Console output of condition this object

Figure 7-17. Console output of condition this object

Note

Console is available in most browsers (except for Internet Explorer (IE) 8 and earlier). The following list describes how to view the console output in each of the major browsers:

  • Firefox: Install FireBug (http://getfirebug.com) - F12

  • Google Chrome: Ctrl+Shift+J

  • Safari: Ctrl+Alt+C

  • Internet Explorer 9: F12

To demonstrate a condition which uses a JavaScript expression, add an additional Number Field item on Page 1 called P1_X. Change the condition in the Show Hide EmpNo Dynamic Action from is not null to JavaScript Expression. In the Value text area enter: this.triggeringElement.value > $('#P1_X').val() as shown in Figure 7-18. Refresh the page and enter 20 in the P1_X field. When you change the department the list of employees will only appear when you select Operations (40) and Sales (30). You'll notice that if you change the value of P1_X the condition will evaluate accordingly without having to submit the page or set the value in session state.

JavaScript condition

Figure 7-18. JavaScript condition

Advanced

The When section defines the events and objects that APEX should listen to. The Advanced section defines how the events are bound to the objects. There are three different ways to attach an event to an object, as shown in Figure 7-19.

Event Scopes

Figure 7-19. Event Scopes

  • Bind: The default method that APEX attaches events to objects with is the bind method. Bind means that the Dynamic Action will be run each time the event occurs on the object. If the object is replaced, the event is no longer attached to the object (it's considered a new object) and the Dynamic Action will not be triggered.

  • Live: Live is very similar to the bind option except that the event will be attached to the object for the lifetime of the page or any new objects of that type. If a Dynamic Action is attached to a row in an Interactive Report then this option will ensure that the Dynamic Action will be triggered each time it is refreshed.

  • Once: If you want a Dynamic Action to fire only once, select this option. For example, you would use this option if you wanted to display a warning message the first time the user selected a department. Otherwise, it may get annoying to the user to consistently get the same warning message. If the action Dynamic Action is set to trigger on page load and is executed during the page load it will still run one more time when its event is triggered.

For more information about attaching events, please refer to the following documentation: http://api.jquery.com/category/events/event-handler-attachment. The jQuery API contains additional event handler attachment options that are currently not available in APEX.

Actions

The True and False Actions sections, as shown in Figure 7-20, contain action(s) to perform based on the Dynamic Action's condition. If multiple actions exist in each section they will be synchronously executed in order, determined by their sequence number.

True and False Actions

Figure 7-20. True and False Actions

At the beginning of this chapter you created the Show Hide EmpNo Dynamic Action using the Standard wizard. The True/False Actions steps allowed you to define a true and false action (see Figure 7-7). One of the available options was to Create Opposite False Action. This meant that it created both the true action (show) and the false action (hide).

To help describe the options available in the Edit Action page, edit the true (Show) action by clicking its Edit icon, which looks like a pencil, as shown in Figure 7-20. You can also modify the Show action by double-clicking the Show action in the tree from the Page Edit screen as shown in Figure 7-21. The Edit Action page is shown in Figure 7-22. The following subsections will describe all the options available when modifying an action.

Edit Show Action

Figure 7-21. Edit Show Action

Create/Edit Action page

Figure 7-22. Create/Edit Action page

Identification

The Identification section allows you to define the sequence and the action. The sequence affects the order, within the scope of the Dynamic Action, that the actions will run in.

The options in the actions select list are broken up into seven categories. The categories have no impact on the system and are there to help organize the list of available actions. This list also includes plug-in Dynamic Actions which are suffixed with "[Plug-in]" as shown in Figure 7-23. Clicking the Action label will display a pop-up window with a brief description for each of the built-in actions. Some actions require additional configuration, which is covered in the Settings and Affected Elements sections.

Action select list

Figure 7-23. Action select list

Execution Options

The Execution Options section determines when to run the action based on the Dynamic Action's condition. An action can either be run when the condition is true or false. Setting the value to True or False will determine which section the action is listed in Figure 7-20.

You can choose whether or not to fire the action once the page is loaded. In order for the option to be run on page load this option needs to be selected and the Dynamic Action's condition result must match the True/False setting. For example, both of the actions in the Show Hide EmpNo Dynamic Action are set to fire on page load. Only one of these will actually be run during the page load based on the value of P1_DEPTNO.Some actions, such as Execute PL/SQL Code and Set Value, allow you to select a third option: Stop Execution On Error, as shown in Figure 7-24. If the action causes an error and this option is set, any other actions that are part of the same Dynamic Action will not run.

Stop Execution On Error

Figure 7-24. Stop Execution On Error

Settings

The Settings section is only available for certain actions. Each Settings section is different depending on the action selected. In the example, the only available option is to Show all page items on the same line. This section will cover the settings options for two different action types: Execute JavaScript Code and Execute PL/SQL Code.

Execute JavaScript Code: When the action is set to Execute JavaScript Code the Settings section contains a text box where you can enter some JavaScript code to be run. To help, APEX provides the this object which contains five different elements. this.triggeringElement, this.browserEvent, and this.data were already covered in the Dynamic Actions Condition section.

  • this.action contains information about the action along with some additional information such as action attributes. Action attributes are useful in plug-in development. Please refer to Chapter 11 for more information about plugins.

  • this.affectedElements is a jQuery object array of all the elements that should be affected as part of this action. If your JavaScript code modifies anything on the page you should reference this object to find out which elements to modify. The affected elements are defined in the Affected Elements section on the Edit Action page which will be covered in the next subsection.

You can get an overview of these objects by clicking on the Code label. To explore all the options available enter console.log(this); in the Code section, refresh your page, trigger the Dynamic Action, then look at the console window.

Execute PL/SQL Code: When the action is set to Execute PL/SQL Code, APEX will send an AJAX request to the database to execute the block of PL/SQL code. You can reference page and application items using bind variables in this block of code. It's important to remember that the page and application items are the values in session memory, which may not be the same as the values currently displayed on the page.

To submit page items as part of the AJAX request, enter them as a comma-delimited list in the Page Items to Submit field. If any of the items submitted as part of the action have session state protection enabled, the action will fail since the item requires a checksum which is not provided in AJAX requests. This is done for security reasons to prevent malicious users from tampering with data.

Affected Elements

The Affected Elements section allows you to define which elements on the page are impacted by the Dynamic Action. In the example, the affected element type was Item(s) on P1_EMPNO since it needed to be shown and hidden. The different types of affected elements are

  • Item(s): Comma delimited list of items that will be affected by the action.

  • Region: Select a region from the drop down list. The list of regions will include both the current page regions and regions on Page Zero.

  • DOM Object: DOM object or ID of element on page.

  • jQuery Selector: jQuery selector which can select multiple elements on the page. Read http://api.jquery.com/category/selectors for more information and examples.

  • Triggering Element: The triggering element is the element that was defined in the When section for the Dynamic Action. In the example it is P1_DEPTNO.

  • Event Source: The event source is the specific element that triggered the Dynamic Action to fire. This may be the triggering element or a child of the triggering element. For example suppose that you created a Dynamic Action for a click event on the Select Employee region. If I click on a "blank" part of the region the event source will be the region itself, which is the same as the triggering element. If I click on one of the select lists the event source will be the select list (P1_EMPNO or P1_DEPTNO) since that is the element that caused the Dynamic Action to run.

Not all actions require or allow for affected elements to be defined. For example it doesn't make sense for the Alert action type, which triggers an alert popup, to have any affected elements.

Dynamic Actions in Action

The example that was used in the previous sections was a very basic Show/Hide Dynamic Action. This section will cover how to create more complex Dynamic Actions. All the available actions won't be covered in this chapter however this example will make you familiar with some of them and how they can interact with one another. This example will also use plug-in Dynamic Actions. A final copy of this example is included in the book's files as an application. The file is 07_example-2_finished_application.sql.

Note

The following example will leverage some features only available in recent web browsers. If you are using Internet Explorer, please ensure that it is version 8 or above.

Business Case

After showing users the previous example, they requested some modifications to the department/employee behavior. Instead of selecting an employee from a select list they would like to select an employee from a report. The full list of requirements is

  • When a user selects the department, a modal window is displayed with a report listing all the employees in the selected department. The report will include the employee's name, job, hire date, and salary.

  • When the user hovers over a report row, the row will be highlighted.

  • If the user clicks on a row in the report, the employee's number is stored in a hidden field and the employee name is displayed beside the Employee label.

  • Right after an employee is selected, the name should be bolded for a few seconds to emphasize that it has been changed.

Using Dynamic Actions, you'll be able to implement all these requirements. In order to simplify the solution, it has been broken it up into small sections.

Setup

Before you start working on this example you'll need to modify a few things from the first example. You will find an application that was configured with the following modifications in the Source Code/Download area of the Apress web site at www.apress.com. You can skip this section if you import the application 07_example-2_base_application.sql.

  1. Delete the Show Hide EmpNo dynamic action that was previously created. To delete the Dynamic Action, edit it and click the Delete button.

  2. Change the P1_EMPNO Read Only attribute to Always as shown in Figure 7-25. This is a trick to quickly have an item's display value shown and its actual value hidden without having to create two separate page items.

    Read Only value

    Figure 7-25. Read Only value

    Note

    Creating a select list item as Read Only will create two HTML elements on the page. The first is a hidden element which stores the value of the item. This element's id is the same as the page item's name—for example, P1_EMPNO. APEX will also create a second element which has the display value of the item. This second element has an id of PX_ITEMNAME_DISPLAY. In the example this is P1_EMPNO_DISPLAY.

  3. Ensure that your application is using Theme 1. To view your application's current theme, edit the application properties by clicking the Edit Applications button on the main application builder page as shown in Figure 7-26. Scroll down to the Theme section to view the current theme. If it is not set to Theme 1 then go to Shared Components Themes to change the current theme.

Application Builder main page

Figure 7-26. Application Builder main page

If you now run the application it should look like Figure 7-27.

Initial setup

Figure 7-27. Initial setup

Create Department Employee Report

To meet the first requirement, you'll need to create a report region which lists the employees in the selected department. To create this report:

  1. Create a standard report region called Department Employees using the following query:

    select e.empno,
           initcap(e.ename) ename,
           initcap(e.job) job,
           e.hiredate,
           e.sal
    from emp e
    where e.deptno = :p1_deptno
  2. Modify the region's attributes and set the static ID to DEPT_EMP_REPORT as shown in Figure 7-28. The static ID will be used as part of a CSS selector later on.

    Set Region Static ID

    Figure 7-28. Set Region Static ID

  3. Modify the report column attributes so that EMPNO is not displayed and the column headings are as shown in Figure 7-29.

    Report Column Attributes

    Figure 7-29. Report Column Attributes

  4. Some additional attributes need to be added to the ENAME column which will be used to send data to P1_EMPNO when the user clicks on a row in the report. To edit the ENAME column, click on the edit link beside ENAME in Figure 7-29. In the Column Formatting section enter <span data-empno="#EMPNO#" data-ename="#ENAME#">#ENAME#</span> in the HTML Expression area as shown in Figure 7-30. The HTML Expression area allows you to customize the appearance of each column.

Column Formatting/HTML Expression

Figure 7-30. Column Formatting/HTML Expression

Note

The two custom attributes, data-empno and data-ename, are HTML 5 compliant custom attributes. In HTML 4 custom attributes are not recommended. HTML 5 supports custom attributes by prefixing the attribute name with "data-". The following blog contains a brief overview of HTML 5 custom data attributes: http://ejohn.org/blog/html-5-data-attributes.

If you refresh Page 1 it will look like Figure 7-31. If you change the department, nothing happens, since you haven't applied any Dynamic Actions.

Department Employees report added

Figure 7-31. Department Employees report added

Refresh Department Employees Report

The first Dynamic Action to create is to refresh the Department Employees report when the P1_DEPTNO select list changes. The following steps will create the Dynamic Action when the department is changed:

  1. Right-click on the Dynamic Actions tree element, on the Page 1 edit page, and select Create from the context menu.

  2. Select Advanced on the first screen in the wizard and click the Next button.

  3. In the Identification section, enter Change DeptNo in the name field and 10 in the sequence field. Click the Next button.

    The driver for this Dynamic Action is when P1_DEPTNO changes. Configure the When section to be the same as shown in Figure 7-32. Click the Next button to continue.

    Create Change DeptNo: When

    Figure 7-32. Create Change DeptNo: When

  4. In the next screen, the True Action, set the Action as Refresh and make sure that Fire on Page Load is unchecked. Click the Next button.

  5. On the optional False Action page leave the Action as No False Action and click the Next button which will bring you to the Affected Elements page.

  6. The affected element(s) will determine which element(s) will be refreshed. Select Region as the Selection Type and then Department Employees as the Region. Click the Finish button to complete the wizard.

Reload the page and change the department. It appears as though nothing has happened. In Firefox, if you look at the console, as shown in Figure 7-33, an AJAX request was made, however no data was found.

AJAX request on department change

Figure 7-33. AJAX request on department change

If you reexamine the query you'll notice that the predicate, where e.deptno = :p1_deptno, references P1_DEPTNO. In order for the report to reflect the new value you'll need to create a Dynamic Action to set P1_DEPTNO's session value before the report is refreshed. To set the value in session state:

  1. Edit the Change DeptNo Dynamic Action.

  2. Add a true action by clicking the Add True Action button.

  3. Set the values as shown in Figure 7-34. The sequence number is set to 1 so that it will be triggered before the report is refreshed. The action type is Execute PL/SQL Code since it's the easiest way to set a value in session state. Click the Create button to create the true action.

Set P1_DEPTNO in session state

Figure 7-34. Set P1_DEPTNO in session state

If you refresh the page and change the department the report should be updated. Figure 7-35 shows the page when Accounting is selected as the department.

Department Employees report updated by Dynamic Action

Figure 7-35. Department Employees report updated by Dynamic Action

Note

The method that was used to set P1_DEPTNO involves an additional AJAX request to the server. In future versions of APEX you should have the ability to define page items to submit as part of the Refresh Dynamic Action. This will improve response time since only one AJAX request is required to set a value in session state and refresh the report.

Highlight Row

Now that the Department Employees report refreshes when the department is changed, the next step is to add the ability to highlight each row when a user hovers over it. If you inspect the HTML for the table that displays the employees, you'll notice that each row has a class of highlight-row as shown in Figure 7-36.

Table HTML

Figure 7-36. Table HTML

The highlight-row class can be used to add a CSS style to highlight the row when you hover over it. The CSS style is

/* Using the DEPT_EMP_REPORT id ensures that this highlighting will only affect the
Table HTML
Department Employees report */ #DEPT_EMP_REPORT .highlight-row:hover > td { background-color: yellow; }

Normally you would include this in a CSS file and store it on a web server which would be referenced in the HTML header. For the purpose of this example, inline CSS will be "injected" into the application using a Dynamic Action.

  1. Create a new Dynamic Action, using the advanced wizard, called On Page Load. This dynamic action will be used for actions that need to only be executed when the page loads.

  2. Set the event to Page Load with no condition and click the Next button to continue.

  3. Use Execute JavaScript Code and enter the code below into the Code section. This code will put the inline style sheet after the affected element defined in the next step.

    //This code will prepent the affected element(s) with the inline CSS HTML code.
    $(this.affectedElements).prepend(
      '<style type="text/css"><!-- #DEPT_EMP_REPORT .highlight-row:hover > td
    Table HTML
    {background-color: yellow;}//--></style>' );
  4. You can define where the inline CSS will get prepended to in the affected elements section. Select Region as the selection type and Department Employees as the region. This means that APEX will insert the above HTML code before the Department Employees region. Click the Create button.

If you refresh the page and hover over each row you should notice that the current row is highlighted yellow. You can change the color by modifying the value in the style tag that was added.

Row Click

The next Dynamic Action will handle what happens when a row is clicked. According to the requirements, when a row is clicked the employee number should be set in a hidden field (P1_EMPNO) and the employee name should be displayed.

To meet this requirement, the additional custom data attributes that were added to the ENAME column in the report will be leveraged. If you inspect the HTML of an employee name in the report column, you'll notice the additional attributes, data-ename and data-empno, as shown in Figure 7-37.

HTML 5 Custom Data Attributes

Figure 7-37. HTML 5 Custom Data Attributes

The following steps will create the Dynamic Action which will handle the row click event:

  1. Create a Dynamic Action, using the Advanced wizard, and call it Row Click.

  2. The event needs to fire when the user clicks on a row inside the Department Employees region. The following configuration for the When section will handle this. The JavaScript condition ensures that the click happened in a row within the region.

    • Event: Click

    • Selection Type: Region

    • Region: Department Employees

    • Condition: JavaScript Expression

    • Value: $(this.browserEvent.target).closest('tr .highlight-row',•

    this.browserEvent.currentTarget).length > 0
  3. Set the Action to Execute JavaScript and uncheck Fire On Page Load. Enter the following in the Code section:

    //dataSpan will represent the span tag that was created earlier that contains the custom
    HTML 5 Custom Data Attributes
    data attributes var dataSpan = $(this.browserEvent.target).closest('tr').find('[data-empno]'), //Set the EMPNO and its display values using the data attributes $('#P1_EMPNO').val(dataSpan.attr('data-empno')); $('#P1_EMPNO_DISPLAY').html(dataSpan.attr('data-ename'));
  4. There is no need to define a false action so leave it empty.

  5. There's no need to define any affected elements since the elements that need to be modified were hard coded in the JavaScript code. Click Create to finish the wizard.

If you refresh the page and click on a row in the Department Employees region the associated employee's name should be displayed beside the Employee label as shown in Figure 7-38.

Select Employee—Row Click

Figure 7-38. Select Employee—Row Click

Emphasize Employee Change

The displayed employee name needs to be emphasized after the employee has been selected. To emphasize the name, immediately hide the employee name, bold it, and then have it fade in. Once the fade in is complete, remove the bold font setting.

To emphasize the employee name, one Dynamic Action will be needed with multiple actions. There are various ways to do this, but the goal of this example is to demonstrate how multiple actions within a Dynamic Action work.

The following steps will create the appropriate Dynamic Action along with the additional true actions:

  1. Create a new Dynamic Action, using the Advanced wizard, called FadeIn Employee Name.

  2. Use the following values for the When section:

    • Event: Change

    • Selection Type: jQuery Selector

    • jQuery Selector: #P1_EMPNO_DISPLAY

    • Condition: No Condition

  3. Select Hide for the true action and uncheck the Fire on Page Load check box. Ensure that the Hide all page items on the same line select list is set to No. This means that only the value will be hidden and not the label.

  4. Select Triggering Element as the affected element. This means that the element (P1_EMPNO_DISPLAY) that was defined in the When section will be hidden.

  5. Add another true action. This action will be used to set the style of employee display name to bold. Use the following configuration for this action:

    • Identification

      • Sequence: 20

      • Action: Set Style

    • Execution Options

      • Fire When Event Result: True

      • Fire On Page Load: uncheck

    • Settings

      • Style Name: font-weight

      • Value: bold

    • Affected Elements

      • Selection Type: Triggering Element

  6. The last true action that is required is to fade in the employee name and then unbold it. The jQuery fadeIn function will be used to do this. Create a true action as follows:

    • Identification

      • Sequence: 30

      • Action: Execute JavaScript Code

    • Execution Options

      • Fire When Event Result: True

      • Fire On Page Load: uncheck

    • Settings

      • Code:

        $(this.affectedElements).fadeIn(2000, function(){
          //The second parameter in the fadeIn function allows you to define a
          //function to be run once the fadeIn is completed.
          //This function  will be used to remove the bold style
          $(this).css('font-weight',''),
        });
    • Affected Elements

      • Selection Type: Triggering Element

Refresh Page 1 and click on an employee in the Department Employees report; the employee name should appear in the Employee area. Once the name appears nothing happens to it. This is due to how the change event is triggered when called from JavaScript.

The change event is triggered when a user modifies a value from the browser. If the value is modified by JavaScript the change event is not fired. To resolve this issue you'll need to manually trigger a change event when P1_EMP_DISPLAY is modified. An event can easily be triggered using the jQuery trigger() function.

To trigger the change event on P1_EMP_DISPLAY modify the Row Click Dynamic Action and edit the Execute JavaScript true action. Change the last line in the code area from this:

$('#P1_EMPNO_DISPLAY').html(dataSpan.attr('data-ename'));

to this:

$('#P1_EMPNO_DISPLAY').html(dataSpan.attr('data-ename')).trigger('change'),

Apply the changes and refresh the run page. The employee name should be bolded and fade in for 2 seconds after a row is clicked. Once the fade in finishes the employee name is no longer bolded.

Note

The only modification that was made to the last line of code was to add .trigger('change') One of the great things about jQuery is its ability to "chain" functions. Each function in jQuery returns itself so you can easily append another function to it. To learn more about jQuery chaining search online for "jquery chaining".

Modal Window

So far all the requirements have been met except for the modal window. Because it is easier to develop, debug, and demonstrate our example without the extra complexities of a modal window, this was saved for last. To implement the modal window a free Dynamic Action plug-in from http://apex-plugin.com will be used.

Note

http://apex-plugin.com is a site where developers post plug-ins for the rest of the community. It is not maintained by Oracle so it's recommended that you review each plug-in before installing it for your organization.

The plug-in that will be used for the modal window is called ClariFit Simple Modal. This plug-in can convert a region into a modal window. The following steps cover how to download and install the plug-in.

  1. Go to http://www.apex-plugin.com/oracle-apex-plugins/dynamic-action-plugin/clarifit-simple-modal_56.html and download the zip file. The download link is on the bottom-right part of the page.

  2. Unzip clarifit_simple_modal.zip. The zip file contains two Dynamic Actions. One is to show a modal region and the other is to hide the modal region.

  3. To import the plug-in go to Shared Components Plug-ins. Click the Import button as shown in Figure 7-39.

    Import Plug-in

    Figure 7-39. Import Plug-in

  4. Select dynamic_action_plugin_com_clarifit_apexplugin_simple_modal_close.sql from the folder that you unzipped the downloaded file in. Ensure that the File Type is Plug-in and click the Next button.

  5. On the File Import Confirmation screen click the Next button to go to the final step.

  6. Select the application and click the Install Plug-in button as shown in Figure 7-40.

    Install Plug-in

    Figure 7-40. Install Plug-in

  7. After installing a plug-in you will be directed to the Edit page. Click the Apply Changes button to finish.

  8. Repeat the previous steps to install the ClariFit Simple Modal - Show plug-in using dynamic_action_plugin_com_clarifit_apexplugin_simple_modal_show.sql.

Now that the Dynamic Action plug-ins are installed, you can create the appropriate actions to display the Department Employees region in a modal window when P1_DEPTNO is changed. First, the Department Employees region should be hidden when the page is loaded since it should only appear when the department is changed. To hide the Department Employees region, add another true action as shown in Figure 7-41, to the On Page Load Dynamic Action which is fired when the page is loaded. Once this action is added refresh the page. The Department Employees region should be hidden.

Hide Department Employees Region on Page Load

Figure 7-41. Hide Department Employees Region on Page Load

The next thing to do is to add an action to display the Department Employees region in the modal window. Since the Change DeptNo Dynamic Action already exists you can add another true action to display the region. Add a new true action to Change DeptNo and configure it as shown in Figure 7-42. The selected action, ClariFit Simple Modal - Show, is the dynamic action plug-in that was imported.

Create show modal window action

Figure 7-42. Create show modal window action

Note

Dynamic Action plug-ins are suffixed with "[Plug-in]" so that they can easily be identified as plug-ins.

If you refresh the page and change a department, the Department Employees region should appear as a modal window. When you select an employee, the employee name gets updated but the Department Employees region still remains as a modal window, as shown in Figure 7-43.

Modal window show

Figure 7-43. Modal window show

The last action to add will close the modal window when the user clicks on a row (i.e., selects an employee). Add a true action to the Row Click Dynamic Action and configure it as shown in Figure 7-44. If you refresh the page it should now meet all the requirements that were outlined at the beginning of this example.

Create close modal window action

Figure 7-44. Create close modal window action

Summary

This chapter covered all aspects of Dynamic Actions: how to create a Dynamic Action and all the options available when configuring a Dynamic Action. The last section demonstrated several kinds of Dynamic Actions and how they can relate with one another. It also showed how to download and import Dynamic Action plug-ins.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset