Interactive Grid Automatic column calculation Using
JavaScript
Hello viewers welcome
to my new video how to calculate oracle apex interactive grid columns using
JavaScript.
Step: 1 : First go the
interactive grid page
Step: 2 :Then go the Event > Change > Create a Dynamic action > dynamic action properties >when column > give the column name> UNIT_PRICE,QUANTITY
Step 3 : True Action
> Execute JavaScript Code.
var unitPrice = ($v("T_UNIT") !== '') ?
parseFloat($v("T_UNIT")) : 0;
var quantity = ($v("T_QUANTITY") !== '') ?
parseFloat($v("T_QUANTITY")) : 0;
var totalAmount = 0;
if (!isNaN(unitPrice) && !isNaN(quantity)) {
totalAmount =
unitPrice * quantity;
}
$s('DEMO_ORDER_TOTAL', totalAmount);
This JavaScript code appears to be calculating a total
amount based on unit price and quantity, then updating a field named
"DEMO_ORDER_TOTAL" with the calculated value. Let's break down each
part:
- Variable Declarations:
- unitPrice: It's
assigned the value of the field "T_UNIT" if it's not an empty
string; otherwise, it's assigned 0.
- quantity: It's
assigned the value of the field "T_QUANTITY" if it's not an
empty string; otherwise, it's assigned 0.
- totalAmount: Initialized
to 0. This variable will store the total calculated amount.
- Conditional Statements:
- There are conditional (ternary)
statements used to check if the values obtained from the fields
"T_UNIT" and "T_QUANTITY" are not empty strings. If
they are not empty, the values are converted to floating-point numbers
using parseFloat(). If they are
empty, the values are set to 0.
- Calculation:
- If both unitPrice and quantity are valid numbers (not NaN), it
calculates the total amount by multiplying unitPrice by quantity.
- Update Field:
- Finally, the calculated totalAmount is assigned to the field
"DEMO_ORDER_TOTAL" using $s() function,
which presumably updates the value displayed in a UI element.
Here's
a simplified breakdown of the code flow:
- Get values of unitPrice and quantity from respective fields.
- Check if both values are valid numbers.
- If yes, calculate the total
amount.
- Update the field
"DEMO_ORDER_TOTAL" with the calculated total amount.
This code is likely used in a web application or some
environment where JavaScript is executed to perform dynamic calculations and
update the UI accordingly.
No comments:
Post a Comment