I have a collection of results for multiple events, and I would like to be able to total multiple columns from the collection for each participant and have those results display in a table. I have been searching the forum and reading through old threads, and it appears that most people are totaling their values through the use of a for each loop. Most of these threads were created before May 2018 so I wasn’t sure how long the WixDataAggregateResult (https://www.wix.com/code/reference/wix-data.WixDataAggregateResult.html) function has been around, but will that accomplish what I need? The examples given only appear to sum one column and I would like to sum multiple. Is there a different newer function that will accomplish what I need? Also, a lot of these examples are displaying the data into textboxes. How would I go about displaying them into a table?
This is the code I have so far. What it does is get the current year of today’s date and display it into a textbox at the top of the page to signify the year it is showing statistics for (defaulting to this year on load), reaches out to another collection ‘Tournament’ to get an array of all the tournaments for the current year, and then pass the array of tournaments to an aggregate on the ‘TxResults’ Collection, grouped by the ‘member’ field and summing multiple columns in the collection.
import wixData from ‘wix-data’;
$w.onReady( function ()
{
// Gets today’s date
const today = new Date();
// Get the current year
const year = Number(today.getFullYear());
//console.log(year)
// set the year on the text
$w(‘#yeartext’).text = String(year);
//get list of txs for the current season not including classic
wixData.query(‘Tournaments’)
.eq(‘season’, year)
.ne(‘classic’, true )
.ascending(‘date’)
.find()
.then((results) => {
let txdata = results.items;
//get names of tournaments for current season
let txlist = txdata.map(item => item.title);
//console.log(txlist)
const filter = wixData.filter(txlist);
//const having = wixData.filter().gt("maxPopulation", 1000000);
wixData.aggregate("TxResults")
.filter(filter)
.group("member")
.sum('caught','alive','totallbs','totaloz','totalpts')
//.having(having)
.descending("totalpts")
.run()
.then( (tresults) => {
let items = tresults.items;
let numItems = tresults.length;
let hasNext = tresults.hasNext();
console.log(numItems)
console.log(hasNext)
console.log(items)
$w('#dataset1').setFilter(wixData.filter(items));
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
})
. catch ((err) => {
let errorMsg = err;
})
});