I am working on two data calculations on a page. The first one is taking a filtered data set and calculating all values in a column. I am using this code.
$w.onReady(function () { $w("#dataset4").onReady(() => { populateCalculatedFields(); } ); $w("#dataset4").onCurrentIndexChanged( (index) => { populateCalculatedFields(); } ); } ); function populateCalculatedFields() { const currentItem = $w("#dataset4").getCurrentItem(); $w("#text58").text = currentItem.comPaymentTotal; }
This is returning the top records comPaymentTotal but it is not taking all 5 in the filtered result and suming them together.
The second thing I am trying to complete is also taking the result count from a filtered data set like it return 5 rows and send it to a text field. Need some help there also.
Getting closer but still not calculating.
$w.onReady(function () { let count = $w("#dataset4").getTotalCount(); // No. of items in dataset // $w("#dataset4").getItems(0, count) // Get all items // .then((results) => { let sumTotal = 0; let items = results.items; items.forEach(item => { sumTotal = sumTotal + Number(item.comPaymentTotal); }); $w("#total").text = "" + sumTotal; // Add other calculations here // }).catch((err) => { console.log(err); }); });
Now I have created a button3 and found some working code and it doesn't work for me after setting my dataset name and my comPaymentTotal column.
export function button3_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("#dataset4").getTotalCount(); // get all of the items $w("#dataset4").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.comPaymentTotal); }); $w("#text60").text = "" + sumTotal; }).catch((err) => { console.log(err); }); }