πŸ“Š Dynamically Calculate Total in Editable Interactive Grid (Oracle APEX + JavaScript)

πŸ“Š Dynamically Calculate Total in Editable Interactive Grid (Oracle APEX + JavaScript)


πŸ”Ή About

This step-by-step blog explains how to dynamically calculate and display the total (SUM) of a column in an Editable Interactive Grid (IG) using Oracle APEX and JavaScript.

Instead of relying on the default footer aggregation, we will:

βœ” Hide the IG footer

βœ” Create a custom display region

βœ” Calculate total dynamically on change

βœ” Show real-time updates


πŸ› οΈ Tools and Technologies

  • Oracle APEX
  • JavaScript

πŸš€ Step-by-Step Implementation


βœ… Step 1: Create Editable Interactive Grid

Create an Editable Interactive Grid based on the EMP table.


βœ… Step 2: Assign Static ID

Go to your IG region β†’ Advanced β†’ Static ID

emp_ig

βœ… Step 3: Hide Footer Using Initialization Code

Go to:

IG Region β†’ Attributes β†’ Initialization JavaScript Function

Add:

function ( config ) {
   config.defaultGridViewOptions = { footer: false }; // Hides Footer
   return config;
}

πŸ’‘ This removes the default total row so we can build a custom one.


βœ… Step 4: Create Display Region

Create a Static Content Region:

Display Total

Display Total

Set Template Options:

  • βœ” Remove Body Padding
  • βœ” Header β†’ Hidden but Accessible
  • βœ” Item Spacing β†’ Slim
  • βœ” Top Margin β†’ None
  • βœ” Bottom Margin β†’ None

βœ… Step 5: Create Page Item

Create a Display Only Item:

PXX_TOTAL_SAL

Set Properties:

  • Send on Page Submit β†’ Off
  • Column β†’ 9
  • Warn on Unsaved Changes β†’ Ignore

βœ… Step 6: Create Dynamic Action (On SAL Change)

πŸ”Ή Event Details

  • Column β†’ SAL
  • Event β†’ Change

πŸ”Ή True Action 1: Set Value

  • Set Type β†’ PL/SQL Expression
  • Expression β†’
:SAL
  • Items to Submit β†’ SAL
  • Affected Item β†’ PXX_TOTAL_SAL

πŸ”Ή True Action 2: Execute JavaScript

var model = apex.region("emp_ig").widget().interactiveGrid("getViews", "grid").model;

var n_sal, n_total = 0;

var col_sal = model.getFieldKey("SAL");

// Loop through IG rows
model.forEach(function(igrow) {            
   n_sal = parseInt(igrow[col_sal], 10);
   if (!isNaN(n_sal)) {
      n_total += n_sal;
   }
});

console.log(n_total);

// Set value to page item
$s('PXX_TOTAL_SAL', n_total);

πŸ”Ή Client-side Condition

  • Type β†’ Item/Column is not null
  • Column β†’ SAL

βœ… Step 7: Dynamic Action on Page Load

Create another Dynamic Action:

πŸ”Ή Event

  • Event β†’ Page Load

πŸ”Ή True Action: Execute JavaScript

Use the same code:

var model = apex.region("emp_ig").widget().interactiveGrid("getViews", "grid").model;

var n_sal, n_total = 0;

var col_sal = model.getFieldKey("SAL");

model.forEach(function(igrow) {            
   n_sal = parseInt(igrow[col_sal], 10);
   if (!isNaN(n_sal)) {
      n_total += n_sal;
   }
});

$s('PXX_TOTAL_SAL', n_total);

🎯 Final Output

βœ” Real-time total calculation

βœ” Updates when SAL column changes

βœ” Clean UI (no footer clutter)

βœ” Fully dynamic without page refresh


πŸ’‘ Why This Approach?

Instead of using default IG aggregation:

❌ Limited customization

❌ Footer clutter

We use:

βœ… JavaScript model API

βœ… Custom UI region

βœ… Real-time updates


πŸš€ Pro Tips

βœ” Use parseFloat() if dealing with decimals

βœ” Add formatting using toLocaleString()

βœ” Extend for multiple columns (Bonus)


πŸ”₯ Use Cases

  • Salary calculations
  • Invoice totals
  • Order summaries
  • Financial dashboards

πŸŽ‰ Conclusion

This approach gives you full control over Interactive Grid calculations and improves user experience with real-time updates and clean UI.

Love coding? Me too! Let’s keep in touch – subscribe to my website for regular chats on Oracle APEX, PL/SQL,SQL JavaScript, and CSS.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *