Skip to main content

Interactive Grid - Add row from another report using JavaScript

There was a requirement to build a page where users can search for data in a report and then choose a row to be added to Interactive Grid (IG), besides regular way of adding rows to IG. There are two approaches (could be more) to achieve this. Old school approach is adding a row to APEX Collections when user chooses a row in report and using same collection in IG query. Other approach could be, doing something with JavaScript (JS) and build solution on top of IG framework using IG JS APIs, without using collections.

When we convert this requirement into technical specification, essentially it has two parts.
  1. Adding new row to IG via JS
  2. Set values for newly added row via JS
Luckily, there are JS APIs and functions already available to achieve both the points.

Adding new row to IG via JS

Adding new empty row via JS is rather straight forward and easy.

    // "order" is IG region static ID
    var $widget = apex.region('order').widget();
    var $grid = $widget.interactiveGrid('getViews', 'grid');
    var $model = $grid.model;

    //insert new record on a model
    var newRecordId = $model.insertNewRecord();

In above code order is IG region static ID. 

Set values for newly added row via JS

Once row is added you can add values into IG columns using below code

    //continuation of above code
    //get the new record
    var $newRecord = $model.getRecord(newRecordId);

    // PRODUCT_ID and ORDER_QUANTITY are column names in IG SQL query
    //update record values
    $model.setValue($newRecord, 'PRODUCT_ID', pProductId);
    //set default quantity to 1
    $model.setValue($newRecord, 'ORDER_QUANTITY', '1');

That's it. 

Here is the link to working example. In this example, there is a Interactive Report (IR) region "Search & Select" and "Product Name" column has a link which calls JS function "addToOrder" with product_id passed as input parameter. Another IG region "Your Order".

JS function code

  function addToOrder(pProductId) {
    // "order" is IG region static ID
    var $widget = apex.region('order').widget();
    var $grid = $widget.interactiveGrid('getViews', 'grid');
    var $model = $grid.model;

    //insert new record on a model
    var newRecordId = $model.insertNewRecord();
    
    //get the new record
    var $newRecord = $model.getRecord(newRecordId);

    // PRODUCT_ID and ORDER_QUANTITY are column names in IG SQL query
    //update record values
    $model.setValue($newRecord, 'PRODUCT_ID', pProductId);
    //set default quantity to 1
    $model.setValue($newRecord, 'ORDER_QUANTITY', '1');    
  }  

Tested with APEX Versions: 18.1,18.2 and 19.1

Thank you.

Comments

Parul Jain said…
Thanks for Sharing this. Great approach.. !! :)
Hari said…
Thank you Parul and Arun.
Veerendra.Patil said…
Hi,
How do you add the ROW in the last?
$model.insertNewRecord() always creates a new row on top.

Unknown said…
hi,
i'm quite new to oracle apex. could you please be so kind to specify what DA do i need to use for example above?
Unknown said…
i have no idea where to put the JS either. please help me. thanks
Hari said…
Hi, What is your requirement? Would it be possible to set-up an example at apex.oracle.com and post details here?
Unknown said…
very good article! but I am still confused about some steps. Could you please share the details of setting up this example?

Popular posts from this blog

ORA-00936: missing expression error while updating data using Tabular form

Are you getting "ORA-00936: missing expression" error while trying to update existing data using tabular form? Are you using "Multi Row Update" or "Tabular Form" processes (ApplyMRU or Apply MRD), then most probably the issue is with a space or new line character you have entered in "Runtime Where Clause" section of "ApplyMRU" process However to know the exact reason behind the error, run page in debug mode and try saving data. Then see debug output. If the issue is with space or new line character which you have entered, then you will see entry similar to below as you can see above (highlighted with yellow color), APEX finds there is some value entered for  "Runtime Where Clause" and it will append this where clause to update query. Since it's not a valid SQL Update statement, we will get "ORA-00936: missing expression" error. Fix : It's obvious. Go to page designer and remove any spaces or new l...

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...