oracle11goracle-apextabular-form

Calculate Sum of Oracle Apex Tabular Form Cell


There is a column as Amount in my oracle apex tabular form. I need to calculate SUM of all fields under this column while data is being entered and display on a Display only field below the tabular form.

I think this can be done using JavaScript and call that JavaScript at onchange of Amount column.

But I don't know how to calculate SUMof Amountcolumn in my oracle apex tabular form. How could I do this?


Solution

  • Add the following JavaScript code into the Page HTML Header property:

    <script type="text/javascript">
    function tot_cal()
    {
    var f5=new Array();
    var tol=0;
    f5=document.getElementsByName("f05"); /*f05 is Apex array which holds the data*/
    
     for(i=0;i<f5.length;i++){
      tol = (tol*1) + (f5[i].value.replace(/,/g, '') * 1);
     }
    /* alert(tol); */
    $s('P10_AMOUNT_VALUE', tol.formatMoney(2,',','.'));
    }
    Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator) {
        var n = this,
        decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
        decSeparator = decSeparator == undefined ? "." : decSeparator,
        thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
        sign = n < 0 ? "-" : "",
        i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
        return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
    };
    </script>
    

    Tabular Form Element/Element Attributes property of the Amount column:

    onchange="tot_cal();"