Author Topic: Totals for columns  (Read 6944 times)

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Totals for columns
« on: February 11, 2014, 06:33:47 pm »
Is it possible to calculate a total for certain columns without using grouping?

I have in colModel:

Code: [Select]
summary:{
            type:["sum"],
            title: ["<b style='font-weight:bold;'>Total:</b> {0}"]}
            },

Which doesn't show any total.

If I group the 3 columns for which I want totals, it does calculate the totals but it looks really weird since these are values columns (that shouldn't be grouped).

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Totals for columns
« Reply #1 on: February 12, 2014, 10:08:23 am »
If you need totals without using grouping, then this one is for you.

http://paramquery.com/pro/demos/summary


======================

It's not right to group by the same column for which you want summary. Please have a re-look at the grouping demo.





nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Totals for columns
« Reply #2 on: February 12, 2014, 05:39:14 pm »
My data is remote, therefore, I need to count the total number of rows in the table to create the loop.

Code: [Select]
var numRegistos=$('#tabela').pqGrid("option","totalRecords");
Is returning "null", therefore, no totals.

How can I count the total number of rows?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Totals for columns
« Reply #3 on: February 12, 2014, 07:51:31 pm »
Total number of rows available on client side is dataModel.data.length

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Totals for columns
« Reply #4 on: February 12, 2014, 09:08:31 pm »
Ok, so this returns the number of rows:

Code: [Select]
$('#tabela').pqGrid("option","dataModel.data.length");
So how do I sum the totals for a given row??

Using a loop seems ok but what kind of method/option shall I use?

This doesn't work:

Code: [Select]
for (var i = 0; i < numRegistos; i++) {
    orcamentoTotal += parseFloat($('#tabela').pqGrid("getCell",{rowIndx:i, dataIndx:"orcamento"}));
};

I read the API and searched for examples but I couldn't find anything.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Totals for columns
« Reply #5 on: February 12, 2014, 09:24:07 pm »
dataModel.data is an array of rows.

Code: [Select]
var data = $grid.pqGrid("option", "dataModel.data");

var sum = 0;
for(var i=0, len=data.length; i<len; i++){
    var rowData = data[i];
    sum += parseFloat( rowData["orcamento"] );
}

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Totals for columns
« Reply #6 on: February 12, 2014, 09:28:20 pm »
Ok, that's exaclty what I was looking for!

Thanks!
 :D