Hi Guys
I'm still a novice what loops are concerned, but I've come up with the following. I'm trying to find all the values for a specific name located in the title column. So I run a filter, get the values back, and now I'm trying to sum them to get a total.
Not working yet, any advice will be appreciated!
$w.onReady(function () {
$w("#dynamicDataset").setFilter(wixData.filter()
.eq("title", $w('#dropdown1').value))
.then((amount) => {
const sumTotal = [];
for (let i = 0; i < amount.items.value; i++) {
sumTotal.push(new amount(amount.items[i].toFixed));
}
$w("#textSum").text = sumTotal;
});
});
@Tiaan - wix-coders.com @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?
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
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
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); });
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?
I tried to make this, but the result is "NaN" only
Hi Maxwell
See this post above from 12 Mar, this is basically the only code on the website:
Let me know if you need clarification on anything. Good luck!
Tiaan
Hello Tiaan
I saw your website https://tiaanrich.wixsite.com/mysite/test/1234
I need to make this
I'm a begginer in WIXCode
can I to see inside your website?
I send a big virtual fist bump for you guys for grinding through this!! This came through right on time for what I need. Thanks for sharing and contributing!!
Thanks Yisrael! Helped me too!
This works great, thank you! I published the site, it's available here for anyone wanting to try something similar:
https://tiaanrich.wixsite.com/mysite/test/1234
Yisrael's working code:
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("#dynamicDataset").getTotalCount(); // get all of the items $w("#dynamicDataset").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.amount); }); $w("#textSum").text = "" + sumTotal; }).catch((err) => { console.log(err); }); }
I'll live.
Hi Yisrael
Thank you for taking the time to help me out with this!
LOL!! I'll give it a quick go.. I hope your confidence will recover! ////
All the best,
Tiaan
Hey Tiaan,
Wow! Take me out back and shoot me.
I was trying to use the dataset like using the collection directly. Wrong, wrong, wrong. 🤯
Here's the corrected code with comments:
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("#dynamicDataset").getTotalCount(); // get all of the items $w("#dynamicDataset").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.amount); }); $w("#textSum").text = "" + sumTotal; }).catch((err) => { console.log(err); }); }
OK - so I'm embarrassed. 🙈
I made the changes in the test project you set up and saved them there as well. Give it a try.
I'm now gonna go cry in my beer.
All the best,
Yisrael
Here you go:
https://tiaanrich.wixsite.com/mysite/test/1234
I understand. Let me set something up and I'll post it here.
I acutally don't know why I even bothered with code for the repeater, this filter does all the work, just twice, so I've removed that now.
I'll check back in in 10min with a link!
Sorry, but I don't see anything wrong. Without actually seeing the site code itself I don't have the proper context to understand it.
Maybe you could clone the site and get rid of the sensitive information so I can look at the code. Otherwise, I don't know what I can do.
BTW - why not just calculate the total right after $w("#table1").rows = results.items;
LOL, I doubt that.. It's a securely hidden page on a live financial site, so I can't share here, but I'll try to give some more contecxt on the page below.
I'm running the consol.log(), it still gives me this error:
TypeError: undefined is not an object (evaluating 'amount.items')
What I do first is to build the dropdown items from the available items in the database as they will change on a daily basis.
I then have a repeater that's connected to #dynamicDataset and successfully filters with the script below:
export function dropdown1_change(event, $w) { $w("#dynamicDataset").setFilter(wixData.filter() .eq("title", $w('#dropdown1').value)) .then((results) => { console.log("Dataset is now filtered"); $w("#table1").rows = results.items; }).catch((err) => { console.log(err); }); }
Then, I want to use this same dropdown value as my filter to calculate to total for all the values that I've just filtered and am displaying on my table with this script:
export function button7_click(event, $w) { $w("#dynamicDataset").setFilter(wixData.filter() .eq("title", $w('#dropdown1').value)) .then((amount) => { console.log(amount.items); const sumTotal = 0; let items = amount.items; items.forEach(item => { sumTotal + item.value; }); $w("#textSum").text = "" + sumTotal; }).catch((err) => { console.log(err); }); }
BTW - did you try the console.log()?
Post the URL so I can inspect. Maybe I'm having a be dumb moment.