📄 zpgrid-aggregate.js
字号:
/** * @fileoverview Plugin for Zapatec Grid to calculate totals. * * <pre> * Copyright (c) 2004-2006 by Zapatec, Inc. * http://www.zapatec.com * 1700 MLK Way, Berkeley, California, * 94709, U.S.A. * All rights reserved. * </pre> *//* $Id: zpgrid-aggregate.js 7629 2007-07-31 09:44:52Z alex $ *//** * Gets called from aggregate functions to calculate totals for one or several * columns. * * <pre> * Takes in following object: * { * aggregate: [function] one of internal aggregate callbacks, * grid: [object] grid object, * rows: [object] array of row objects, * column: [object or number] array of zero-based column numbers or single * zero-based column number to calculate, * label: [string, optional] label for the value, * labelColumn: [string, optional] zero-based column number where to put label * (by default label is placed before the value in the same cell) * } * * Returns following object: * { * rows: * [ * { * cells: * [ * { * v: [string] value to display * }, * ... * ] * } * ] * } * </pre> * * @private * @param {object} oArg Arguments object * @return Row with totals * @type object */Zapatec.Grid.aggregate = function(oArg) { // Check arguments if (!oArg || typeof oArg.aggregate != 'function') { return; } // Get grid var oGrid = oArg.grid; if (!(oGrid instanceof Zapatec.Grid)) { return; } // Get fields var aFields = oGrid.getFields(); // Get columns var aColumns = []; oArg.totals = []; if (oArg.column instanceof Array) { // Multiple columns for (var iCol = 0; iCol < oArg.column.length; iCol++) { var iColId = oArg.column[iCol]; if (typeof aFields[iColId] != 'undefined') { aColumns.push(iColId); oArg.totals[iColId] = 0; } } } else if (typeof aFields[oArg.column] != 'undefined') { // Single column aColumns.push(oArg.column); oArg.totals[oArg.column] = 0; } if (!aColumns.length) { return; } oArg.column = aColumns; // Calculate totals if ((oArg.rows instanceof Array) && oArg.rows.length) { oArg.aggregate(oArg); } // Get label column var iLabelColumn; if (typeof oArg.labelColumn != 'undefined' && typeof aFields[oArg.labelColumn] != 'undefined') { iLabelColumn = oArg.labelColumn; } // Form prefix var sPrefix = ''; if (typeof iLabelColumn == 'undefined' && oArg.label) { sPrefix = oArg.label + ' '; } // Form result oResult = { rows: [ { cells: [] } ] }; for (var iCol = 0; iCol < aFields.length; iCol++) { var val = oArg.totals[iCol]; if (typeof val == 'undefined') { val = ''; } else { val = sPrefix + val; } oResult.rows[0].cells.push({v: val}); } // Add label if (typeof iLabelColumn != 'undefined' && oArg.label) { oResult.rows[0].cells[iLabelColumn].v = oArg.label; } return oResult;};/** * Calculates sum for one or several columns. * * <pre> * Takes in following object: * { * grid: [object] grid object, * rows: [object] array of row objects, * column: [object or number] array of zero-based column numbers or single * zero-based column number to calculate, * label: [string, optional] label for the value (overrides standard label), * labelColumn: [string, optional] zero-based column number where to put label * (by default label is placed before the value in the same cell) * } * * Returns following object: * { * rows: * [ * { * cells: * [ * { * v: [string] value to display * }, * ... * ] * } * ] * } * </pre> * * @param {object} oArg Arguments object * @return Row with totals * @type object */Zapatec.Grid.sum = function(oArg) { oArg.aggregate = Zapatec.Grid.aggregateSum; // Default label if (typeof oArg.label == 'undefined') { oArg.label = 'Total:' } return Zapatec.Grid.aggregate(oArg);};/** * Internal aggregate callback to calculate sum of the column. Gets called from * {@link Zapatec.Grid#aggregate}. * * <pre> * Takes in following object: * { * totals: [object] array having as many members as there are fields in the * grid and initializied with 0 for each column to calculate; 0 will be * replaced with sum of the column by this function, * grid: [object] grid object, * rows: [object] array of row objects, * column: [object] array of zero-based column numbers to calculate * } * * Instead of returning result, modifies array passed with "totals" property of * input object. * </pre> * * @private * @param {object} oArg Arguments object */Zapatec.Grid.aggregateSum = function(oArg) { // Get grid var oGrid = oArg.grid; // Calculate sum and get precisions var aPrecisions = oArg.totals.slice(); var aRows = oArg.rows; var iRows = aRows.length; var aColumns = oArg.column; var iColumns = aColumns.length; for (var iRow = 0; iRow < iRows; iRow++) { var oRow = aRows[iRow]; for (var iCol = 0; iCol < iColumns; iCol++) { var iColId = aColumns[iCol]; // Get cell value var val = oGrid.getCellValueOriginal(oGrid.getCellByRow(oRow, iColId)); // Convert to number var iVal = val * 1; if (!isNaN(iVal)) { // Save precision var iPrecision = Zapatec.Utils.getPrecision(val); if (iPrecision > aPrecisions[iColId]) { aPrecisions[iColId] = iPrecision; } // Add to the sum and correct precision of result oArg.totals[iColId] = Zapatec.Utils.setPrecision(oArg.totals[iColId] + iVal, aPrecisions[iColId]); } } } // Correct precisions for (var iCol = 0; iCol < iColumns; iCol++) { var iColId = aColumns[iCol]; oArg.totals[iColId] = Zapatec.Utils.setPrecisionString(oArg.totals[iColId], aPrecisions[iColId]); }};/** * Calculates average for one or several columns. Input and output object format * is the same as for {@link Zapatec.Grid#sum}. * * @param {object} oArg Arguments object * @return Row with totals * @type object */Zapatec.Grid.avg = function(oArg) { oArg.aggregate = Zapatec.Grid.aggregateAvg; // Default label if (typeof oArg.label == 'undefined') { oArg.label = 'AVG:' } return Zapatec.Grid.aggregate(oArg);};/** * Internal aggregate callback to calculate average of the column. Gets called * from {@link Zapatec.Grid#aggregate}. * * <pre> * Takes in following object: * { * totals: [object] array having as many members as there are fields in the * grid and initializied with 0 for each column to calculate; 0 will be * replaced with average of the column by this function, * grid: [object] grid object, * rows: [object] array of row objects, * column: [object] array of zero-based column numbers to calculate * } * * Instead of returning result, modifies array passed with "totals" property of * input object. * </pre> * * @private * @param {object} oArg Arguments object */Zapatec.Grid.aggregateAvg = function(oArg) { // Get sum Zapatec.Grid.aggregateSum(oArg); // Get number of rows var iRows = oArg.rows.length; // Get precision and calculate average for (var iCol = 0; iCol < oArg.column.length; iCol++) { var iColId = oArg.column[iCol]; var iPrecision = Zapatec.Utils.getPrecision(oArg.totals[iColId]); var fAvg = oArg.totals[iColId] / iRows; oArg.totals[iColId] = Zapatec.Utils.setPrecisionString(fAvg, iPrecision); }};/** * Calculates min value for one or several columns. Input and output object * format is the same as for {@link Zapatec.Grid#sum}. * * @param {object} oArg Arguments object * @return Row with totals * @type object */Zapatec.Grid.min = function(oArg) { oArg.aggregate = Zapatec.Grid.aggregateCompare; oArg.sign = '<'; // Default label if (typeof oArg.label == 'undefined') { oArg.label = 'Min:' } return Zapatec.Grid.aggregate(oArg);};/** * Calculates max value for one or several columns. Input and output object * format is the same as for {@link Zapatec.Grid#sum}. * * @param {object} oArg Arguments object * @return Row with totals * @type object */Zapatec.Grid.max = function(oArg) { oArg.aggregate = Zapatec.Grid.aggregateCompare; oArg.sign = '>'; if (typeof oArg.label == 'undefined') { oArg.label = 'Max:' } return Zapatec.Grid.aggregate(oArg);};/** * Internal aggregate callback to calculate min or max of the column. Gets * called from {@link Zapatec.Grid#aggregate}. * * <pre> * Takes in following object: * { * totals: [object] array having as many members as there are fields in the * grid and initializied with 0 for each column to calculate; 0 will be * replaced with min or max of the column by this function, * sign: [string] sign to use, e.g. '<' to get min and '>' to get max, * grid: [object] grid object, * rows: [object] array of row objects, * column: [object] array of zero-based column numbers to calculate * } * * Instead of returning result, modifies array passed with "totals" property of * input object. * </pre> * * @private * @param {object} oArg Arguments object */Zapatec.Grid.aggregateCompare = function(oArg) { // Get grid var oGrid = oArg.grid; // Init totals var oRow = oArg.rows[0]; for (var iCol = 0; iCol < oArg.column.length; iCol++) { var iColId = oArg.column[iCol]; oArg.totals[iColId] = oGrid.getCellByRow(oRow, iColId); } // Find max cell for (var iRow = 0; iRow < oArg.rows.length; iRow++) { oRow = oArg.rows[iRow]; for (var iCol = 0; iCol < oArg.column.length; iCol++) { var iColId = oArg.column[iCol]; var oCell = oGrid.getCellByRow(oRow, iColId); // Compare cells if (eval('oGrid.getCellValueCompare(oCell)' + oArg.sign + 'oGrid.getCellValueCompare(oArg.totals[iColId])')) { oArg.totals[iColId] = oCell; } } } // Convert to values for (var iCol = 0; iCol < oArg.column.length; iCol++) { var iColId = oArg.column[iCol]; oArg.totals[iColId] = oGrid.getCellValue(oArg.totals[iColId]); }};/** * Calculates count for one or several columns. Input and output object format * is the same as for {@link Zapatec.Grid#sum}. * * @param {object} oArg Arguments object * @return Row with totals * @type object */Zapatec.Grid.count = function(oArg) { oArg.aggregate = Zapatec.Grid.aggregateCount; if (typeof oArg.label == 'undefined') { oArg.label = 'Count:' } return Zapatec.Grid.aggregate(oArg);};/** * Internal aggregate callback to get count of the column. Gets called from * {@link Zapatec.Grid#aggregate}. * * <pre> * Takes in following object: * { * totals: [object] array having as many members as there are fields in the * grid and initializied with 0 for each column to calculate; 0 will be * replaced with count of the column by this function, * grid: [object] grid object, * rows: [object] array of row objects, * column: [object] array of zero-based column numbers to calculate * } * * Instead of returning result, modifies array passed with "totals" property of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -