In my head it seems simple but i can’t find anything anywhere that helps me out. Im looking for a code that will sit on the homepage of the website that does the following:
Calcualte the SUM of all of one field in a database.
Inserts this value into a text elemenet on my homepage.
Not having much look with this, i had a look at the link you posted and found the basis for the code but it is resulting as 0.
$w.onReady( function () {
//TODO: write your page related code here…
// no need to set the filter on the database since it’s still set
// how many items filtered in the dataset? let count = $w(“#dataset1”).getTotalCount();
// get all of the items
$w(“#dataset1”).getItems(0, count)
.then((results) => { let sumTotal = 0; // declare sum let items = results.items;
items.forEach(item => {
// the amount is a string so convert
sumTotal = sumTotal + item.weekProfitLoss;
});
$w(“#text41”).text = “£439” + sumTotal;
}). catch ((err) => {
console.log(err);
});
Hi,
Do you get the items from the database? you can check that using " console.log ".
Maybe you need to run the code only after the dataset in ready, use the onReady event handler.