1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-18 06:16:39 +02:00
TableFilter/doc/file/src/extensions/colOps/colOps.js.html
2015-08-02 18:27:59 +10:00

399 lines
16 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<base data-ice="baseUrl" href="../../../../">
<title data-ice="title">src/extensions/colOps/colOps.js | TableFilter v0.0.0 API Document</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
<link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
<script src="script/prettify/prettify.js"></script>
</head>
<body class="layout-container">
<header>
<a href="./">Home</a>
<a href="identifiers.html">Identifier</a>
<a href="source.html">Source</a>
<a data-ice="repoURL" href="https://github.com/koalyptus/TableFilter.git" class="repo-url-github">Repository</a>
<div class="search-box">
<span>
<img src="./image/search.png">
<span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
</span>
<ul class="search-result"></ul>
</div>
</header>
<nav class="navigation" data-ice="nav"><div data-ice="classWrap">
<h2>Class</h2>
<ul>
<li data-ice="classDoc"><span><a href="class/src/extensions/advancedGrid/adapterEzEditTable.js~AdapterEzEditTable.html">AdapterEzEditTable</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/extensions/sort/adapterSortabletable.js~AdapterSortableTable.html">AdapterSortableTable</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/alternateRows.js~AlternateRows.html">AlternateRows</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/checkList.js~CheckList.html">CheckList</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/clearButton.js~ClearButton.html">ClearButton</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/extensions/colOps/colOps.js~ColOps.html">ColOps</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/extensions/colsVisibility/colsVisibility.js~ColsVisibility.html">ColsVisibility</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/dropdown.js~Dropdown.html">Dropdown</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/extensions/filtersVisibility/filtersVisibility.js~FiltersVisibility.html">FiltersVisibility</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/gridLayout.js~GridLayout.html">GridLayout</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/help.js~Help.html">Help</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/highlightKeywords.js~HighlightKeyword.html">HighlightKeyword</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/loader.js~Loader.html">Loader</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/paging.js~Paging.html">Paging</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/popupFilter.js~PopupFilter.html">PopupFilter</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/rowsCounter.js~RowsCounter.html">RowsCounter</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/statusBar.js~StatusBar.html">StatusBar</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/modules/store.js~Store.html">Store</a></span></li>
<li data-ice="classDoc"><span><a href="class/src/tablefilter.js~TableFilter.html">TableFilter</a></span></li>
</ul>
</div>
</nav>
<div class="content" data-ice="content"><h1 data-ice="title">src/extensions/colOps/colOps.js</h1>
<pre class="source-code line-number"><code class="prettyprint linenums" data-ice="content">import Dom from &apos;../../dom&apos;;
import Str from &apos;../../string&apos;;
import Types from &apos;../../types&apos;;
export default class ColOps{
/**
* Column calculations
* @param {Object} tf TableFilter instance
*/
constructor(tf, opts) {
//calls function before col operation
this.onBeforeOperation = Types.isFn(opts.on_before_operation) ?
opts.on_before_operation : null;
//calls function after col operation
this.onAfterOperation = Types.isFn(opts.on_after_operation) ?
opts.on_after_operation : null;
this.opts = opts;
this.tf = tf;
}
init(){
this.calc();
}
/**
* Calculates columns&apos; values
* Configuration options are stored in &apos;opts&apos; property
* - &apos;id&apos; contains ids of elements showing result (array)
* - &apos;col&apos; contains the columns&apos; indexes (array)
* - &apos;operation&apos; contains operation type (array, values: &apos;sum&apos;, &apos;mean&apos;,
* &apos;min&apos;, &apos;max&apos;, &apos;median&apos;, &apos;q1&apos;, &apos;q3&apos;)
* - &apos;write_method&apos; array defines which method to use for displaying the
* result (innerHTML, setValue, createTextNode) - default: &apos;innerHTML&apos;
* - &apos;tot_row_index&apos; 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.
*/
calc() {
var tf = this.tf;
if(!tf.isFirstLoad &amp;&amp; !tf.hasGrid()){
return;
}
if(this.onBeforeOperation){
this.onBeforeOperation.call(null, tf);
}
var opts = this.opts,
labelId = opts.id,
colIndex = opts.col,
operation = opts.operation,
outputType = opts.write_method,
totRowIndex = opts.tot_row_index,
excludeRow = opts.exclude_row,
decimalPrecision = Types.isUndef(opts.decimal_precision) ?
2 : opts.decimal_precision;
//nuovella: determine unique list of columns to operate on
var ucolIndex = [],
ucolMax = 0;
ucolIndex[ucolMax] = colIndex[0];
for(var ii=1; ii&lt;colIndex.length; ii++){
var saved = 0;
//see if colIndex[ii] is already in the list of unique indexes
for(var jj=0; jj&lt;=ucolMax; jj++){
if(ucolIndex[jj] === colIndex[ii]){
saved = 1;
}
}
//if not saved then, save the index;
if (saved === 0){
ucolMax++;
ucolIndex[ucolMax] = colIndex[ii];
}
}
if(Str.lower(typeof labelId)==&apos;object&apos; &amp;&amp;
Str.lower(typeof colIndex)==&apos;object&apos; &amp;&amp;
Str.lower(typeof operation)==&apos;object&apos;){
var rows = tf.tbl.rows,
colvalues = [];
for(var ucol=0; ucol&lt;=ucolMax; ucol++){
//this retrieves col values
//use ucolIndex because we only want to pass through this loop
//once for each column get the values in this unique column
colvalues.push(
tf.getColValues(ucolIndex[ucol], true, excludeRow));
//next: calculate all operations for this column
var result,
nbvalues=0,
temp,
meanValue=0,
sumValue=0,
minValue=null,
maxValue=null,
q1Value=null,
medValue=null,
q3Value=null,
meanFlag=0,
sumFlag=0,
minFlag=0,
maxFlag=0,
q1Flag=0,
medFlag=0,
q3Flag=0,
theList=[],
opsThisCol=[],
decThisCol=[],
labThisCol=[],
oTypeThisCol=[],
mThisCol=-1;
for(var k=0; k&lt;colIndex.length; k++){
if(colIndex[k] === ucolIndex[ucol]){
mThisCol++;
opsThisCol[mThisCol]=Str.lower(operation[k]);
decThisCol[mThisCol]=decimalPrecision[k];
labThisCol[mThisCol]=labelId[k];
oTypeThisCol = outputType !== undefined &amp;&amp;
Str.lower(typeof outputType)===&apos;object&apos; ?
outputType[k] : null;
switch(opsThisCol[mThisCol]){
case &apos;mean&apos;:
meanFlag=1;
break;
case &apos;sum&apos;:
sumFlag=1;
break;
case &apos;min&apos;:
minFlag=1;
break;
case &apos;max&apos;:
maxFlag=1;
break;
case &apos;median&apos;:
medFlag=1;
break;
case &apos;q1&apos;:
q1Flag=1;
break;
case &apos;q3&apos;:
q3Flag=1;
break;
}
}
}
for(var j=0; j&lt;colvalues[ucol].length; j++){
//sort the list for calculation of median and quartiles
if((q1Flag==1)|| (q3Flag==1) || (medFlag==1)){
if (j&lt;colvalues[ucol].length -1){
for(k=j+1; k&lt;colvalues[ucol].length; k++) {
if(eval(colvalues[ucol][k]) &lt;
eval(colvalues[ucol][j])){
temp = colvalues[ucol][j];
colvalues[ucol][j] = colvalues[ucol][k];
colvalues[ucol][k] = temp;
}
}
}
}
var cvalue = parseFloat(colvalues[ucol][j]);
theList[j] = parseFloat(cvalue);
if(!isNaN(cvalue)){
nbvalues++;
if(sumFlag===1 || meanFlag===1){
sumValue += parseFloat( cvalue );
}
if(minFlag===1){
if(minValue===null){
minValue = parseFloat( cvalue );
} else{
minValue = parseFloat( cvalue ) &lt; minValue ?
parseFloat( cvalue ): minValue;
}
}
if(maxFlag===1){
if (maxValue===null){
maxValue = parseFloat( cvalue );
} else {
maxValue = parseFloat( cvalue ) &gt; maxValue ?
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;
}
}
var posa;
if(q1Flag===1){
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){
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&lt;=mThisCol; i++){
switch( opsThisCol[i] ){
case &apos;mean&apos;:
result=meanValue;
break;
case &apos;sum&apos;:
result=sumValue;
break;
case &apos;min&apos;:
result=minValue;
break;
case &apos;max&apos;:
result=maxValue;
break;
case &apos;median&apos;:
result=medValue;
break;
case &apos;q1&apos;:
result=q1Value;
break;
case &apos;q3&apos;:
result=q3Value;
break;
}
var precision = !isNaN(decThisCol[i]) ? decThisCol[i] : 2;
//if outputType is defined
if(oTypeThisCol &amp;&amp; result){
result = result.toFixed( precision );
if(Dom.id(labThisCol[i])){
switch( Str.lower(oTypeThisCol) ){
case &apos;innerhtml&apos;:
if (isNaN(result) || !isFinite(result) ||
nbvalues===0){
Dom.id(labThisCol[i]).innerHTML = &apos;.&apos;;
} else{
Dom.id(labThisCol[i]).innerHTML= result;
}
break;
case &apos;setvalue&apos;:
Dom.id( labThisCol[i] ).value = result;
break;
case &apos;createtextnode&apos;:
var oldnode = Dom.id(labThisCol[i])
.firstChild;
var txtnode = Dom.text(result);
Dom.id(labThisCol[i])
.replaceChild(txtnode, oldnode);
break;
}//switch
}
} else {
try{
if(isNaN(result) || !isFinite(result) ||
nbvalues===0){
Dom.id(labThisCol[i]).innerHTML = &apos;.&apos;;
} else {
Dom.id(labThisCol[i]).innerHTML =
result.toFixed(precision);
}
} catch(e) {}//catch
}//else
}//for i
// row(s) with result are always visible
var totRow = totRowIndex &amp;&amp; totRowIndex[ucol] ?
rows[totRowIndex[ucol]] : null;
if(totRow){
totRow.style.display = &apos;&apos;;
}
}//for ucol
}//if typeof
if(this.onAfterOperation){
this.onAfterOperation.call(null, tf);
}
}
destroy(){}
}
</code></pre>
</div>
<footer class="footer">
Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.1.4)</span></a>
</footer>
<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>