Hi everyone,
As you can see on the picture I have a column in my dataset collection with numbers. I would like to be able to sum up all these numbers to then be able to display the total on my site in a text box.
I have been trying to figure this out for 2 days nows, reading everything I could find. Still can’t figure out how to do it. Hope you can help!
Thanks! - having read the article I’m still stuck. As I’m pretty new to this it might be to complicated. I guess I understand so far the actions the aggregate does - but where in the code do I tell it where to show the result?
All I want is to sum up a column and show it in a textbox.
Here is something to get you jump-started. This Sum_amount function is being called in the onReady function of the page, after all of the elements are loaded. The value is placed in an input called “input1” after the successful aggregate query runs. The import line needs to be there in order to use any wixData function. You will need to substitute the real name of the collection for “CollectionName”.
I’m using the exact same code and only getting the value for the first row in the column. Two things to note that are probably different, I am filtering that dataset to use on the page and have a table on the page that displays the filtered data. Could this be screwing it up as well? Its also on a dynamic page not a static page. Any help would be greatly appreciated!!
That aggregate query is run directly on the collection data as the page loads, so filtering of the dataset would not impact that.
I have not used dynamic pages much, so I’m hesitant to suggest one way or another whether or not that it is the culprit. It would be helpful if you could share your code. Perhaps, the cause of the problem will be obvious to one of us.
what is so strange is that it gives me the value of the first cell in the row on the dynamic page. I will say that the routing may be complicated. My page is a dynamic page built from the showInformation dataset. On that page, I have a table on that page that pulls from the stallSales dataset and filters based on the horseshowId matching the ID field from the showInformation page. I hope that makes sense, the idea is that I will next try to integrate stripe into the stallSales portion.
My issue now I need to put a filter in so that it only adds up some of the rows. In my dataset ‘stallSales’ I have a column for horse show ID. I am on a page that all that data in the table is filtered off of that horse show ID. It is a dynamic page built from the show information dataset. I am just trying to figure out how to use the horse show ID that correlates to the dynamic page I am on to set the filter and I keep coming up blank. Any help would be greatly appreciated.
On a dynamic page, you should get the current item form the dynamic dataset (in the onReady event of the dataset, using the getCurrentItem() function). Read from the current item the value for horse show ID. Now, set this value as a filter in the aggregate query above.
UPDATE: I just realized user Id in the private member data is the same as owner ID in my collection.
@tony-brunsman I want to do the same filter as I would on a dataset I’m displaying on the page (where logged in user’s Owner ID (user ID) matches the Owner ID in the data collection). (I tried referencing the filtered data collection in the aggregate function but it wasn’t valid.)
This is my page code (I am a beginning coder, BTW). I’m getting totals for ALL records, not the filtered ones.
UPDATED CODE (8/28):
import wixData from ‘wix-data’; import wixUsers from ‘wix-users’;
let user = wixUsers.currentUser;
let userId = user.id; let isLoggedIn = user.loggedIn;
let searchOwner = user.id.value; let filter = wixData.filter().eq(“_owner”,searchOwner); //I can’t figure out why this isn’t working
$w.onReady(() => {
Sum_amount();
});
export function Sum_amount(){
wixData.aggregate(“MilesHoursLog”)
.filter(filter) //wouldn’t this use the filter I referenced above?
.sum(“hours”,“sumHours”)
.sum(“miles”,“sumMiles”)
.run()
.then( (results) => {
$w(‘#input1’).value = results.items[0].sumHours; //display total in input field at bottom of table
$w(‘#input2’).value = results.items[0].sumMiles; //display total in input field at bottom of table
} );
}
@tony-brunsman Thanks for getting me closer (I think) Heath H-M and anthonyb. But now my totals are zero (before they were for the whole data collection. I hope my mistake is little but I can’t figure out what’s wrong (no errors in Preview).
import wixData from ‘wix-data’; import wixUsers from ‘wix-users’;
let user = wixUsers.currentUser;
let userId = user.id; let isLoggedIn = user.loggedIn;
export function Sum_amount(){
wixData.aggregate(“MilesHoursLog”)
.filter(filter)
.sum(“hours”,“sumHours”)
.sum(“miles”,“sumMiles”)
.run()
.then( (results) => {
$w(‘#input1’).value = results.items[0].sumHours; //display total in input field at bottom of table
$w(‘#input2’).value = results.items[0].sumMiles; //display total in input field at bottom of table
} );
}
let searchOwner = user.id.value; let filter = wixData.filter().eq(“_owner”,searchOwner);
$w.onReady(() => {
Sum_amount();
});
export function Sum_amount(){
wixData.aggregate(“MilesHoursLog”)
.filter(filter)
.sum(“hours”,“sumHours”)
.sum(“miles”,“sumMiles”)
.run()
.then( (results) => {
$w(‘#input1’).value = results.items[0].sumHours; //display total in input field at bottom of table
$w(‘#input2’).value = results.items[0].sumMiles; //display total in input field at bottom of table
} );
}
Any ideas on how I can filter the data collection by owner for aggregating hours and miles for the logged in user?
let searchOwner = user.id.value; let filter = wixData.filter().eq(“_owner”,searchOwner);
$w.onReady(() => {
Sum_amount();
});
export function Sum_amount(){
wixData.aggregate(“MilesHoursLog”)
.filter(filter)
.sum(“hours”,“sumHours”)
.sum(“miles”,“sumMiles”)
.run()
.then( (results) => {
$w(‘#input1’).value = results.items[0].sumHours; //display total in input field at bottom of table
$w(‘#input2’).value = results.items[0].sumMiles; //display total in input field at bottom of table
});
}
No errors, but the filter is not working. The aggregated totals are for all records in the collection instead of for those of the logged-in user.
I have same problem with the filtering also, how can I filter the name so that I can compute only his runs (distance). I am making a fun run page. This is what I have: