Set aggregate totals to repeater?

I’ve successfully used the code found here to sum a data collection into a table. But I actually want the data to display in a repeater. (I am ranking teams by points and like the repeating format.) How do I adapt the code so that the team name displays in one text box of the repeater and the aggregated score displays in another? Here’s my code so far for the table:

import wixData from ‘wix-data’;

$w.onReady(function () {
$w(“#table1”).columns = [{
“id”: “col1”,
“dataPath”: “teamName”,
“label”: “Team”,
“type”: “string”
},
{
“id”: “col2”,
“dataPath”: “points”,
“label”: “Total Points”,
“visible”: true,
“type”: “number”
}

]; 

wixData.query("PointsSubmission") 
	.descending("points") 
	.limit(1000) // include a limit if you have more than 50 items 
	.find() 
	.then((result) => { 
		const teams = result.items.map(x => x.teamName) 
			.filter((obj, index, self) => index === self.indexOf(obj)); 
		const aggregated = teams.map(x => { 
			return { 
				teamName: x, 
				points: result.items.filter(obj => obj.teamName === x) 
					.map(z => z.points) 
					.reduce((sum, current) => sum + current) 
			}; 
		}); 

		$w("#table1").rows = aggregated; 
	}); 

});

you should look in the API docs for $w.repeater and the onItemReady function and the .data property and then you can master and make this happen. I would take the same array you connected here to a table and just add it to .data = aggregated; and then in the onItemReady which loops through all items just do

$w(’#textbox1’).text = itemData.points;

and the value of that text will be the data you have in your data.

Hi Andreas,

Thanks for the quick response! So now I revised the code and am now getting the following two errors when previewed:

Red alert = Wix code SDK error: Each item in the items array must have a member named _id which contains a unique value identifying the item.
Yellow alert = Wix code SDK Warning: The data that was passed to data contained at least two items with the same ID: . Only the first item was accepted.

The reason I’m using aggregate in the first place is because my collection contains multiple items per team and will be regularly updated with new items. Is that causing this problem? How do I fix these errors?

My code is:

import wixData from ‘wix-data’;

$w.onReady(function () {
$w(“#repeater1”).onItemReady(($w, itemData, index) => {
$w(“#teamname”).text = itemData.teamName;
$w(“#teamscore”).text = itemData.points;
});

wixData.query("PointsSubmission") 
	.descending("points") 
	.limit(1000) // include a limit if you have more than 50 items 
	.find() 
	.then((results) => { 
		const teams = results.items.map(x => x.teamName) 
			.filter((obj, index, self) => index === self.indexOf(obj)); 
		const aggregated = teams.map(x => { 
			return { 
				teamName: x, 
				points: results.items.filter(obj => obj.teamName === x) 
					.map(z => z.points) 
					.reduce((sum, current) => sum + current) 
			}; 
		}); 
		$w("#repeater1").data = aggregated; 
	}); 

});

Hi Andreas,

Are you able to help with this? Still struggling to resolve =(
It looks like the error message is saying that there are multiple items with a blank ID, as it looks like a space between the colon and period.
" The data that was passed to data contained at least two items with the same ID: . "

Thanks!

Hey
All repeater items must have a _id field which must be unique. Take the _id of th erecords and add ”_id”: to your array and it will work. If repeater elements does not have the _id parameter it will break

Forgive my ignorance, but where would I add the “_id” in the code? And which ID would I use for the aggregated data of each team? Each item in the collection is given a unique ID, even if the team name is the same in multiple items.

const aggregated = teams.map(x => {
return {
_id: x._id,
teamName: x,
points: results.items.filter(obj => obj.teamName === x)
.map(z => z.points)
.reduce((sum, current) => sum + current)
};
});

I guess

That didn’t work, but I ended up finding another solution. I have the data submit to two different collections – one that maintains the history of every point submission, and one that only contains the total sum for each team. Then the repeater draws from the collection of sums. Thanks for helping, though!!

hey bro…do you have the complete code for this solution?