17.9 Implementing Dynamic Actions

This section provides an overview of dynamic actions and explains how to create and modify them.

Topics:

17.9.1 Understanding Dynamic Actions

Dynamic actions provide a way to define complex client-side behavior declaratively without the need for JavaScript. Using the Dynamic Action Create wizard, you specify an action that is performed when a defined set of conditions occur. You can also specify which elements are affected by the action, and when and how they are affected.

When working with dynamic actions, you should be mindful of the fact that the more dynamic actions you add to a page, the greater your overall page size. This is because the dynamic action framework emits additional code to the client for each dynamic action defined, which then also must be downloaded and executed by the framework in the client. See "Debugging Dynamic Actions" for information on how to debug problems.

The process of implementing a dynamic action involves the following:

  1. Edit or create an item, button, region, DOM object, or jQuery selector on a page. This component is referenced within the dynamic action in defining when it fires.

  2. Create a dynamic action from the application page that invokes the action.

  3. Run your application to test the dynamic action.

For Dynamic Action implementation examples, go to the Learning Library at the following location, click the All Content tab and enter search criteria for Application Express (APEX) Product OBEs:

http://www.oracle.com/technetwork/tutorials/index.html

17.9.2 Creating a Dynamic Action

Creating a Dynamic Action involves specifying when the action happens (with optional conditions), what action or actions are performed, and what elements are affected by the action.

  1. Navigate to the appropriate Page Definition. See "Accessing the Page Definition".

  2. Under Page Rendering, scroll down to Dynamic Actions and click the Create icon.

    The Dynamic Action wizard appears.

  3. For Identification, enter the following and click Next.

    • Name - Enter the name of the dynamic action.

    • Sequence - Enter the sequence of this component. The sequence determines the order of execution.

  4. For When, specify when the action is performed and click Next. Available options include:

    1. Event - Dynamic actions can be defined to fire based on events that happen on the page. There are four different categories of events that can be utilized, Browser events, Framework events, Component events and Custom events. Here are the details of all the supported events, including the internal JavaScript event name in brackets.

      Browser Events

      Note:

      The events displayed differ according to the page's current User Interface type. If you want to select an event that corresponds to a different type, then you have the option of selecting Show unsupported, which displays all events including those that do not correspond to the current type.
      • Change (change) - Fires when a control loses the input focus and its value has been modified since gaining focus. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Click (click) - Fires when the pointing device button is clicked over the triggering element. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Double Click (dblclick) - Fires when the pointing device button is double clicked over the triggering element. Supported User Interface Types: Desktop.

      • Get Focus (focusing) - Fires when the triggering element receives focus by either a pointing device or by tabbing into the element. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Key Down (keydown) - Fires when a key on the keyboard is pressed. Use this event when you want to capture special keystrokes such as arrow keys, after a key has been pressed. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Key Press (keypress) - Fires when a key on the keyboard is pressed resulting in text being entered. Use this event when you want to capture actual text entry. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Key Release (keyup) - Fires when a key on the keyboard is released. Use this event when you want to capture special keystrokes such as arrow keys, after a key has been released. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Lose Focus (focusout) - Fires when the triggering element loses focus either by the pointing device or by tabbing out of the element. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Mouse Button Press (mousedown) - Fires when the pointing device button is pressed over the triggering element. Supported User Interface Types: Desktop.

      • Mouse Button Release (mouseup) - Fires when the pointing device button is released over the triggering element. Supported User Interface Types: Desktop.

      • Mouse Enter (mouseenter) - Fires once when the pointing device is moved into the triggering element. Supported User Interface Types: Desktop.

      • Mouse Leave (mouseleave) - Fires once when the pointing device is moved away from the triggering element. Supported User Interface Types: Desktop.

      • Mouse Move (mousemove) - Fires when the pointing device is moved while it is over the triggering element. Supported User Interface Types: Desktop.

      • Orientation Change (orientationchange) - Fires when a device's orientation changes (by turning it horizontally or vertically). The 'this.browserEvent' JavaScript object contains an 'orientation' property that will equal either 'portrait' or 'landscape'. Supported User Interface Type: 'jQuery Mobile Smartphone'.

      • Page Load (Desktop User Interface uses ready, jQuery Mobile Smartphone User Interface uses pageint) – Fires when the page loads. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Page Unload (unload) - Fires when a page is unloaded. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Resize (resize) - Fires when the browser window is resized. Supported User Interface Types: Desktop.

      • Resource Load (load) - When the triggering element is the window element (using a DOM Object value of window in the When attributes), the event fires when the browser finishes loading all content within a document, including window, frames, objects and images. For other elements, it fires when the target element and all of its content has finished loading. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Scroll (scroll) - Fires when a scrollable triggering element is scrolled. This could be the browser window (using a DOM Object value of window in the When attributes), scrollable frames or elements with the overflow CSS property set to scroll (or auto when the element's explicit height is less than the height of its contents). Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Scroll Start (scrollstart) - Fires when a scroll begins. Supported User Interface Type: jQuery Mobile Smartphone.

      • Scroll Stop (scrollstop) - Fires when a scroll ends. Supported User Interface Type: jQuery Mobile Smartphone.

      • Select (select) - Fires when a user selects some text in a text field. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Swipe (swipe) - Fires when a horizontal drag of 30px or more (and less than 75px vertically) occurs, within a 1 second duration. Supported User Interface Type: jQuery Mobile Smartphone.

      • Swipe Left (swipeleft) - Fires when a swipe event occurs moving in the left direction. Supported User. Interface Type: jQuery Mobile Smartphone.

      • Swipe Right (swiperight) - Fires when a swipe event occurs moving in the right direction. Supported User Interface Type: jQuery Mobile Smartphone.

      • Tap (tap) - Fires after a quick touch event. Supported User Interface Type: jQuery Mobile Smartphone.

      • Tap and Hold (taphold) - Fires after a held touch event (held for close to 1 second). Supported User Interface Type: jQuery Mobile Smartphone.

      • Touch Cancel (vmousecancel) - Normalized event for handling touch or mousecancel events. Supported User Interface Type: jQuery Mobile Smartphone.

      • Touch End (vmouseup) - Normalized event for handling touchend or mouseup events. Supported User Interface Type: jQuery Mobile Smartphone.

      • Touch Move (vmousemove) - Normalized event for handling touchmove or mousemove events. Supported User Interface Type: jQuery Mobile Smartphone.

      • Touch Start (vmousedown) - Normalized event for handling touchstart or mousedown events. Supported User Interface Type: jQuery Mobile Smartphone.

      • Virtual Click (vclick) - Normalized event for handling touchend or mouse click events. On touch devices, this event is dispatched after Touch End. Supported User Interface Type: jQuery Mobile Smartphone.

      Framework Events

      • After Refresh (apexafterrefresh) - Fires after the triggering element has been refreshed. The event is only valid for triggering elements that perform Partial Page Refresh and fire this event. The native components that support this are Interactive Reports, Classic Reports, Charts, List View and all item types with cascading LOV support. Plug-ins might support this event as well. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Before Page Submit (apexbeforepagesubmit) - Fires before a page being submitted. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Before Refresh (apexbeforerefresh) - Fires before the triggering element has been refreshed. The event is only valid for triggering elements that perform Partial Page Refresh and fire this event. The native components that support this are Interactive Reports, Classic Reports, Charts, List View and all item types with cascading LOV support. Plug-ins might support this event as well. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      Component Events

      These events are available when there is a component (either an item, region, or dynamic action) available to your application that triggers a custom event. These events appear in the following format Event name [Component Name], for example the Change Order event triggered by the Shuttle native item type appears as Change Order [Shuttle]. Component events are either triggered from native components shipped with Oracle Application Express, or from plug-in components you have installed into your application.

      • Events triggered by native components:

        Change Order [Shuttle] (shuttlechangeorder) – Fires when the order of a value in the right hand select list is changed (either using Move Top, Move Up, Move Down, or Move Bottom). There are currently no other events triggered by native components in Oracle Application Express. Supported User Interface Types: Desktop and jQuery Mobile Smartphone.

      • Events triggered by plug-in components:

        These will be available when added to your current application and will be in the format Event name [Component Name]. For help related to events raised by plug-ins, refer to Help text on the plug-in configuration page, by navigating to Shared Components, Plug-ins, plug-in name, Help Text, where the plug-in author may have included documentation. Supported User Interface Types: Determined by the plug-in's User Interfaces > Supported for settings.

      • Custom Event:

        By selecting Custom an additional field displays enabling you to define of a custom event. This is useful when the native or plug-in provided events are insufficient.

    2. Selection Type - Select the type of page element that triggers the dynamic action. A corresponding name field displays to specify the name of the page element.

      Note:

      Only available if the event selected supports definition of a page element. Selecting any of the following events hides this field: Page Load, Page Unload, Resize, Before Page Submit, Orientation Change. All other event types show this field.
    3. Item(s), Button, Region, DOM Object, or jQuery Selector - Specify the name of the page element as follows:

      • Item(s) - Enter or select one or more page item names that trigger the dynamic action. For multiple items, separate page items with a comma.

      • Button - Select the button that triggers the dynamic action. If the button is a Template button, the button selected must use a button template that includes an ID attribute set to #BUTTON_ID#. The dynamic action framework relies on this ID value to reference the button.

      • Region - Select the region name that triggers the dynamic action. The region selected must use a region template that includes a container element with an ID attribute set to #REGION_STATIC_ID#. The dynamic action framework relies on this ID value to reference the region.

        The region can fire the event itself, for example, Before Refresh or After Refresh events, or can be a container to catch events that fire on items or other elements in the region. Only the following subset of events support this: Change (change), Click (click), Double Click (dblclick), Get Focus (focus), Key Down (keydown), Key Press, Key Release (keyup), Lose Focus (blur), Mouse Button Press (mousedown), Mouse Button Release (mouseup), Mouse Move (mousemove).

      • DOM Object - Enter either a Document Object Model (DOM) object constant (for example, document or window), or the ID of a DOM object that triggers the dynamic action.

      • jQuery Selector - Enter the jQuery selector syntax to return one or more page elements that trigger the dynamic action.

    4. Condition - To provide a condition, make a selection from the Condition list. Depending on the condition type, you may be required to enter a Value to test the condition against. If no condition is specified, only True Actions fire. If a condition is specified, the True Action fires when the condition is met, the False Action fires when it is not.

  5. Specify the action that is performed when the previously specified event occurs and conditions are satisfied and then click Next.

    • For True Actions - Depending on the selected action, additional options display. Use these options to specify additional settings and values required to perform the action.

      Component

      • Clear - Clears the affected elements.

      • Disable - Disables the affected elements. By disabling the affected elements, the page item will be non-editable and will not retain the item's value when the page is submitted.

      • Enable - Enables the affected elements.

      • Hide - Hides the affected elements. Also has the option to Hide all page items on the same line.

      • Refresh - Triggers a refresh of the affected elements. Note that not all elements support a refresh, you can use it for interactive reports, classic reports, charts, list view and all item types with cascading LOV support. Plug-ins might support this action as well.

      • Set Focus - Sets the focus to the affected elements. This defaults to the first of the affected elements if there are multiple. This can be especially useful when used with the Show and Enable actions to take the user straight to the appropriate item.

      • Set Value - Sets the value of the affected elements. This supports the following Set Types: Static Assignment, JavaScript Expression, SQL Statement, PL/SQL Expression, PL/SQL Function Body. SQL Statement Set Type supports setting between 1 and 100 page item values, by selecting between 1 and 100 columns in the query. All other Set Types only support setting 1 page item. See the Execute PL/SQL Code action for an alternative way to set multiple values on the page.

      • Show - Shows the affected elements. Also has the option to Show all the page items on the same line.

      Execute

      • Execute JavaScript Code - Enables you to define or call custom, page specific JavaScript code to use within the dynamic action framework. If you are defining JavaScript code that is specific to just one page, you can also make use of the new page level attribute Function and Global Variable Declaration to define this. Functions and variables defined here can subsequently be referenced from this action.

      • Execute PL/SQL Code - Executes some PL/SQL code on the server. If an error occurs during execution, the user is alerted. To specify page items that are set on the page when the call to the server returns, based on their values in session state, define Page Items to Return. When the Page Items to Return option is used, and you want to prevent subsequent Change based dynamic actions from firing for the Page Items to Return, set Suppress Change Event to Yes.

      Notification

      • Alert - Displays an Alert.

      • Confirm - Displays a confirmation dialog. If the user chooses Cancel then the proceeding actions are not executed and the current event is canceled.

      Style

      • Add Class - Adds 1 or more CSS classes to the affected elements.

      • Remove Class - Removes one, more or all classes from the affected elements.

      • Set Style - Sets any style, CSS, property to the affected elements.

      Miscellaneous

      • Cancel Event - Cancels subsequent dynamic actions or events from firing, based on certain conditions. This could be useful in the following situations:

        If you have multiple dynamic actions on a page that are based on the same event (such as Click) and they refer to the same element on the page, you can use the Cancel Event action to prevent subsequent dynamic actions from firing, conditionally based on its When Condition.

        If a dynamic action has many true actions, you can use Cancel Event to halt subsequent actions from being processed. Currently this has limited value as it is not possible to define a dynamic action's actions to fire conditionally, so this would just always prevent subsequent actions being processed, which could be useful in debugging.

        Used to cancel page submission, when used with the Before Page Submit event of dynamic actions. For example, suppose you define a dynamic action that fires on the Before Page Submit even. For the Condition, you define JavaScript Expression and in the Value enter an expression such as $v('P2_ENAME') == 'DO NOT SUBMIT'. Then, if you define a True Action that uses the Cancel Event action, the page is not submitted when the ENAME field is equal to DO NOT SUBMIT.

      • Submit Page - Submits the page. Also, has the option to disable the current page during page submission and display a processing style GIF.

      Plug-ins

      • my_plugin_action[Plug-in] - Depending on your application configuration, you may also have additional plug-in dynamic actions available here. These are displayed as my_plugin_action [Plug-in]. Plug-in dynamic actions are installed within the application's shared components. The plug-in developer must assign an appropriate category for the action (Component, Execute and so forth).

    • Fire On Page Load - Select this option to also trigger this action when the page loads. This may or may not be checked by default, depending on the type of Action selected.

    • Generate Opposite False Action - Select this option to also generate the opposite false action. Actions treated as opposite are Show / Hide, Enable / Disable and Add Class / Remove Class. This option only displays if one of the above actions are selected, and a When Condition has been specified.

  6. For Affected Elements (available only for actions that support affected elements), specify the elements affected by this dynamic action and how they are affected then click Next.

    The final step in creating the dynamic action is different depending on whether the action you have selected supports selection of affected elements. Some actions, such as Submit Page, do not require affected elements to be selected. Other actions, such as Show, do. If the action selected does not support the selection of affected elements, the last page will be a Confirm page where you can review details of the dynamic action. If the action selected does support selection of affected elements, options on this page include:

    • Selection Type - Select the type of page element that is affected by the dynamic action. Depending on the selected type, additional options display. Use these options to specify additional settings and values required to specify how the element is affected.

    • Item(s), Button, Region, DOM Object, or jQuery Selector - Specify the name of the page element as follows:

      • Item(s) - Select the item name that is affected by the dynamic action.

      • Button - Select the button that is affected by the dynamic action. If the button is a Template button, the button selected must use a button template that includes an ID attribute set to #BUTTON_ID#. The dynamic action framework relies on this ID value to reference the button.

      • Region - Enter or select the region name that is affected by the dynamic action. The region selected must use a region template that includes a container element with an ID attribute set to #REGION_STATIC_ID#. The dynamic action framework relies on this ID value to reference the region.

      • DOM Object - Enter either a Document Object Model (DOM) object constant (for example, document or window), or the ID of a DOM object that is affected by the dynamic action.

      • jQuery Selector - Enter the jQuery selector syntax to return one or more page elements that is affected by the dynamic action.

      • Triggering Element - Provides access to the page element defined in the 'When' of the dynamic action.

      • Event Source - Provides access to the page element where the event originated, which could be different from the Triggering Element in some cases. For example, if the When element is some container element (such as a region) that has been defined to handle events raised by its sub-elements (such as page items in that region), you can use the Event Source to easily act upon the sub-element that triggered the event.

  7. Click Create Dynamic Action.

17.9.3 Editing Dynamic Actions

Once you create a dynamic action, you can modify attributes defined during the creation process, specify attributes not available during the process (such as specifying an Authorization Scheme) and add additional true actions.

To edit a dynamic action:

  1. Navigate to the appropriate Page Definition. See "Accessing the Page Definition".

  2. Under Page Rendering, scroll down to Dynamic Actions and click the dynamic action you want to modify.

    The Edit Dynamic Action page appears.

  3. Make changes and click Apply.

17.9.4 Adding Additional True Actions

Dynamic actions contain a condition that can be specified to control when True actions fire. For example, consider an application that displays employee information to managers. If the employee is exempt, their salary displays, otherwise their hourly wage displays.

The Dynamic Action creation wizard allows only one true action to be specified. To add more than one action you must use the create wizard to create the dynamic action and then edit the action to add additional true actions.

To add an action:

  1. Navigate to the appropriate Page Definition. See "Accessing the Page Definition".

  2. Under Page Rendering, scroll down to Dynamic Actions and click the dynamic action you want to modify.

    The Edit Dynamic Action page appears.

  3. To add an action that executes when the conditions are met or when no condition is specified, scroll down to True Actions and click Add True Action.

    The Create/Edit Action page appears.

  4. Under Identification:

    1. Sequence - Enter the sequence for this component. This indicates the execution order.

    2. Action - Select the action that triggers when the condition is true.

  5. Under Execution Options:

    1. Fire on Page Load - Select if you want the action to also trigger when the page loads.

    2. Stop Execution on Error - Specify whether any proceeding actions should be executed, if an error occurs while executing the current action. Only available when the selected Action has been defined to expose this attribute, for example Execute PL/SQL Code.

    3. Wait for Result - Specify whether any proceeding actions should wait for the result of the current action (if they are dependent on the result), or whether they can just execute without waiting. Only available when the selected Action has been defined to expose this attribute, for example Execute PL/SQL Code.

  6. For Affected Elements (only available if the selected action supports definition of affected elements):

    Note:

    Some or all of these fields may not be available to select, depending on the type of action selected. For example the Disable action does not support selecting a Region as the affected element.
    1. Selection Type - Select the type of page element that is affected by the dynamic action. A corresponding name field displays to specify the name of the page element.

    2. Item(s), Button, Region, DOM Object, or jQuery Selector - Specify the name of the page element as follows:

      • Item(s) - Enter or select the item name that is affected by the dynamic action.

      • Button - Select the button that is affected by the dynamic action. If the button is a Template button, the button selected must use a button template that includes an ID attribute set to #BUTTON_ID#. The dynamic action framework relies on this ID value to reference the button

      • Region - Enter or select the region name that is affected by the dynamic action. The region selected must use a region template that includes a container element with an ID attribute set to #REGION_STATIC_ID#. The dynamic action framework relies on this ID value to reference the region.

      • DOM Object - Enter either the Document Object Model (DOM) object or the ID of a DOM object that is affected by the dynamic action.

      • jQuery Selector - Enter the jQuery selector syntax to return one or more page elements that is affected by the dynamic action.

  7. For Comments, enter developer comments or notes. These comments never display when the application is running.

  8. Click Create.

    The Edit Dynamic Action page displays with the added action listed under True Actions.

17.9.5 Defining the Frequency and Scope

After creating the dynamic action, the scope of the action can be modified to trigger only once, for the lifetime of the current page, or until triggering elements are updated by a Partial Page Refresh (PPR).

To specify scope:

  1. Navigate to the appropriate Page Definition. See "Accessing the Page Definition".

  2. Under Page Rendering, scroll down to Dynamic Actions and click the dynamic action you want to modify.

    The Edit Dynamic Action page appears.

  3. Scroll down to Advanced and for Event Scope make one of the following selections:

    1. Static (default) - Binds the event handler to the triggering elements for the lifetime of the current page, but will no longer be bound if the triggering elements are updated by Partial Page Refresh (PPR).

    2. Dynamic - Binds the event handler to the triggering elements for the lifetime of the current page, including any elements that are updated by Partial Page Refresh (PPR). Specifying Dynamic causes an additional field Static Container (jQuery Selector) to be displayed. Specifying a Static Container can help improve the performance of the way events are handled with a Dynamic Event Scope. This should be an element on the page that itself does not get re-created, but contains the triggering elements that are re-created using Partial Page Refresh (PPR). This element reference must be defined as a jQuery selector. For example if you have a dynamic action that does something to the rows of an interactive report region (which is re-created by PPR), this would need an Event Scope of Dynamic, in order for the dynamic action to still work after the report has been refreshed. And here, the Static Container value could be set to a jQuery selector selecting the region's Static ID value, for example: '#my_region'.

    3. Once - Binds the event handler to the triggering elements for a once only event.

  4. Click Apply Changes.

17.9.6 Debugging Dynamic Actions

Debugging dynamic actions in Oracle Application Express is slightly different than other debugging, because much of the processing done with the dynamic action framework is done on the client, not on the server. To debug dynamic actions, Oracle Application Express outputs debug information to the browser's JavaScript console if your browser supports it (for example Firefox with Firebug installed shows the debug information in its Console pane). The debug information tells you when a dynamic action fires, the name of the dynamic action and also the Action JavaScript Object, which contains all the information needed by the client to execute the action (for example, event name, when details, action, affected elements and so on).

To debug a dynamic action:

  1. Ensure the application containing the dynamic action has Debugging enabled. See "Accessing Debugging Mode".

  2. Run the page containing the dynamic action.

  3. Open the browser's JavaScript console.

  4. From the Developer toolbar, click Debug.

    The page refreshes. If you have any dynamic actions that are set to fire on page load, you see the following text output to the console:

    Dynamic Action Fired: [Dynamic action name] ({JavaScript Object containing all Dynamic Action information})

    Since debug information is only output when running in Debug mode, leaving Debug mode switched on enables you to further test if dynamic actions are firing when you expect them to. For example if you have defined a dynamic action that fires when a certain item's value changes, change that item's value and the console shows the debug output if the dynamic action fires.