0 Replies Latest reply on Jun 17, 2019 11:48 AM by Jose Aróstegui

    How to trigger total recalculation of an IG sum from a non IG text item?

    Jose Aróstegui

      Hi experts,


      I'm using Apex 18.2 and I've successfully implemented a total calculation inside an IG thanks to John Snyders-Oracle (John Snyders):


      How to hack APEX Interactive Grid Part 4 – HardLikeSoftware


      // 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.
          // Call this whenever the model data changes.
          function update(model) {
              var salKey = model.getFieldKey("SAL"), 
                  total = 0;
              model.forEach(function(record, index, id) {
                  var sal = parseInt(record[salKey], 10),  // record[salKey] should be a little faster than using model.getValue in a loop
                      meta = model.getRecordMetadata(id);
                  if (!isNaN(sal) && !meta.deleted && !meta.agg) {
                      total += sal;
              $s("P1_TOTAL", 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 important thing to realize is that each
                  // view has its own model
                  if ( ui.viewId === "grid" ) {
                      sid = model.subscribe( {
                          onChange: function(type, change) {
                              if ( type === "set" ) {
                                  // don't bother to recalculate if other columns change
                                  if (change.field === "SAL" ) {
                                      update( model );
                              } else if (type !== "move" && type !== "metaChange") {
                                  // any other change except for move and metaChange affect the calculation
                                  update( model );
                          progressView: $("#P1_TOTAL") // in theory this will cause a spinner on this field but I don't see it.
                      } );
                      // 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() {});


      My next requirement is to trigger the recalculation when an item from outside the IG is updated.


      Can anybody provide guidance on this?

      Thanks a lot,