Author Topic: Pivot Grid - Default Aggregate function for Columns  (Read 1009 times)

mlipham

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 2
    • View Profile
Pivot Grid - Default Aggregate function for Columns
« on: March 20, 2025, 08:50:33 am »
I have a (Pivot) grid with several data types in it – dates, string, and some numbers.


I have been tasked with fixing the behavior where if one drags a date field into the “Aggregates” box (lower right), it automatically assumes “Sum()”.

It can be clicked and changed to a proper aggregate function, like count() or Min/Max, but the default is sum(). As you know, this means the values in the grid appear as NaN or other weird formats.
 

One suggestion I saw was to add summary  type=null to each column definition, which didn't seem to help.


As an alternative, I have some custom .js code that seems to fit the bill – almost. The code below will intercept the ‘drag-in’ and changes it to count() by default – yay! – but the data in the grid still says NaN, as though it ran it as sum(). This leads me to believe I probably need to refresh the grid to register the new change.


So even if I add the refresh, I can’t help wondering – is there a simpler way that I am overlooking? Am I going about this the hard way? Is there a much simpler approach or configuration to achieve the same?


Below please find my code scratchings: (Yes, I also need to add code to handle strings)


( event: pivotCM:  )

Code: [Select]
function (evt, ui) {
   
    //previous code to concat field value to column header

    ...

    //Code to change from sum to count

    ui.CM.forEach(col => {

        if (col.dataType == "date" && col.summary.type == "sum") {

            col.summary.format = "";

            col.summary.type = "count";

        }

        if (col.dataType === "date") {

            if (col.summary.type === "count") {

                col.summary.format = "0";               

            } else {

                col.summary.format = "mm/dd/yy hh:mm:ss";

            }

        }

    });

}


Thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6385
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #1 on: March 20, 2025, 12:37:59 pm »
I understand that's incorrect behavior of pqgrid pivot to use default aggregate as "sum" for all columns. It would be fixed in upcoming version.

Meanwhile please use this patch to fix it

Code: [Select]
jQuery.paramquery.cToolPanel.prototype.getObj=function(t){var a={},e=this.that;return t.find(".pq-pivot-col").each((function(t,r){var u=r.dataset.di,o=e.getColumn({dataIndx:u}),n=r.getAttribute("type")||o.summaryDefault||e.iGroup.getAggOptions(o.dataType)[0];o.summaryDefault=a[u]=n})),a};

you may remove the custom code in pivotCM event related to it.
« Last Edit: March 20, 2025, 12:39:41 pm by paramvir »

mlipham

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #2 on: March 21, 2025, 08:08:19 pm »
Awesome - thanks!
I'll keep my eyes peeled for the new version.