About totalCount

How can I modify my code in order to get results not just in the current page, but the total number? What am I doing wrong?


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 )

You want to get the total number of what?

Thank you very much for your help. As you must have guessed I know almost nothing of coding. As I’m 57 years old, that’s perhaps understandable. What I’m trying to do is to get the number of books (qtd) I have sold from a particular author (autor) in a chosen month. I have the collection and a repeater. The entire code I’ve tried is as follows. It works fine, except the results which are shown just for each repeater page, and not the sum altogether. Is this information enough for your kind help?
Thanks a lot


$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 () 

}

@carlosgomesnc
Ok, your code is a little bit confusing for me and really hard to understand, especialy without knowing your database.

Look here…

You are using this code here to do some kind of RESET right?
You are setting all the repeater values to …

  1. —>“mes”
  2. —> 0

But you first generate an object for this —> “itens”, why?
Why not go the DIRECT way?

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
 })

DIRECT-WAY:

$w("#repAutor").forEachItem(($item, itemData)=>{
   $item("#txtItem").text = "mes"
   $item("#tbQuantidade").value = 0
})

Your button —> “#btnSomar” is outside of repeater?

And even this one makes no sense for me…

$w.onReady(function () {
    $w("#repAutor").onItemReady(($item, itemData, index) => {
        $item("#txtItem").text = "mes"
        $item("#tbQuantidade").value = 0
    })

    $w("#btnSomar").onClick(()=>{calcular();})
});

function calcular(){
 let total

    $w("#repAutor").forEachItem(($item, itemData, index)=>{
        console.log("itemData--->qtd: ", itemData.qtd)
        itemData.qtd = $item("#tbQuantidade").value
        total = total + Number(itemData.qtd)
        $w("#txtSoma").text = String(total)
 })
 
    $w("#txtSoma").expand() //---> just to be sure that this element is expanded and not collapsed
    $w("#txtSoma").show('float')
}

Anyway, if your —> “itens”-data is not a reset-function, than it could work…

let itens =[
    {"_id":"0001","item":"mesX","qtd":0},
    {"_id":"0002","item":"mesY","qtd":22},
    {"_id":"0003","item":"mesZ","qtd":3},
    {"_id":"0004","item":"mesA","qtd":42},
    {"_id":"0005","item":"mesC","qtd":14},
]

Then it would look like this…

let ITEMS =[
 {"_id":"0001","item":"mesX","qtd":0},
 {"_id":"0002","item":"mesY","qtd":22},
 {"_id":"0003","item":"mesZ","qtd":3},
 {"_id":"0004","item":"mesA","qtd":42},
 {"_id":"0005","item":"mesC","qtd":14},
]



$w.onReady(function () {
    $w("#repAutor").data = ITEMS
    $w("#repAutor").onItemReady(($item, itemData, index) => {console.log($item._id)
        $item("#txtItem").text = $item.item
        $item("#tbQuantidade").value = $item.qtd
 })
    $w("#btnSomar").onClick(()=>{calcular();})
});

function calcular(){
 let total

    $w("#repAutor").forEachItem(($item, itemData, index)=>{
        console.log("itemData--->qtd: ", itemData.qtd)
        itemData.qtd = $item("#tbQuantidade").value
        total = total + Number(itemData.qtd)
        $w("#txtSoma").text = String(total)
        console.log(total)
 })
 
    $w("#txtSoma").expand() //---> just to be sure that this element is expanded and not collapsed
    $w("#txtSoma").show('float')
}

BTW: Take also a look onto the CONSOLE (press F-12 if you are using the chrome-browser) . You can do the same in the wix-editor.

I hope i understood your issue the right way.
Good luck and happy coding.

Just recognized, that you already have posted your issue here…
https://www.wix.com/velo/forum/coding-with-velo/how-to-sum-results-from-a-repeater