Hello everyone
I would like to make some math calculations from a repeater filtered results. I know already how to sum database quantities, for example. However, when I filter those quantities for each month, I would like to obtain just the quantities sum of that particular month.
Anyone?
Hi Nova,
Loop trough your repeater with the .forEach
Before loop add a variable ‘var sum = 0’
In the for each do sum = sum + “your value to add”
Kind regards,
Kristof
Hello Kristof
Thanks a lot for your kind help. I will surely try it, and give my feedback later.
All the best
Carlos
Sorry for my ignorance, Kristof. Could you give me some code example?
@novacausa
Like my Ninja-Friend → Kristof already suggested you, you’ll have to loop trough your repeater.
It is not reccomended to ask for codes, but you will get your code, when you take a look into the VELO-API.
//Loop through all of a repeater's repeated items
$w("#myRepeater").forEachItem( ($item, itemData, index) => {
let repeatedElement = $item("#repeatedElement"); console.log(repeatedElement)
let nonRepeatedElement = $w("#nonRepeatedElement"); console.log(nonRepeatedElement)
let itemDataValue = itemData.someProperty; console.log(itemDataValue)
} );
Of course you will have to modify, expand & complete it, to get it to work!
How to solve such issues on your own ?
Take a look into the VELO-API !!!
@russian-dima
Thank you for your kind help.
Hello
I believe I managed to sum results from repeater. However, I would like to get the global value and not a partial sum in each page of the repeater. Can you help me?
$w . onReady ( function () {
$w ( “#btnSomar” ). onClick (()=>{
calcular ()
})
let itens = [
{ “_id” : “0001” , “item” : “mes” , “qtd” : 0 },
]
$w ( “#repAutor” ). data = itens
$w ( “#repAutor” ). forEachItem (( $item , itemData )=>{
$item ( “#txtItem” ). text = itemData . item
$item ( “#tbQuantidade” ). value = itemData . qtd
})
});
function calcular (){
let itemNaLista = $w ( “#repAutor” ). data
$w ( “#repAutor” ). forEachItem (( $item , itemData , index )=>{
itemNaLista [ index ]. qtd = $item ( “#tbQuantidade” ). value
})
let total = itemNaLista . reduce (( total , item ) => total + Number ( item . qtd ), 0 );
$w ( “#txtSoma” ). text = String ( total )
$w ( “#txtSoma” ). show ()
}