Skip to Main Content

APEX

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trying to calculate dynamically the sum of a colum in an interactive Grid

Christian Pitet.6 hours ago

Hi,

Under Oracle APEX 24.2.14, I have an on a form P.13 an Interactive grid with a colum “DUREE”. I would like to sum dynamically the column TOTAL of the IG.

I have tried A DA on the clicking on a button "Compute sum", the sum is displayed in the field P13_GRAND_TOTAL.

The colum DUREE is computed from the diffrence BETWEEN the colums DATE_DEBUT ans DATE_FIN.

I would have like to not to have to click on the button, instead to dynamically calculate the item P13_GRAND_TOTAL, each time DUREE is calculated.

I tried to adapt this script form the IG cookbok, but it is not working, it is not doing the calculation. I have taken this script from the IG Cookbook:

// create a private scope where $ is set to apex.jQuery
(function($) {
   // This is the function that calculates over all the rows of the model and then
   // updates something else.
   // Change this to do whatever calculation is needed.
   // Call this whenever the model data changes.
   function update(model) {
       var salKey = model.getFieldKey("DUREE"), 
           total = 0;
       console.log(">> starting sum DUREE column")
       model.forEach(function(record, index, id) {
           var duree = parseFloat(record[salKey]),  // record[salKey] should be a little faster than using model.getValue in a loop
               meta = model.getRecordMetadata(id);
           if (!isNaN(duree) && !meta.deleted && !meta.agg) {
               total += duree;
           }
       });
       console.log(">> setting sum DUREE column to " + total)
       $s("P13_TOTAL_DUREE", total);
   }
   //
   // This is the general pattern for subscribing to model notifications
   //
   // need to do this here rather than in Execute when Page Loads so that the handler
   // is setup BEFORE the IG is initialized otherwise miss the first model created event
   $(function() {
       // the model gets released and created at various times such as when the report changes
       // listen for model created events so that we can subscribe to model notifications
       $("#emp").on("interactivegridviewmodelcreate", function(event, ui) {
           var sid,
               model = ui.model;
           // note this is only done for the grid veiw. It could be done for
           // other views if desired. The imporant thing to realize is that each
           // view has its own model
           if ( ui.viewId === "grid" ) {
               sid = model.subscribe( {
                   onChange: function(type, change) {
                       console.log(">> model changed ", type, change);
                       if ( type === "set" ) {
                           // don't bother to recalculate if other columns change
                           if (change.field === "DUREE" ) {
                               update( model );
                           }
                       } else if (type !== "move" && type !== "metaChange") {
                           // any other change except for move and metaChange affect the calculation
                           update( model );
                       }
                   },
                   progressView: $("#P13_TOTAL_DUREE") // this will cause a spinner on this field
               } );
               // if not lazy loaded there is no notification for initial data so update
               update( model ); 
               // just in case fetch all the data. Model notifications will
               // cause calls to update so nothing to do in the callback function.
               // can remove if data will always be less than 50 records
               model.fetchAll(function() {});
           }
       });
   });
})(apex.jQuery);

This is the modified Javascript that I have adapted for the the columns and elements on the IG. If you could be kind enough to investigate the Javascript to check for errors, I am not good with Javascript.

Best regards.

This post has been answered by Dasshini Ravi on Apr 2 2026
Jump to Answer
Comments
Post Details
Added 6 hours ago
2 comments
156 views