I have repeater witch showing first two columns from my datasets, 3rd column is variable on frontend. Below is part of code I’m using in my repeater
let sum = ( column1 * column2)
$item(“#colums3”).text = sum.toString()
I have hard time to figure how can I get sum from variables in 3rd column since I can’t use (wixData . aggregate (“data”).sum(column3) not part of my data)
Can you try this?
let total = 0
$w('#repeater1').onItemReady(($item, itemData, index)=>{
for (let i = 0; i < itemData.length; i++) {
let partialSum = itemData[i].column1 * itemData[i].column2;
total+=partialSum
}
})
$w("#total").text = total.toString()
itemData . length returned value of undifiened I had strugles to find solution. Then relocated for loop in query results.length. working well now. First time using “for loop” thank you so much for showing path - very helpfull. Let me know if I’m wrong on this changes.
import wixData from ‘wix-data’ ;
let total = 0
$w . onReady ( function () {
$w ( “#repeater1” ). onItemReady ( ( $item , itemData , index ) => {
$item ( “#column1” ). text = itemData.colum1 . toString ();
$item ( “#column2” ). text = itemData.colum2 . toString ();
} );
wixData . query ( “test” )
. find ()
. then ( ( results ) => {
$w ( “#repeater1” ). data = results.items ;
for ( let i = 0 ; i < results.length ; i ++) {
let partialSum = results.items[i ]. colum1 * results.items[i ]. colum2 ;
total += partialSum
$w ( “#totalSum” ). text = total . toString ()
}
} );
});
Can you do results.totalCount instead of results.length , I guess it won’t be length.
And if it’s error again. change it like this.
let partialSum = Number(results.items[i].colum1) * Number(results.items[i].colum2);
You can also set data to column3’s like this.
$w("#repeater1").onItemReady( ($item, itemData, index) => {
$item("#column1").text = itemData.colum1.toString();
$item("#column2").text = itemData.colum2.toString();
//The reason I use the Number function is I don't know if the column3 data is
//number, if it is numbers you may not write it.
$item("#column2").text = (Number(itemData.colum1) * Number(itemData.colum2)).toString()
} );
Thank You for all options