Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

subtotal table that has user defined categories

Avatar

Level 1

I have a table where I need to subtotal user defined values. The entries can be any format - although they are usually of the form A12345(e.g.) - and there can be multiple entries of the same value. What I came up with is a javascript that builds an array, filters the unique values, compares the two and then subtotals the results in a second table. The script triggers on exiting the Tmark and Estimated Volume fields of the form.uniqueSortStaticTable_loop.jpg

//function to find unique values in a given array

    var unique = function(origArray) {

    var newArray = [],

    origLen = origArray.length,

    found,

    x, y;

    

    for ( x = 0; x < origLen; x++ ) {

    found = undefined;

    for ( y = 0; y < newArray.length; y++ ) {

    if ( origArray[x] === newArray[y] ) {

    found = true;

    break;

    }

    }

    if ( !found) newArray.push( origArray[x] );

    }

    return newArray;

    }

    

//use find unique function to set values for the original array and for that same array filtered for unique values

var tmarkArray = []

var uniqArray = [];

for (var i=0; i < 4; i++) {

    var mark = xfa.resolveNode("form1.page1.table.row[" + i + "].tmark").rawValue;   

    uniqArray.push(mark);

    tmarkArray.push(mark);

    }

    uniqArray = unique(uniqArray);

       

//get array of estimated volume values from first table

var nAmount = []

for (var i=0; i < 4; i++) {

    var estVol = xfa.resolveNode("form1.page1.table.row[" + i + "].estVol").rawValue;   

    nAmount.push(estVol);

    }

//clear values in second table to allow recalculation

for (var i=0; i<tmarkArray.length; i++)

{   

xfa.resolveNode("form1.page1.Table1.row[" + i + "].Cell1").rawValue = null;

xfa.resolveNode("form1.page1.Table1.row[" + i + "].Cell2").rawValue = null;   

xfa.resolveNode("form1.page1.Table1.row[" + i + "].Cell3").rawValue = null;   

}

//loop through first table and fill second table with subtotalled amounts.

for (var i=0; i<tmarkArray.length; i++)

{   

for (var a=0; a < 4; a++) {

    if (tmarkArray[i] == uniqArray[a])    {

        if  (xfa.resolveNode("form1.page1.Table1.row["+ a +"].Cell3").rawValue != null){

       

                xfa.resolveNode("form1.page1.Table1.row["+ a +"].Cell1").rawValue = tmarkArray[i];

                xfa.resolveNode("form1.page1.Table1.row["+ a +"].Cell3").rawValue = xfa.resolveNode("form1.page1.Table1.row["+ a +"].Cell3").rawValue + nAmount[i];

            }

        else {

                xfa.resolveNode("form1.page1.Table1.row["+ a +"].Cell1").rawValue = tmarkArray[i];

                xfa.resolveNode("form1.page1.Table1.row["+ a +"].Cell3").rawValue = nAmount[i];

            }

        }

        }

}

The script works, but I don't think it is very elegant and it only works on a static table. I would like to be able to use it on dynamic tables.

0 Replies