The library provides a list of statistics counters that can be used to get some total value in the footer of a column.
Statistics counters are set by the method attachFooter().
General specifying a statistics counter for a column
mygrid = new dhtmlXGridObject('gridbox');
mygrid.setHeader("Sales,Book Title,Author");
mygrid.attachFooter("#stat_average,,");
mygrid.attachFooter("#stat_max,,");
...
Counter | Description |
---|---|
stat_total | calculates the sum of all values in a column |
stat_multi_total | calculates the product of 2 columns totals |
stat_max | presents the maximum value in the column |
stat_min | presents the minimum value in the column |
stat_average | calculates the average of all values in a column |
stat_count | counts the number of rows in the column |
If you add the counter after loading data, you need to force calculation by the following command:
Forced invoking calculation in the grid
mygrid.callEvent("onGridReconstructed",[]);
You can easily create a custom counter using the technique shown below:
The simplest custom counter
mygrid = new dhtmlXGridObject('gridbox');
...
mygrid._in_header_stat_rowcount=function(tag,index,data){//'stat_rowcount'-counter name
var calc=function(){ // function used for calculations
return this.getRowsNum();
}
this._stat_in_header(tag,calc,index,data); // default statistics handler processor
}
mygrid.attachFooter("#stat_rowcount, , ");
mygrid.init()
Understanding the "data" parameter
mygrid = new dhtmlXGridObject('gridbox');
...
mygrid.attachFooter(" , ,text1{#stat_rowcount}text2");
mygrid.init()
// the '_in_header_stat_rowcount()' function takes parameter data=[text1,text2]
To make the counter available for any grid initialized in the app you should specify the counter globally, on the prototype level:
dhtmlXGridObject.prototype._in_header_stat_rowcount=function(tag,index,data){
var calc=function(){ // function used for calculations
return this.getRowsNum();
}
this._stat_in_header(tag,calc,index,data); // default statistics handler processor
}
To set the same numeric format for the counter as the one set for the related column, you may use the inner grid function _aplNF(value,column_index). In this case, the counter will take the format set by the setNumberFormat() method.
Setting the counter to the format of the column
mygrid = new dhtmlXGridObject('gridbox');
mygrid.setHeader("Sales, Book title, Author,Price");
mygrid.setNumberFormat("0,000.00",0);// sets the format for the 'Sales' column
mygrid._in_header_stat_sum=function(tag,index,data){
var calck=function(){
var sum=0;
this.forEachRow(function(id){
sum+=this.cellById(id,index).getValue()*1;
})
return this._aplNF(sum,0);
}
this._stat_in_header(tag,calck,index,data);
}
mygrid.attachFooter("#stat_sum, , ,");
...
mygrid.init();
You have the possibility to integrate the value return by any statictics counter into HTML.
Integrating a counter into HTML
<span id="average_sale"></span>
<div
id="gridbox"
style="margin-top:10px; width:379px;height:270px;background-color:white;"
></div>
<script>
mygrid = new dhtmlXGridObject('gridbox');
...
mygrid._in_header_stat_average(document.getElementById('total_sales'),3,
["<b>Average price per store: </b>","$"]);
</script>
mygrid._in_header_[name of the counter] takes the following parameters:
The function can be called both before and after grid initialization.
Related sample: Numeric filtering and master checkbox
Back to top