Pass parameter(s) to custom aggregate function

Hello,

I am trying to implement a custom function for an aggregate. The format I am seeking is the following :
this.totalVacances =  [{
columns: [
{
type: 'Custom',
field: 'arrayOfWeeks.week1.displayedText',
format: 'C2',
footerTemplate: 'Sum: ${Custom}',
customAggregate: this.customAggregateFnWeek1
},
{
type: 'Custom',
field: 'arrayOfWeeks.week2.displayedText',
format: 'C2',
footerTemplate: 'Average: ${Custom}',
customAggregate: this.customAggregateFnWeek2
}            // AND SO ON... THESE ARE ONLY 2 WEEKS. IT COULD GO UP TO 52 weeks (but not always 52 weeks, it depends on the data supplied)
]
}];


As you can see, I would like to dynamically create and aggregate array and use a custom aggregate function to display custom information. Here is what I have so far :
for (let i = 1; i <= Object.keys(employee.arrayOfWeeks).length; i++) {
  // Dynamically create footer rows
  this.totalVacances[0]['columns'].push({
type: 'Custom',
field: `arrayOfWeeks.week${i}.displayedText`,
format: 'C2',
footerTemplate: '${Custom}',
customAggregate: this.customAggregateFn
});
}

My custom aggregate function looks like this :
customAggregateFn(data, i): number {

let total = 0;

data.result.forEach(employee => {
if (employee['arrayOfWeeks']['week' + i]['cellClass'] === 'semaine1Vacances') {
total += 1;
}
});

return total;
}

My problem is that I need to supply the "i" value of the for loop used to create the aggregate to the custom function for the calculations. I suspect that the custom aggregate doesn't accept this kind of manipulation. Is there a way I can transfer the value of "i" to the custom aggregate function?

Here is a working example :

Thank you for your support

3 Replies 1 reply marked as answer

RR Rajapandi Ravi Syncfusion Team June 17, 2020 01:20 PM UTC

Hi Remy, 

Greetings from syncfusion support 

From validating your query, we could see that you like to pass the iteration value of the loop to the custom aggregate function. So we suggest you to follow the below way to achieve your requirement. Please refer the below code example and sample for more information. 

App.component.ts 
 
this.totalVacances[0]['columns'].push({ 
                  type: 'Custom', 
                  field: `arrayOfWeeks.week${i}.displayedText`, 
                  footerTemplate: '${Custom}', 
                  customAggregate: this.customAggregateFn.bind(this, i) 
                }); 


Screenshot: 

 

Regards,
Rajapandi R 


Marked as answer

RE Remy June 21, 2020 03:05 AM UTC

Hi Rajapandi,

That's exactly what I was searching for. Thank you very much for your support.

Regards,
Remy


RR Rajapandi Ravi Syncfusion Team June 22, 2020 08:25 AM UTC

Hi Remy, 

We are happy to hear that our suggested solution was working with your end. 
 
Please get back to us if you need further assistance. 

Regards, 
Rajapandi R 


Loader.
Up arrow icon