Forum Stats

  • 3,874,184 Users
  • 2,266,680 Discussions
  • 7,911,761 Comments

Discussions

IG compute values

Dernbauer
Dernbauer Member Posts: 105 Red Ribbon

I have a IG with three columns, Var1; Var2 and Var3

I would like to compute values of Var1 when the values are changing.

Finaly it should be fire a alert when the sum of Var1 is greater then 100 (because it is percent).

I did

var model = apex.region("IG1").widget().interactiveGrid("getViews","grid").model;

var var1 = model.getFieldKey("VAR1");

var sum;

var total = 0;

model.forEach(function(igrow) {

    sum = parseInt(igrow[var1],10);

    total += sum;

  })

alert("Wert ist :" + total );

It works, BUT

But it is fired

on the IG Column and if I change the one value the computed value is the old an not the new value, so it ignores the new inputed value.


Example

VAR1

VAR1 Value 1

VAR1 Value 2

VAR1 Value 3

SUM = 6

If I Change one of the values lets assume the first from 1 to 5 uit should be 10 but it shows still 6.

If i change it the next time it shows 10 wich is correct for the prvious values .


Any Ideas


Thank you

Answers

  • Hamza Al-abbasi
    Hamza Al-abbasi Member Posts: 784 Bronze Crown

    Hello,

    See IG Cookbook Page 5 Application it may helps.

  • Dernbauer
    Dernbauer Member Posts: 105 Red Ribbon

    Again ;-)

    APEX 21.2.0

    A IG with 3 Fields (var1; var2; var3).

    On Event Change VAR1

    True Action

    And Execuote JavaScriptCode

    Code is:

    var model = apex.region("IG1").widget().interactiveGrid("getViews","grid").model;

    var var1 = model.getFieldKey("VAR1");

    var sum;

    var1 = 0;

    var total = 0;

    console.log("Var1=" + var1);

    console.log("total=" + total);

    model.forEach(function(igrow) {

        sum = parseInt(igrow[var1],10);

        total += sum;

        //alert ("Var1xxx = " + igrow[var1]);

    //    console.log(Number(igrow[var1]));

    //    console.log(sum);

         

        console.log("Var1 = " +igrow[var1]);

        console.log("Total = " +total);

    })

    //alert("Wert ist :" + total );

    If I execute it:

    If I change one Value of VAR1:

    If I change that value again;

    It takes always the value from the last action. but not from the current action.


    Any Ideas ?

    Thank you

  • Hamza Al-abbasi
    Hamza Al-abbasi Member Posts: 784 Bronze Crown
    edited May 26, 2022 9:32PM

    Any Ideas ?

    If you downlead the IG Cookbook app above you will get the idea :) any way try this In Function and Global Variable Declaration :

    NOTE: Change [IG_STATIC_ID] to your IG Static Id e.g."emp" :

    (function($) {
      function update(model) {
        var var1Key = model.getFieldKey("VAR1"), 
          total = 0;
    
        model.forEach(function(record, index, id) {
          var var1 = parseFloat(record[var1Key]),
            meta = model.getRecordMetadata(id);
    
          if (!isNaN(var1) && !meta.deleted && !meta.agg) {
            total += var1;
          }
        });
        console.log(">> setting sum VAR1 column to " + total);
      }
    
      $(function() {
        $("#[IG_STATIC_ID]").on("interactivegridviewmodelcreate", function(event, ui) {
          var sid,
            model = ui.model;
    
          if ( ui.viewId === "grid" ) {
            sid = model.subscribe( {
              onChange: function(type, change) {
                if ( type === "set" ) {
                  if (change.field === "VAR1" ) {
                    update( model );
                  }
                } else if (type !== "move" && type !== "metaChange") {
                  update( model );
                }
              }
            } );
            update( model ); 
            model.fetchAll(function() {});
          }
        });
      });
    
    })(apex.jQuery);