The question about subtotal row

Is there functionality or API to create a subtotal row for each group in dhtmlx grid ?Thanks.

If you mean grouped view ( grid.groupBy ) - there is no built in solution for subtotal rows.
You may try to use TreeGrid instead and use parent rows as subtotals


There are 16 rows of countable data.



I want to make one subtotal for each 4 row. The layout is shown below. Is there any method to make the subtotal??? Thanks



         sales     cost



-------------------------------------



           1          3



           1          3



           1          3



           1          3



-----------------------------------------------



subtotal :4        12



------------------------------------------------



           1         2



           1         2



           1         2



           1         2



-----------------------------------------------



subtotal :4      8



        

You can add custom styled rows and use in-grid math to calculate sub-totals.
Working sample sent by email


Thank you for  your answer.I try to add new custom row and i want to pass some parameter value into mygrid.addRow().



However, new added row’s cell values show variable name like subtotal_sales and subtotal_Cost rather than the variable value.



My coding is shown below. How can I pass some  parameter values into mygrid.addRow()?



 



function subtotal()



{




var subtotal_sales =sumColumn2(1)
var subtotal_Cost =sumColumn2(2)




mygrid.addRow(‘subtotal’,‘subtotal_sales,subtotal_Cost’ , 4 );





   }



function sumColumn2( ind){
        var out = 0;
        for(var i=0;i<4;i++){
            out+= parseFloat(mygrid.cells2(i,ind).getValue())
        }
        return out;
    }

mygrid.addRow(‘subtotal’,‘subtotal_sales,subtotal_Cost’ , 4 )
Must be
mygrid.addRow(‘subtotal’,subtotal_sales+","+subtotal_Cost, 4 )
or
mygrid.addRow(‘subtotal’,[subtotal_sales,subtotal_Cost], 4 )


Thank you for the support teams’ samples. So I can make this subtotal. Now I want to use the method of mygrid.attachFooter mygrid.attachFooter(’ Total,{#stat_total}/2,  {#stat_total}/2 , {#stat_total}/2 ')  to calculate the total.  However, I cannot divide the value of {#stat_total} by 2.



 



       sales     cost



-------------------------------------



           1          3



           1          3



           1          3



           1          3



-----------------------------------------------



subtotal :4        12



------------------------------------------------



           1         2



           1         2



           1         2



           1         2



-----------------------------------------------



subtotal :4      8



 



Total:    8/2        40/2 <=================== should display   Total:   4        20

Grid doesn’t provide such a functionality. But you can try to use one of the following approaches:

1) there is #stat_multi_total which allows to get a sum of products of two columns. And it for example some column in grid is hidden and each cell in it is equal to 0.5 you can do the following instead of {#stat_total}/2:

${#stat_multi_total}index1:index2

Where index1 - the index of the column where values are placed, and index2 is the index of the hidden column
2) also you can use the next approach:
grid.attachEvent(“onStatReady”,function(){

grid.ftr.rows[1].cells[index].innerHTML = Math.round(parseFloat(grid.ftr.rows[1].cells[index].innerHTML)/2;

})
There “index” is the index of the column with {#stat_total}