📄 zpgrid-aggregate.js
字号:
* input object. * </pre> * * @private * @param {object} oArg Arguments object */Zapatec.Grid.aggregateCount = function(oArg) { // Get grid var oGrid = oArg.grid; // Get number of rows var iRows = oArg.rows.length; // Set count for (var iCol = 0; iCol < oArg.column.length; iCol++) { oArg.totals[oArg.column[iCol]] = iRows; }};/** * Calculates count of distinct values 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.countDistinct = function(oArg) { oArg.aggregate = Zapatec.Grid.aggregateCountDistinct; if (typeof oArg.label == 'undefined') { oArg.label = 'Distinct:' } return Zapatec.Grid.aggregate(oArg);};/** * Internal aggregate callback to get count of distinct values 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 distinct values 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.aggregateCountDistinct = function(oArg) { // Get grid var oGrid = oArg.grid; // Prepare buffer var aDistinct = []; for (var iCol = 0; iCol < oArg.column.length; iCol++) { aDistinct[iCol] = {}; } // Count distinct for (var iRow = 0; iRow < oArg.rows.length; iRow++) { var oRow = oArg.rows[iRow]; for (var iCol = 0; iCol < oArg.column.length; iCol++) { var iColId = oArg.column[iCol]; var sVal = oGrid.getCellValueString(oGrid.getCellByRow(oRow, iColId)); // Check distinct if (typeof aDistinct[iCol][sVal] == 'undefined') { aDistinct[iCol][sVal] = true; oArg.totals[iColId]++; } } }};/** * Returns array of rows for totals section. * * @return Array of rows for totals section * @type object */Zapatec.Grid.prototype.getTotals = function() { // Check if there are totals var aTotalsRules = this.totalsRules; var iTotalsRules = aTotalsRules.length; // Calculate totals var aTotals = []; for (var iTotal = 0; iTotal < iTotalsRules; iTotal++) { var oTotal = aTotalsRules[iTotal]; // Get callback if (typeof oTotal.callback != 'function') { oTotal.callback = Zapatec.Grid.sum; } // Calculate rows var oResult = oTotal.callback({ grid: this, rows: this.getFilteredRows(), column: oTotal.column, label: oTotal.label, labelColumn: oTotal.labelColumn }); // Add rows if (oResult && (oResult.rows instanceof Array)) { for (var iRow = 0; iRow < oResult.rows.length; iRow++) { aTotals.push(oResult.rows[iRow]); } } } return aTotals;};/** * Forms grid output. * * @private * @param {object} aHtml Output array * @param {object} aCols Array with field objects to output * @param {object} aFixedCols Array with fixed field objects to output * @param {object} aRows Array with row objects to output * @param {boolean) bFixed Optional. Indicates that this is fixed part of table */Zapatec.Grid.prototype.outputTotals = function(aHtml, aCols, aFixedCols, aRows, bFixed) { if (typeof this.config.callbackTotalsDisplay == 'function' || typeof this.config.callbackTotalDisplay == 'function' || !aRows) { return; } if (this.config.horizontal) { // Horizontal layout this.outputTotalsHoriz(aHtml, aCols, aFixedCols, aRows, bFixed); } else { // Vertical layout this.outputTotalsVert(aHtml, aCols, aFixedCols, aRows, bFixed); }};/** * Forms grid output. * * @private * @param {object} aHtml Output array * @param {object} aCols Array with field objects to output * @param {object} aFixedCols Array with fixed field objects to output * @param {object} aRows Array with row objects to output * @param {boolean) bFixed Optional. Indicates that this is fixed part of table */Zapatec.Grid.prototype.outputTotalsHoriz = function(aHtml, aCols, aFixedCols, aRows, bFixed) { if (bFixed) { aCols = aFixedCols; } for (var iCol = 0; iCol < aCols.length; iCol++) { // Get field object var oField = aCols[iCol]; var aCl = []; aCl.push('zpGridCol zpGridCol'); aCl.push(iCol); if (iCol % 2 == 1) { aCl.push(' zpGridColOdd'); } else { aCl.push(' zpGridColEven'); } if (iCol == aCols.length - 1) { aCl.push(' zpGridColLast'); } var sClass = aCl.join(''); var aTr = []; aTr.push('<tr id="zpGrid'); aTr.push(this.id); aTr.push('TotalCol'); aTr.push(oField.i); if (bFixed) { aTr.push('Fixed'); } aTr.push('" class="'); aTr.push(sClass); aTr.push('">'); // Output rows if (bFixed) { for (var iRow = 0; iRow < aRows.length; iRow++) { this.outputTotalsCell(aTr, oField, aRows[iRow], iCol, iRow, aRows.length); } } else { for (var iRow = 0; iRow < aRows.length; iRow++) { var bHidden = (iCol < aFixedCols.length); this.outputTotalsCell(aTr, oField, aRows[iRow], iCol, iRow, aRows.length, bHidden); } } aTr.push('</tr>'); aHtml.push(aTr.join('')); }};/** * Forms grid output. * * @private * @param {object} aHtml Output array * @param {object} aCols Array with field objects to output * @param {object} aFixedCols Array with fixed field objects to output * @param {object} aRows Array with row objects to output * @param {boolean) bFixed Optional. Indicates that this is fixed part of row */Zapatec.Grid.prototype.outputTotalsVert = function(aHtml, aCols, aFixedCols, aRows, bFixed) { for (var iRow = 0; iRow < aRows.length; iRow++) { // Get row var oRow = aRows[iRow]; if (!oRow) { return; } var aCl = []; aCl.push('zpGridRow zpGridRowTotals zpGridRowTotals'); aCl.push(iRow); aCl.push(' zpGridRowTotals'); aCl.push(iRow % 2 == 1 ? 'Odd' : 'Even'); if (iRow == aRows.length - 1) { aCl.push(' zpGridRowLast zpGridRowTotalsLast'); } var sClass = aCl.join(''); var aTr = []; aTr.push('<tr id="zpGrid'); aTr.push(this.id); aTr.push('Total'); aTr.push(iRow); if (bFixed) { aTr.push('Fixed'); } aTr.push('" class="'); aTr.push(sClass); aTr.push('" style="'); if (oRow.style) { aTr.push(oRow.style); } aTr.push('">'); // Display cells if (bFixed) { for (var iCol = 0; iCol < aFixedCols.length; iCol++) { this.outputTotalsCell(aTr, aFixedCols[iCol], oRow, iRow, iCol, aCols.length); } } else { for (var iCol = 0; iCol < aCols.length; iCol++) { var bHidden = (iCol < aFixedCols.length); this.outputTotalsCell(aTr, aCols[iCol], oRow, iRow, iCol, aCols.length, bHidden); } } aTr.push('</tr>'); aHtml.push(aTr.join('')); }};/** * Forms grid output. * * @private * @param {object} aTr Output array * @param {object} oField Field object * @param {object} oRow Row object * @param {object} iRow Number of row on the page * @param {number} iCol Visible column number * @param {number} iCols Visible column count * @param {boolean} bHidden Optional. Indicates that this is hidden part of * fixed part of row */Zapatec.Grid.prototype.outputTotalsCell = function(aTr, oField, oRow, iRow, iCol, iCols, bHidden) { if (iCol >= this.config.fixedLeft && iCol < this.currentHorizontalOffset + this.config.fixedLeft) { // Hidden column return; } if (this.config.visibleColumns) { bHidden = false; } // Get cell var oCell = oRow.cells[oField.i]; if (!oCell) { return; } var aCl = []; aCl.push('zpGridCell zpGridCell'); aCl.push(iCol); aCl.push(' zpGridColId'); aCl.push(oField.i); aCl.push(' zpGridCellData zpGridCellData'); aCl.push(iCol); aCl.push(' zpGridCellTotals zpGridCellTotals'); aCl.push(iCol); if (iCol % 2 == 1) { aCl.push(' zpGridCellOdd zpGridCellDataOdd zpGridCellTotalsOdd'); } else { aCl.push(' zpGridCellEven zpGridCellDataEven zpGridCellTotalsEven'); } if (iCol + 1 == iCols) { aCl.push(' zpGridCellLast zpGridCellDataLast zpGridCellTotalsLast'); } var sClass = aCl.join(''); var aTd = []; aTd.push('<td class="'); aTd.push(sClass); aTd.push('" id="zpGrid'); aTd.push(this.id); aTd.push('Total'); if (this.config.horizontal) { aTd.push(iCol); } else { aTd.push(iRow); } aTd.push('Cell'); aTd.push(oField.i); if (bHidden) { aTd.push('Hidden'); } var sStyle = this.getCellStyle(oCell, iRow); if (sStyle) { aTd.push('" style="'); aTd.push(sStyle); } aTd.push('">'); this.outputTotalsCellValue(aTd, oField, oRow, oCell); aTd.push('</td>'); aTr.push(aTd.join(''));};/** * Forms grid output. * * @private * @param {object} aTd Output array * @param {object} oField Field object * @param {object} oRow Row object * @param {object} oCell Cell object */Zapatec.Grid.prototype.outputTotalsCellValue = function(aTd, oField, oRow, oCell) { // div is needed to be able to set column width aTd.push('<div class="zpGridDiv'); // Set cell type if (this.getClassByType) { aTd.push(' '); aTd.push(this.getClassByType(this.getFieldType(oField))); } aTd.push('">'); var sData = oCell.v + ''; // Empty cell may cause visual problems in horizontal layout if (!sData || !sData.length) { sData = ' '; } aTd.push(sData); aTd.push('</div>');};/** * Redraws totals for the specified columns. * * <pre> * Takes in following object: * { * column: [object or number, optional] array of zero-based column numbers or * single zero-based column number to redraw (default is all columns) * } * </pre> * * @param {object} oArg Arguments object */Zapatec.Grid.prototype.redrawTotals = function(oArg) { // If we are responsible for visualisation if (this.visualize) { // Get totals var aTotals = this.getTotals(); if (!aTotals) { return; } // Get fields var aFields = this.getFields(); // Redraw cells var iGridId = this.id; for (var iTotal = 0; iTotal < aTotals.length; iTotal++) { var oRow = aTotals[iTotal]; for (var iField = 0; iField < aFields.length; iField++) { // Get table cell element var sCellId = 'zpGrid' + iGridId + 'Total' + iTotal + 'Cell' + iField; var oTd = document.getElementById(sCellId); if (oTd) { // Redraw cell var aTd = []; this.outputCellValue(aTd, aFields[iField], oRow.cells[iField]); var sTd = aTd.join(''); oTd.innerHTML = sTd; // Redraw hidden cell var oTdHidden = document.getElementById(sCellId + 'Hidden'); if (oTdHidden) { oTdHidden.innerHTML = sTd; } } } } }};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -