rice-framework/krad-web/src/main/webapp/krad/scripts/krad.utility.js
/**
 * Initialize/recalculate the totals placed in the footer of a richTable.  Also, calculates and places
 * the totals related to group totalling, if present.
 *
 * @param nRow tr element for the footer
 * @param aaData full table data (as derived from the original HTML)
 * @param iStart index for the current display starting point of the current page in the display array
 * @param iEnd index for the current display ending point of the current page in the display array
 * @param aiDisplay index array to translate the visual position to the full data array
 * @param columns columns to total
 */
Initialize/recalculate the totals placed in the footer of a richTable.  Also, calculates and places
the totals related to group totalling, if present.


Initialize / recalculate the totals placed in the footer of a richTable . Also calculates and places the totals related to group totalling if present .
function initializeTotalsFooter(nRow, aaData, iStart, iEnd, aiDisplay, columns) {
    var footerRow = jQuery(nRow);
    var dataTable = footerRow.closest('table.dataTable');
    footerRow.addClass("uif-totalRow");
    if (jQuery(dataTable).hasClass("uif-hasAddLine")) {
        iEnd = iEnd + 1;
    }
    var onePage = iStart == 0 && iEnd == aaData.length;
    if (onePage) {
        footerRow.find("[data-role='pageTotal'], label[data-role='pageTotal']").hide();
    } else {
        footerRow.find("[data-role='pageTotal'], label[data-role='pageTotal']").show();
    }
    var groupTotalRows = dataTable.find("tr[data-groupvalue]");
    var hasGroups = dataTable.data("groups");
    if (!hasGroups || (hasGroups && groupTotalRows.length)) {
        var nCells = footerRow.find("th").has("[data-role='totalsBlock']");
        var groupLabel = footerRow.find("th:first [data-role='groupTotalLabel']");
        var hasTotalsInFooter = false;
        for (var c = 0; c < nCells.length; c++) {
            var cell = jQuery(nCells[c]);
            var index = cell.index();
            jQuery("[data-role='totalsBlock']", cell).each(function () {
                var totalDiv = jQuery(this).find("[data-role='total']");
                var skipTotal = totalDiv.data(kradVariables.SKIP_TOTAL);
                if (!skipTotal && totalDiv.length) {
                    calculateTotal(totalDiv, 0, aaData.length, columns[c], aaData, aiDisplay);
                }
                if (totalDiv.length) {
                    hasTotalsInFooter = true;
                }
                var pageTotalDiv = jQuery(this).find("[data-role='pageTotal']");
                if (!onePage && pageTotalDiv.length) {
                    calculateTotal(pageTotalDiv, iStart, iEnd, columns[c], aaData, aiDisplay);
                    hasTotalsInFooter = true;
                }
                if (groupTotalRows.length) {
                    var groupTotalDiv = jQuery(this).find("[data-role='groupTotal']");
                    var rowIndex = 0;
                    groupTotalRows.each(function () {
                        var groupTotalRow = jQuery(this);
                        var tds = groupTotalRow.find("td");
                        var td = jQuery(tds[index]);
                        var groupValue = groupTotalRow.data("groupvalue");
                        var lastValue = aaData[aiDisplay[iEnd - 1]][0];
                        if (lastValue && lastValue.toLowerCase() == groupValue && iEnd < aiDisplay.length &&
                            aaData[aiDisplay[iEnd]][0] && aaData[aiDisplay[iEnd]][0].toLowerCase() == groupValue) {
                            groupTotalRow.hide();
                        }
                        else {
                            var groupCellsToTotal = new Array();
                            for (var i = 0; i < aaData.length; i++) {
                                var currentRow = aaData[i];
                                var groupingValue;
                                var isRowObject = currentRow != null && typeof currentRow === 'object'
                                    && !jQuery.isArray(currentRow);
                                if (isRowObject) {
                                    groupingValue = currentRow['c0'].val;
                                }
                                else {
                                    groupingValue = currentRow[0];
                                }
                                if (groupingValue != undefined &&
                                    normalizeGroupString(groupingValue).toLowerCase() == groupValue) {
                                    if (isRowObject) {
                                        groupCellsToTotal.push(currentRow['c' + columns[c]]);
                                    }
                                    else {
                                        groupCellsToTotal.push(currentRow[columns[c]]);
                                    }
                                }
                            }
                            groupTotalRow.show();
                            calculateGroupTotal(groupCellsToTotal, td, groupTotalDiv, rowIndex, columns[c]);
                        }
                        if (groupLabel.length && jQuery(tds[0]).is(":empty")) {
                            groupLabel = groupLabel.clone();
                            groupLabel = groupLabel.attr("id", groupLabel.attr("id") + "_" + rowIndex + columns[c]);
                            groupLabel.find("[id]").each(function () {
                                jQuery(this).attr("id", jQuery(this).attr("id") + "_" + rowIndex + columns[c]);
                            });
                            groupLabel.show();
                            jQuery(tds[0]).append(groupLabel);
                        }
                        rowIndex++;
                    });
                }
            });
        }
        if (hasTotalsInFooter) {
            footerRow.show();
            footerRow.find("th:hidden").addClass("show-footer");
        }
        else {
            footerRow.hide();
        }
    }
}
function initializeTotalsFooter ( nRow , aaData , iStart , iEnd , aiDisplay , columns ) { var footerRow = jQuery ( nRow ) ; var dataTable = footerRow . closest ( 'table.dataTable' ) ; footerRow . addClass ( "uif-totalRow" ) ; if ( jQuery ( dataTable ) . hasClass ( "uif-hasAddLine" ) ) { iEnd = iEnd + 1 ; } var onePage = iStart == 0 && iEnd == aaData . length ; if ( onePage ) { footerRow . find ( "[data-role='pageTotal'], label[data-role='pageTotal']" ) . hide ( ) ; } else { footerRow . find ( "[data-role='pageTotal'], label[data-role='pageTotal']" ) . show ( ) ; } var groupTotalRows = dataTable . find ( "tr[data-groupvalue]" ) ; var hasGroups = dataTable . data ( "groups" ) ; if ( ! hasGroups || ( hasGroups && groupTotalRows . length ) ) { var nCells = footerRow . find ( "th" ) . has ( "[data-role='totalsBlock']" ) ; var groupLabel = footerRow . find ( "th:first [data-role='groupTotalLabel']" ) ; var hasTotalsInFooter = false ; for ( var c = 0 ; c < nCells . length ; c ++ ) { var cell = jQuery ( nCells [ c ] ) ; var index = cell . index ( ) ; jQuery ( "[data-role='totalsBlock']" , cell ) . each ( function ( ) { var totalDiv = jQuery ( this ) . find ( "[data-role='total']" ) ; var skipTotal = totalDiv . data ( kradVariables . SKIP_TOTAL ) ; if ( ! skipTotal && totalDiv . length ) { calculateTotal ( totalDiv , 0 , aaData . length , columns [ c ] , aaData , aiDisplay ) ; } if ( totalDiv . length ) { hasTotalsInFooter = true ; } var pageTotalDiv = jQuery ( this ) . find ( "[data-role='pageTotal']" ) ; if ( ! onePage && pageTotalDiv . length ) { calculateTotal ( pageTotalDiv , iStart , iEnd , columns [ c ] , aaData , aiDisplay ) ; hasTotalsInFooter = true ; } if ( groupTotalRows . length ) { var groupTotalDiv = jQuery ( this ) . find ( "[data-role='groupTotal']" ) ; var rowIndex = 0 ; groupTotalRows . each ( function ( ) { var groupTotalRow = jQuery ( this ) ; var tds = groupTotalRow . find ( "td" ) ; var td = jQuery ( tds [ index ] ) ; var groupValue = groupTotalRow . data ( "groupvalue" ) ; var lastValue = aaData [ aiDisplay [ iEnd - 1 ] ] [ 0 ] ; if ( lastValue && lastValue . toLowerCase ( ) == groupValue && iEnd < aiDisplay . length && aaData [ aiDisplay [ iEnd ] ] [ 0 ] && aaData [ aiDisplay [ iEnd ] ] [ 0 ] . toLowerCase ( ) == groupValue ) { groupTotalRow . hide ( ) ; } else { var groupCellsToTotal = new Array ( ) ; for ( var i = 0 ; i < aaData . length ; i ++ ) { var currentRow = aaData [ i ] ; var groupingValue ; var isRowObject = currentRow != null && typeof currentRow === 'object' && ! jQuery . isArray ( currentRow ) ; if ( isRowObject ) { groupingValue = currentRow [ 'c0' ] . val ; } else { groupingValue = currentRow [ 0 ] ; } if ( groupingValue != undefined && normalizeGroupString ( groupingValue ) . toLowerCase ( ) == groupValue ) { if ( isRowObject ) { groupCellsToTotal . push ( currentRow [ 'c' + columns [ c ] ] ) ; } else { groupCellsToTotal . push ( currentRow [ columns [ c ] ] ) ; } } } groupTotalRow . show ( ) ; calculateGroupTotal ( groupCellsToTotal , td , groupTotalDiv , rowIndex , columns [ c ] ) ; } if ( groupLabel . length && jQuery ( tds [ 0 ] ) . is ( ":empty" ) ) { groupLabel = groupLabel . clone ( ) ; groupLabel = groupLabel . attr ( "id" , groupLabel . attr ( "id" ) + "_" + rowIndex + columns [ c ] ) ; groupLabel . find ( "[id]" ) . each ( function ( ) { jQuery ( this ) . attr ( "id" , jQuery ( this ) . attr ( "id" ) + "_" + rowIndex + columns [ c ] ) ; } ) ; groupLabel . show ( ) ; jQuery ( tds [ 0 ] ) . append ( groupLabel ) ; } rowIndex ++ ; } ) ; } } ) ; } if ( hasTotalsInFooter ) { footerRow . show ( ) ; footerRow . find ( "th:hidden" ) . addClass ( "show-footer" ) ; } else { footerRow . hide ( ) ; } } }
Initialize/recalculate the totals placed in the footer of a richTable.
Initialize / recalculate the totals placed in the footer of a richTable .
//Only calculate totals if no grouping or when there is grouping, wait for those rows to become
 //generated - avoids unnecessary totalling
 // Total each column in the columns list
 //find the totalsBlocks in the column footer cell, and calculate the appropriate totals
 //for each group total row calculate the group total for the column we are totalling
 //This means if the group has data that goes into another page, do not display
 //the group total here - iEnd is the index-1 of the last displayed item (currently displayed)
 // in the display order list (aiDisplay)
 //check if row is a rowObject - using mData setting
 //copy the label to the first column if a group left label exists
 //resetting ids to unique ids on the clone
 //Hide the footer row if no footer totals or page totals exist
