Author Topic: calculateSummary  (Read 4433 times)

sdswuxi2017

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 6
    • View Profile
calculateSummary
« on: May 25, 2017, 02:18:34 pm »
Hi,

I have a question  about calculateSummary() where I use multiplication or Division formula.
please check the attachment.

« Last Edit: May 25, 2017, 02:21:16 pm by sdswuxi2017 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6260
    • View Profile
Re: calculateSummary
« Reply #1 on: May 25, 2017, 05:47:14 pm »
Just use

Code: [Select]
formula: function (ui) {
     var rd = ui.rowData;
     return rd.qty * rd.price;
}

in the amount column formula.
« Last Edit: May 25, 2017, 09:57:39 pm by paramquery »

sdswuxi2017

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: calculateSummary
« Reply #2 on: May 26, 2017, 10:54:13 am »
Hi Sir,

I used the code you mentioned above, but it was not exactly the result i want.
In your code, the calculated column value could be rd.qty * rd.price. It's correct. But in the Summarydata row, the value of this cell will be calculated with the same logic in above cells: sum(rd.qty) * sum(rd.price).  In my case, we cannot calculate list this.
Please see attached screenshot and you may get a better understand to what i actually asked. The formula marked in Red is the one pqgrid provides and the formula marked in Green will be the one i want to achieve.

Thanks a lot in advance.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6260
    • View Profile
Re: calculateSummary
« Reply #3 on: May 28, 2017, 10:42:23 pm »
I see, you are right. I'm able to reproduce the mentioned issue with the formula.

I'm moving it to bug report, thanks for reporting the issue.

Meanwhile you would need to calculate the product column manually ( instead of using the formula ) in calculateSummary() function.
« Last Edit: May 28, 2017, 10:50:07 pm by paramquery »

sdswuxi2017

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: calculateSummary
« Reply #4 on: June 20, 2017, 09:02:15 am »
Hi Sir,

Would you please advise if this bug will be fixed in next release? And when it would happen?

Thanks.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6260
    • View Profile
Re: calculateSummary
« Reply #5 on: June 21, 2017, 12:10:31 am »
I found that it works fine with some modifications in the example code.

Code: [Select]
  //calculate summary data.
  function calculateSummary() {
    var revenueTotal = 0,
      profitTotal = 0,
      productTotal = 0,
      data = this.option('dataModel.data'),
      len = data.length;

    data.forEach(function(row){
      revenueTotal += row.revenues;
      profitTotal += row.profits;
      productTotal += row.product;
    })

    var revenueAverage = (revenueTotal / len),
      profitAverage = (profitTotal / len),
      productAverage = (productTotal / len),
      totalData = {
        rank: "Total",
        company: "",
        revenues: revenueTotal,
        profits: profitTotal,
        product: productTotal,
        pq_rowcls: 'green',
        summaryRow: true
      },
      averageData = {
        rank: "Average",
        company: "",
        revenues: revenueAverage,
        profits: profitAverage,
        product: productAverage,
        pq_rowcls: 'yellow',
        summaryRow: true
      };

    this.option('summaryData', [totalData, averageData]);
  }

Code: [Select]
{ //formula field.
      title: "Expenditure = Revenues * Profits",
      dataType: "float",
      dataIndx: 'product',
      format: '$##,###.00',
      editable: false,
      formula: function(ui) {
        var rd = ui.rowData;
        if (rd.summaryRow) {
          return rd.product;
        } else {
          return rd.revenues * rd.profits;
        }
      }
    }

Code: [Select]
    //calculate summary upon these 2 events.
    dataReady: calculateSummary,
    change: calculateSummary,   

http://jsfiddle.net/dhnygcmf/2/

Hope it helps.
« Last Edit: June 21, 2017, 09:08:51 am by paramquery »

sdswuxi2017

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: calculateSummary
« Reply #6 on: June 23, 2017, 01:28:23 pm »
 The issue has been resolved,thank you. :)