Trying to sum db values with a loop

Hello Mr. Tiaan! I got the code. I’d like to see how you set up the encoding site page. Thank you very much for your attention!

https://tiaanrich.wixsite.com/mysite/test/1234

I tried to make this, but the result is “NaN” only

You need to provide more details… code, database collection field keys, etc. As you can see from above, the code is working and being used.

Thank you so much for posting this! It works like a charm.

Though I’m a little surprised haha…
I pasted the code and only adjusted to the right dataset and text field - and I’m wondering where in the code it specifies exactly what field it should sum?

Here are the lines of code that sum the field:

	items.forEach(item => {
	   // the amount is a string so convert
	   sumTotal = sumTotal + Number(item.amount);
	});

The variable item is an item (or row) from the collection (via the dataset), and amount is the field in the item that we want to sum.

@yisrael-wix Got it! It just so happened that my collection had the same field name. Thank you for taking your time to respond!

Hi Yisrael and Tiaan,

I’ve used the code you shared for calculation, thanks for that.
I have 2 question and need your help.

  1. in same code I need to have calculation like this: same Sum to be divided to all displayed items count and that final only to show in text field >> I GOT HOW TO DIVIDE, BUT NEED TO MAKE SO THAT FINAL NUMBER ROUNDED TO BE 1 DIGIT ONLY AFTER POINT, LIKE 12.4 INSTEAD OF 12.423333312 , TNX…
  2. If it’s possible that instead of click button the calculation happened by any change of filter?
    below is my full code, kindly advise how to make these changes in my code.
    P.S. when I’m going preview mode I’m getting 2 lines of same error, though everything working fine, any advise also on this? Publisher ‘’ failed with error: ‘Failed to fetch’ TypeError: Failed to fetch

code of my page:

import wixData from ‘wix-data’;

export function button1_click(event, $w) {
// 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 + Number(item.TotalScore);
});
$w(“#grandtotal”).text = “” + sumTotal;
}). catch ((err) => {
console.log(err);
});
}

$w.onReady(() => {
$w(‘#dataset1’).onReady(() => {
count();

    $w('#weekno , #storename , #staffname').onChange(() => { 
        search(); 
    }) 
    $w('#clear').onClick(() => { 
        $w('#weekno , #storename , #staffname').value = ""; 
        $w('#dataset1').setFilter(wixData.filter()) 
            .then(count); 
    });

Hi there,

Hope you can help, I’ve so far overcome all the obstacles thrown my way for my site build, but the one that really has me stumped is what I think you guys are discussing on here.

Can I first apologize in advance, I’m an absolute amateur with anything do to with code.

So I’ve added a ‘sum’ column to my dataset called ‘MemberGreenPoints’ as you can see below, and am just wanting to add some code that will automatically add the sum of the ‘Biking Points OandA’, ‘Coffee Cup’, ‘Economic OandA’ and ‘Water Bottle OandA’ values.


Is there any way you can show me what the code should look like for this please? I’ll be forever grateful!!

Also, this is where my amateur really shines through, where does the code go? Is it on the page code panel, or the site or is it a hook? Or somewhere else altogether that I’ve not yet discovered?

Thanks a million.
Jobbo

@jobbojnr

Unfortunately you won’t be able to add code to the database to do it automatically. My advice would be to do the calcs on a spreadsheet and simply import the values.

Good luck!
Tiaan

@tiaan

Thanks Tiaan, that way won’t work as I need the total to sum immediately after they’ve submitted their points.

Is there another way around this that you know of?

Cheers,
Jobbo

I need something like this for my repeater but I don’t not need the dropdown and the submit button. I have a repeater and an application form which both connected to a database collection(db). So when a user submit the application, the db and my repeater will automatically update. What I need is the total sum can be outside the repeater like this in the picture?

There is another thing I like to do is, if a user submitted wrongly and decide to submit it again, is there a code that can remove the user previous submission?

FYI, I am a beginner of Wix code, so please help. Thanks

@tiaan @yisrael-wix I am trying to get this to work but my table is in a repeater on a dynamic page. The repeater is linked to dataset1 and table is linked to dataset 2. Dataset2 is filtered with the built in filter option not through code based on a value in dataset 1. Dataset 1 is filtered from the dynamic dataset, and here is the code I am using:

import wixData from 'wix-data'


export function button1_click(event$w) {
 //Add your code for this event 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("#dataset2").getTotalCount();
 
 // get all of the items
   $w("#dataset2").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 + Number(item.type1StallsPurchased);
       console.log(sumTotal);
    });
    $w("#text19").text = "" + sumTotal;
    }).catch((err) => {
       console.log(err);
    });
}


All I get is NaN

The table is loading perfect and is filtering things exactly how I want and displaying them perfectly. Any ideas?