Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by diego ogniben on Oct 16, 2009 09:38
open dhtmlx forum
groupBy and stat functions

Hi, using the groupBy method:

mygrid.groupBy(1,["","#title", "#stat_min"]);

in my grid with date values in the 3rd column, i get NaN as a result.
Does it work only with numbers?
I need to get mins and max of the dates in various groups, is this possible?

Thx

Answer posted by dhxSupport on Oct 19, 2009 01:51
>>Does it work only with numbers?
Yep, it works only with numbers. But you can create custom statistics counter which will work with dates. Please find more information here http://dhtmlx.com/dhxdocs/doku.php?id=dhtmlxgrid:implementing_new_types_of_statistics_counters
Answer posted by diego ogniben on Oct 19, 2009 02:57
Ehmm, if i try to define the custom function following your sample:

  mygrid._in_header_stat_mindate=function(tag,index,c){
      var calck=function(){
          var maxdate=0; //set initial
          this.forEachRow(function(id){
              alert(this.cells(id,index).getValue());
              //here i should put dates comparison
          })
          return maxdate;
      }
  this._stat_in_header(tag,calck,index,c);
  }

and then use:

mygrid.groupBy(1,["","#title", "#stat_mindate"]);


i get:

"a is not a function"   in: dhtmlXGridObject.prototype._b_processing=function(a,ind,rind){...}


Did I miss something?


Answer posted by Stanislav (support) on Oct 19, 2009 06:56
The previously suggested solution is the one for normal grid's , it not applicable for groupBy mode. 
The next code can be used in groupBy to get min-date ( sample code based on assuming that all dates is in common format ) 


mygrid._g_stat_mindate = function(c,n,i){
   if (!i) c=new Date(9999,1,1);
   n = new Date(Date.parse(n));

   var res = (n<c?n:c);
   res.toString = function(){ return this.getDate()+"/"+(this.getMonth()+1)+"/"+this.getFullYear(); }
   return res;  
}

mygrid.groupBy(1,["","#title", "#stat_mindate"]);


Where res.toString line defines format of final text in the grid. 
Also, to have above code works correctly, you need to change dhtmlxgrid_group.js 
   c=a(c,this.cells3(this.rowsCol[i],ind).getValue()*1,j);
to the
   c=a(c,this.cells3(this.rowsCol[i],ind).getValue(),j);
Answer posted on Oct 19, 2009 07:51
It works, thanks so much!
The result appears in the attached picure.
One last question (i promise :) ): is it possible to have the value of the column "Est. Dur." to be the difference of the previous
2 columns  (24/10/2009 - 15/10/2009)  ?




Attachments (1)
grid.jpg31.44 Kb
Answer posted by Stanislav (support) on Oct 19, 2009 09:59
The code similar to the next can be used to implement such use-case

mygrid.groupBy(...some configuration...)
for (var text in mygrid._groups){
        var row = mygrid._groups[text].row;
        var start = mygrid.cells3(row,3).getValue();
        var end = mygrid.cells3(row,4).getValue();
        var diff = some_magic_method(start, end);

        mygrid.cells3(row,5).setValue(diff);
}


Answer posted by diego ogniben on Oct 22, 2009 01:11
Thanks for the hint, however if i do that or even if i do:

mygrid.attachEvent("onGroup", function(){
      for (var text in mygrid._groups){
        alert(text);
        }
      }
);
mygrid.groupBy(...);


i never reach the alert(text) row. Am i missing something obvious?

Answer posted by Stanislav (support) on Oct 22, 2009 04:19
Are you calling group-by before or after loading data in grid?
If grid was grouped before data loading, onGroup event occurs when there is no data - so no any group, and code will not be triggered.
Answer posted by diego ogniben on Oct 22, 2009 08:50
You were right, obviously!
However the code breaks at your last line:

mygrid.cells3(row,5).setValue(diff);


just after setting the right value in the 5th column for the first group.
The error it throws is : "this.grid is undefined" at the following line (taken from firebug):

dhtmlXGridCellObject.prototype.setCValue=function(val, val2){this.cell.innerHTML=val;this.grid.callEvent("onCellChanged", [....
Answer posted by Stanislav (support) on Oct 23, 2009 09:38
As fast solution, you can change the code as

row.grid = mygrid;
mygrid.cells3(row,5).setValue(diff);

Answer posted by diego ogniben on Oct 26, 2009 06:41
Thanks a lot, the goal has been reached. I like this grid so much.