Skip to main content

Interactive Grid - Client Side Aggregate Validations

In my previous blog post Interactive Grid - Aggregate Validations, I have explained how we can do "Aggregate Validations" for Interactive Grid (IG) data using server side validations. Server side validations technique is useful for complex validations that may involve checking data from multiple tables or number of rows in IG are more than 50 etc. However, for simple validations, like the one defined in my previous blog post, we can achieve it using Java Script (JS) APIs available for IG, entirely on client side.

Let's consider the same example again. Budget planning, where you can allocate percentage for each category, like 10% for healthcare, 10% for education etc. Here, we need to implement a simple table level validation to ensure total allocation % does not exceed 100.

  • To start with, create IG region on BLOG_BUDGET_PLANNING table and enable edit mode. Assume BLOG_BUDGET_PLANNING columns as PLAN_ID, PLAN_CATEGORY, PLAN_PERCENTAGE and Audit (Who) columns
  • In IG > Attributes > Toolbar section, deselect "Save Button" option, so default IG "Save" button is not displayed.
  • Next, create "SAVE" button which will trigger dynamic action and executes below JS code. 
Dynamic Action JS Code.

var totalPercentage = 0;
// get IG model. agg_val is interactive grid region "Static ID"
var model = apex.region("agg_val").widget().interactiveGrid("getCurrentView").model;

// loop through all the rows in model and calculate total allocation percentage
model.forEach(function(record, index, id) {
    // PLAN_PERCENTAGE is column alias for "Allocation %" column
    var currPercentage = parseFloat(model.getValue(record, "PLAN_PERCENTAGE")),
        meta = model.getRecordMetadata(id);

    // ignore aggregated and deleted rows
    // we can use meta data of row to identify if the row is new or getting deleted etc.
    if (!isNaN(currPercentage) && !meta.deleted && !meta.agg) totalPercentage += currPercentage;
});

//console.log("totalPercentage " + totalPercentage);
// clear errors, if any
apex.message.clearErrors();

if (totalPercentage <= 100) {
    /* Use below code if you want to submit the page */
    /*
    apex.page.submit({
      request: "SAVE",
      showWait: true
    });  
    */
    // below code triggers IG AJAX Save and refreshes the IG region
    model.save(function() {
        apex.region("agg_val").refresh();
    });
} else {
    // Error object, for single error
    errorDetails = {
        type: "error",
        location: ["page"],
        message: "Total Allocation should not exceed 100.",
        unsafe: false
    };
    // show error
    apex.message.showErrors(errorDetails);
}

Important Note: When pagination style "Page" is chosen for IG, then only first 50 rows (or rows selected in "Rows Per Page") are loaded in the "modal". For pagination style "Scroll", initially 50 rows are loaded into the "modal" and subsequent rows are loaded as user scrolls down. So, if your IG has more than 50 rows, then choose pagination style as "Page" and choose "Rows Per Page" accordingly to ensure all rows are loaded into the model. You can also load all the rows into the model using model.fetchAll and it works for both the pagination styles.

However, if the number of rows in IG are more than 50, then, displaying all the rows in UI may not look good and could also have performance impact. Even if you use model.fetchAll to load all rows into the modal, it can still have performance impact. Below note is from APEX documentation.

Loading too much data onto the client can take a long time and cause the browser to become unresponsive.

So in these cases, I recommend to use technique I have explained in my previous blog post Interactive Grid - Aggregate Validations. Last, but not least, client side validations are good, but not sufficient. So, it's always recommended to have server side validations, irrespective of whether you have client side validations or not.

That's it. We are good to go. link to demo

What do you think about this approach? Do you have any other ideas to solve this problem? I welcome your feedback.

Thank you.

Update-1 on 12-APR-2020: Updated code to use model.save method to trigger ajax save, instead of page submit. Also updated some notes. Please refer comment from John Snyders below.

Comments

John Snyders said…
This client side validation is good but you MUST also do the same server side validation.
It is possible to combine this with ajax save (rather than page submit) if you desire.
Hari said…
Thank you John. Yes, agree. Client side validations are good but not sufficient. I have added this point to above blog post. I have also modified code to use model.save to make use ajax save instead of page submit.
APEX Rocks said…
Nice post explaining APEX JS APIs with simple example. Thank you.

Popular posts from this blog

Interactive Grid - Conditional Enable/Disable

In this blogpost, I am going to discuss few approaches using which we can conditionally enable/disable Interactive Grid (IG) column(s) based on other column(s) values. Note:    There is a bug  30801170  in APEX 19.2/20.1 with respect to "enable/disable" dynamic actions for IG columns. Workaround for this bug is provided at the end of this blogpost . This bug has been fixed in APEX version 20.2. Client Side Only Conditions If conditions to enable/disable are simple, then we can check those conditions easily on the client side. For e.g. let's consider IG on EMP table with following SQL Query. SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO FROM EMP Let's consider the requirement as - Enable "Commission" column only when JOB is equals to 'SALESMAN' and disable "Commission" column in all other cases. This can be done declaratively using dynamic actions (DA) DA "Enable/Disable Commission - 1" Create DA and give it a prope

Interactive Grid - Bulk Operation on Selected Rows

What's the Problem? Let's say you have an IG on employee table and you want to update selected employees commission based on user's input. Ideally, it should be very simple, where you can write UPDATE statement in page process and select IG region as "Editable Region" under "Execution Options" of the page process. But, when you select rows and submit page, you can see that, this process won't get executed! The reason is  Selection of 'Row Selector' check-boxes is not considered as row-change. Thus selected rows are not submitted to server. So, is there any work around?  Yes! Luckily there are lot of JavaScript (JS) APIs available to work with IG. If you are not already aware, you can refer "APEX IG Cookbook"  or  JavaScript API Reference documentation. If we continue with above Employee IG example, when user selects IG rows, enters "Commission %" and clicks on "Update Commission" button, then we can writ

Interactive Grid - Process Filtered Data on Server Side

Recently one of the APEX developers has reached out to me and asked if it's possible to capture filtered rows data of the Interactive Grid on the server-side and do some processing. In APEX 20.1, there is a new API APEX_IG , using which we can achieve this. Photo by Jakub Kapusnak on Unsplash The approach is very simple and straightforward. Get the internal region id based on the Static ID given for the IG region Get the last viewed report id based on region id Open query context for the region and report using region id and report id Fetch and loop through the rows using the query context Do something with fetched rows And finally, close the query context If you have already done this for interactive reports, then you should be already aware of these steps. The only difference here is, we use APEX_IG APIs instead of APEX_IR APIs. For the demo purpose, let's Build an Interactive Grid on EMP table and let's give it a Static ID as emp Create a Textarea page item with