Author Topic: Subtotals calculation  (Read 7805 times)

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Subtotals calculation
« on: March 11, 2014, 05:43:02 pm »
I'm using 'summary' to calculate subtotals as per your demo.

It seems, however, that when one of the values is zero or a calculated column, the subtotal isn't calculted. See Yellow marks in the attachment.

Also, I tried to add a different style to the group rows in my CSS, like:
Code: [Select]
.pq-group-row {
background-color: red !important;
}

As in the red rectangles of the attachment.

Thanks in advance for your support!  :-\


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Subtotals calculation
« Reply #1 on: March 11, 2014, 05:58:40 pm »
1)
zeros are not a problem. There might be nulls in your data. Either remove the nulls (replace with 0) or implement a callback function for summary

http://paramquery.com/pro/api#option-column-summary

2)
calculated column : if it's a computed column in the view only (using render callback) then summary of that column won't work. You have to use actual field (dataIndx) in the data to make summary work. When you load data you can calculate the value in the computed field.
« Last Edit: March 11, 2014, 06:11:06 pm by paramquery »

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Subtotals calculation
« Reply #2 on: March 11, 2014, 09:34:20 pm »
Ok, I've got the idea.

NULLS in the database are the problem...

Thanks!

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Subtotals calculation
« Reply #3 on: March 11, 2014, 10:37:57 pm »
Subtotals are working fine now!

Just one thing: I'm in Europe, and I need to format the numbers with comma for decimal separator, period for thousands separator and to use the Euro sign.

I have a function formatNumber that takes a number as an argument, to do just that.

But how do I pass the total to the summary. This is what I tried without luck:

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

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Subtotals calculation
« Reply #4 on: March 12, 2014, 10:22:55 pm »
you can implement callback function

summary: {
 type: function (arr){
   //calculate sum of the elements in the arr.
   //use formatNumber function
   //and return value
 },
 title: [ "<b style='font-weight:bold;'>Total :</b> {0}" ]

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Subtotals calculation
« Reply #5 on: March 13, 2014, 12:15:26 am »
I tried this but it doesn't work, what's wrong? :-\

Code: [Select]
summary: {
                type: function(arr){
                calculo=eval(arr.join("+"));
                calculo=formatNumber(calculo,2,',','.');
                return calculo;
                },
                title: ["<b style='font-weight:bold;'>Total :</b> {0}"]
            }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Subtotals calculation
« Reply #6 on: March 14, 2014, 12:16:44 am »
What's the output.

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Subtotals calculation
« Reply #7 on: March 14, 2014, 12:27:34 am »
Just empty cells.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Subtotals calculation
« Reply #8 on: March 14, 2014, 12:30:29 am »
put a debugger or alert statement in your type callback and see what each line of your code is doing.

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Subtotals calculation
« Reply #9 on: March 14, 2014, 04:05:54 pm »
It seems the function isn't even being called, since nothing is outputed to the console/alert:

Code: [Select]
summary: {
                type: function (arr) {
                    calculo = eval(arr.join("+"));console.log("1 "+calculo);
                    calculo = formatNumber(calculo, 2, ',', '.');console.log("2 "+calculo);
                    return "calculo";console.log("3 "+calculo);
                },
                title: ["<b style='font-weight:bold;'>Total :</b> {0}"]
            }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Subtotals calculation
« Reply #10 on: March 14, 2014, 04:49:34 pm »
Correction from the API reference : http://paramquery.com/pro/api#option-column-summary

type is an array, it should be this:

type: [function (arr) {
                    calculo = eval(arr.join("+"));console.log("1 "+calculo);
                    calculo = formatNumber(calculo, 2, ',', '.');console.log("2 "+calculo);
                    return "calculo";console.log("3 "+calculo);
                }]


nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Subtotals calculation
« Reply #11 on: March 14, 2014, 05:45:30 pm »
Done. One less to worry about!
Thanks!