/*------------------------------------------------------------------------ - HTML Table Filter Generator - Columns Operations feature v1.0 - By Max Guglielmi (tablefilter.free.fr) - Licensed under the MIT License - Special credit to Nuovella Williams -------------------------------------------------------------------------*/ TF.prototype.SetColOperation = function() /*==================================================== - Calculates values of a column - params are stored in 'colOperation' table's attribute - colOperation['id'] contains ids of elements showing result (array) - colOperation['col'] contains index of columns (array) - colOperation['operation'] contains operation type (array, values: sum, mean) - colOperation['write_method'] array defines which method to use for displaying the result (innerHTML, setValue, createTextNode). Note that innerHTML is the default value. - colOperation['tot_row_index'] defines in which row results are displayed (integers array) - changes made by nuovella: (1) optimized the routine (now it will only process each column once), (2) added calculations for the median, lower and upper quartile. =====================================================*/ { if( !this.isFirstLoad && !this.hasGrid ) return; if(this.onBeforeOperation) this.onBeforeOperation.call(null,this); var labelId = this.colOperation['id']; var colIndex = this.colOperation['col']; var operation = this.colOperation['operation']; var outputType = this.colOperation['write_method']; var totRowIndex = this.colOperation['tot_row_index']; var excludeRow = this.colOperation['exclude_row']; var decimalPrecision = this.colOperation['decimal_precision']!=undefined ? this.colOperation['decimal_precision'] : 2; //nuovella: determine unique list of columns to operate on var ucolIndex =[]; var ucolMax=0; ucolIndex[ucolMax]=colIndex[0]; for(var i=1; imaxValue? parseFloat( cvalue ): maxValue;} } } }//for j if (meanFlag==1) meanValue = sumValue/nbvalues; if (medFlag==1) { var aux = 0; if(nbvalues%2 == 1) { aux = Math.floor(nbvalues/2); medValue = theList[aux]; } else medValue = (theList[nbvalues/2]+theList[((nbvalues/2)-1)])/2; } if (q1Flag==1) { var posa=0.0; posa = Math.floor(nbvalues/4); if (4*posa == nbvalues) {q1Value = (theList[posa-1] + theList[posa])/2;} else {q1Value = theList[posa];} } if (q3Flag==1) { var posa=0.0; var posb=0.0; posa = Math.floor(nbvalues/4); if (4*posa == nbvalues) { posb = 3*posa; q3Value = (theList[posb] + theList[posb-1])/2; } else q3Value = theList[nbvalues-posa-1]; } for(var i=0; i<=mThisCol; i++ ) { switch( opsThisCol[i] ) { case 'mean': result=meanValue; break; case 'sum': result=sumValue; break; case 'min': result=minValue; break; case 'max': result=maxValue; break; case 'median': result=medValue; break; case 'q1': result=q1Value; break; case 'q3': result=q3Value; break; } var precision = decThisCol[i]!=undefined && !isNaN( decThisCol[i] ) ? decThisCol[i] : 2; if(oTypeThisCol!=null && result) {//if outputType is defined result = result.toFixed( precision ); if( tf_Id( labThisCol[i] )!=undefined ) { switch( oTypeThisCol.tf_LCase() ) { case 'innerhtml': if (isNaN(result) || !isFinite(result) || (nbvalues==0)) tf_Id( labThisCol[i] ).innerHTML = '.'; else tf_Id( labThisCol[i] ).innerHTML = result; break; case 'setvalue': tf_Id( labThisCol[i] ).value = result; break; case 'createtextnode': var oldnode = tf_Id( labThisCol[i] ).firstChild; var txtnode = tf_CreateText( result ); tf_Id( labThisCol[i] ).replaceChild( txtnode,oldnode ); break; }//switch } } else { try { if (isNaN(result) || !isFinite(result) || (nbvalues==0)) tf_Id( labThisCol[i] ).innerHTML = '.'; else tf_Id( labThisCol[i] ).innerHTML = result.toFixed( precision ); } catch(e){ }//catch }//else }//for i //eventual row(s) with result are always visible if(totRowIndex!=undefined && row[totRowIndex[ucol]]) row[totRowIndex[ucol]].style.display = ''; }//for ucol }//if typeof if(this.onAfterOperation) this.onAfterOperation.call(null,this); }