Ora

How to Refresh a Region in Oracle APEX?

Published in APEX Region Management 6 mins read

Refreshing a region in Oracle APEX is a fundamental action often required to update displayed data, reflect changes, or react to user interactions without reloading the entire page. The most common and recommended way to achieve this is by utilizing Oracle APEX's robust client-side JavaScript API, specifically the apex.region().refresh() method, or by configuring a Dynamic Action.

Understanding Region Refresh in APEX

Refreshing a region dynamically updates its content from the server. This process is known as a Partial Page Refresh (PPR) and significantly enhances the user experience by avoiding full page reloads, which can be slower and disruptive. It's essential when data within a report, chart, or interactive grid changes, and you need the UI to reflect those changes immediately.

Methods to Refresh an APEX Region

There are primarily two highly effective methods to refresh a region in Oracle APEX: through a Dynamic Action or by writing custom JavaScript.

1. Refreshing a Region Using Dynamic Actions

Dynamic Actions are the no-code/low-code way to implement client-side interactivity in APEX. They provide a declarative interface to trigger actions based on various events.

Steps to Create a Dynamic Action for Region Refresh:

  1. Navigate to Page Designer: Open your APEX application and go to the Page Designer for the page containing the region you wish to refresh.
  2. Create a New Dynamic Action:
    • In the "Rendering" pane, select the page or a specific item that will trigger the refresh (e.g., a button, a select list, another region).
    • Right-click on it and choose "Create Dynamic Action."
  3. Define the Dynamic Action Event:
    • Name: Give it a descriptive name (e.g., "Refresh_My_Report_Region").
    • Event: Select the event that should trigger the refresh. Common events include:
      • Click: For buttons or links.
      • Change: For select lists, text fields (when their value changes).
      • After Refresh: If one region needs to refresh after another has completed its refresh.
    • Selection Type: Often "Item" or "Button," depending on your trigger.
    • Item(s) / Button: Specify the exact item or button.
  4. Define the True Action:
    • Right-click on the "True" branch of your newly created Dynamic Action and choose "Create True Action."
    • Action: Select "Refresh."
    • Selection Type: Choose "Region."
    • Region: From the dropdown, select the specific region you want to refresh (e.g., "My Classic Report," "Employee Interactive Grid").
    • Page Items to Submit (Crucial for Contextual Refresh): If your refreshed region's source query relies on the values of page items (e.g., a search filter, a selected ID), list those page items here, separated by commas (e.g., P1_DEPARTMENT_ID,P1_SEARCH_TEXT). These item values will be sent to the server before the region refreshes, allowing the region's query to use their current state.

Example Use Case:
Imagine you have a select list P1_DEPARTMENT_SELECT and a Classic Report region named EMPLOYEE_REPORT. When the user selects a department from the list, the EMPLOYEE_REPORT should update to show only employees from that department.

  • Dynamic Action Trigger: On P1_DEPARTMENT_SELECT.
  • Event: Change.
  • True Action: Refresh.
  • Region: EMPLOYEE_REPORT.
  • Page Items to Submit: P1_DEPARTMENT_SELECT (so the report's query can use :P1_DEPARTMENT_SELECT).

2. Refreshing a Region Using JavaScript

For more complex scenarios, programmatic control, or when integrating with custom client-side logic, you can directly use the APEX JavaScript API. The recommended method is apex.region().refresh(). This method is designed to refresh the content of a region that supports being refreshed.

Syntax:

apex.region("YOUR_REGION_STATIC_ID").refresh();

How to Implement Custom JavaScript Refresh:

  1. Identify Region Static ID:
    • Select your target region in Page Designer.
    • In the "Properties" pane, locate the "Static ID" attribute under the "Identification" section. If it's empty, provide a unique ID (e.g., my_employee_report). This ID is essential for the JavaScript to target the correct region.
  2. Add JavaScript Code:
    • Directly in a Button URL/Link: For simple cases, you can put javascript:apex.region('my_employee_report').refresh(); in the "Target" of a button's "Link Builder" dialogue (set "Action" to "Redirect to URL").
    • In a Dynamic Action (Execute JavaScript Code): This is common when you need to combine multiple actions or have more complex conditions. Create a Dynamic Action, and for the "True Action," choose "Execute JavaScript Code."
    • In a Page's "Function and Global Variable Declaration" or "Execute when Page Loads" sections: For page-level logic or utility functions.

Example: Refreshing a Region After an AJAX Call:

If you have a custom AJAX process (apex.server.process) that updates database records, you might want to refresh a report after it completes successfully.

apex.server.process(
    "MY_PROCESS_NAME", // Name of your On-Demand APEX Process
    {
        x01: "value1",
        pageItems: "#P1_ITEM_TO_SUBMIT,#P1_ANOTHER_ITEM"
    },
    {
        success: function(pData) {
            // After successful processing, refresh the report region
            apex.message.showPageSuccess("Record updated successfully!");
            apex.region("my_employee_report").refresh();
        },
        error: function(jqXHR, textStatus, errorThrown) {
            apex.message.showPageErrors([{
                type: "error",
                message: "Error updating record: " + textStatus,
                location: ["page"]
            }]);
        }
    }
);

Table: Comparison of Refresh Methods

Feature Dynamic Action (Refresh Action) JavaScript (apex.region().refresh())
Ease of Use Declarative, low-code/no-code, wizard-driven Requires JavaScript knowledge, more programmatic
Flexibility Excellent for common scenarios, multiple actions Highly flexible for complex logic, custom integrations
Error Handling Built-in (for basic execution) Must be handled explicitly within JavaScript
Debugging Inspect DA execution in browser developer tools Use browser developer tools for JS debugging
Performance Similar, both trigger AJAX calls Similar, both trigger AJAX calls
Use Case Button clicks, item changes, page load, chained events Custom logic, AJAX callbacks, third-party integrations
Page Items Submit Built-in option (Page Items to Submit) Must manually pass items in apex.server.process or DA

Best Practices for Region Refresh

  • Use Static IDs: Always assign unique Static IDs to regions you intend to refresh programmatically or via Dynamic Actions.
  • Submit Necessary Page Items: If your region's source SQL query uses page item values (e.g., :P1_SELECTED_ID), ensure those items are submitted during the refresh. For Dynamic Actions, use the "Page Items to Submit" property.
  • Consider Interactive Grids/Reports: These regions have their own client-side refresh capabilities, often triggered by their built-in actions or a generic apex.region().refresh().
  • Performance: While PPRs are efficient, avoid excessive or unnecessary refreshes, especially for very large regions or complex queries, as they still involve server communication.
  • User Feedback: For long-running refreshes, consider showing a loading indicator to the user (e.g., using apex.item.loadingIndicator()).

By understanding and applying these methods, you can effectively manage and update content within your Oracle APEX applications, providing a dynamic and responsive user experience.