I’d like to ask the community for help. I’ve created a database (collection) which I can filtered. What I want to do is that filtered result sum up.
See I would expect the sum up function (in this case the result should be 454,36):
here is my code which work for filtering. Now I’d like to add the code for sum up.
import wixData from 'wix-data';
$w.onReady(() => {
$w('#dataset1').onReady(() => {
count();
$w('#code1 , #year , #supplier , #category').onChange(() => {
search();
})
$w('#clear').onClick(() => {
$w('#code1 , #year , #supplier , #category').value = "";
$w('#dataset1').setFilter(wixData.filter())
.then(count);
});
function search() {
let filter = wixData.filter();
let code = $w("#code1").value;
let year = $w("#year").value;
let supplier = $w("#supplier").value;
let category = $w("#category").value;
if (code && code !== 'all') {
filter = filter.eq("code", Number (code));
}
if (year && year !== 'all') {
filter = filter.eq("year", Number (year));
}
if (supplier && supplier !== 'all') {
filter = filter.eq("supplier", supplier);
}
if (category && category !== 'all') {
filter = filter.eq("category", category);
}
$w('#dataset1').setFilter(filter)
.then(count);
}
function count() {
let total = $w('#dataset1').getTotalCount();
if (total > 0) {
$w('#textCount').text = `${total} result has found.`;
} else {
$w('#textCount').text = "No result found!";
}
}
});
});
Do you have any idea how to sum up the columns Netto (field-key: netto)? Please help
Hi, wixDataAggregate would be the best route to go if he wasn’t wanting to show the total of the dataset that is filtered. For that, he’d be better off accomplishing this with a dataset command. The following solution uses getItems after applying the filter and loops through the results to get a total. Adjust element names as needed.
$w('#dataset1').setFilter(filter)
.then(count);
$w("#dataset1").getItems(0,count)
.then((getResults) => {
let nettoTotal = 0;
getResults.items.forEach((item) => {
nettoTotal = nettoTotal + item.netto;
})
$w("#txtSumNetto").text = nettoTotal.toString()+ " EUR";
})
})
I did modification of the code, but unfortunately the Netto prices are not still calculated. the result is that text element #textSum doesn’t show the sum of net prices…
here is a whole code, any idea?
import wixData from 'wix-data';
$w.onReady(() => {
$w('#dataset1').onReady(() => {
count();
$w('#code1 , #year , #supplier , #category').onChange(() => {
search();
})
$w('#clear').onClick(() => {
$w('#code1 , #year , #supplier , #category').value = "";
$w('#dataset1').setFilter(wixData.filter())
.then(count);
});
function search() {
let filter = wixData.filter();
let code = $w("#code1").value;
let year = $w("#year").value;
let supplier = $w("#supplier").value;
let category = $w("#category").value;
if (code && code !== 'all') {
filter = filter.eq("code", Number (code));
}
if (year && year !== 'all') {
filter = filter.eq("year", Number (year));
}
if (supplier && supplier !== 'all') {
filter = filter.eq("supplier", supplier);
}
if (category && category !== 'all') {
filter = filter.eq("category", category);
}
$w('#dataset1').setFilter(filter)
.then(count);
$w("#dataset1").getItems(0,count)
.then((getResults) => {
let nettoTotal = 0;
getResults.items.forEach((item) => {
nettoTotal = nettoTotal + item.netto;
})
$w("#textSum").text = nettoTotal.toString();
})
}
function count() {
let total = $w('#dataset1').getTotalCount();
if (total > 0) {
$w('#textCount').text = `${total} result has found.`;
} else {
$w('#textCount').text = "No result found!";
}
}
});
});
import wixData from ‘wix-data’;
$w.onReady(() => {
Sum_amount();
});
export function Sum_amount(){
wixData.aggregate(“OngoingProjects”) // instead I wanna use $w ( ‘#dataset6’ ). onReady ( () => {
.sum(“outstandingAmount”,“sumamount”)
.run()
.then( (results) => {
$w(‘#text310’).text = results.items[0].sumamount.toString();
This code works but I want to use with #dataset6 on ready function. Not able to do so. Please help