How to show count based on item in a repeater?

Hi all,

I want to be able to show how many resumes have been submitted to the said position. I am trying to count it based on a text field that is inside a repeater but it is not working. This is the code (and explanation below)

wixData . query ( “enterContest07” )
. contains ( “firstName” , $w ( ‘#text34’ ). text )
. count ()
. then ( ( results ) => {

 $w ( "#text42" ). text  =  results . toString () 

} )

enterContest07 is the collection
firstName is the column
text34 is the variable text box on repeater

Can someone help me with it? Tks!

I need a little more context. Is there are different count for every repeater item?
You could put the count, into each repeater item, when the page loads. So you would put it/call it from the onReady() function.

Does each repeater item show a different job?

@cristine , in this case your results.toString() is correct because you used the .count() feature.

This returns a number and not an array of items. Therefore it is NOT possible to use .items.length in your case.

Your code, where you count the number of rows(records) that match the ‘firstName’, looks good to me, but of course, I wasn’t able to test and run it. You can actually confirm if you get the correct number back by using:

console.log("The value of count is: "+results.toString());

Insert this after the .then() line.

From what I can tell, it looks like you have the count, you just don’t know how to insert it into the repeater. This is why I need a bit more information.

Hi Paul, thank you for your answer. Yes, each repeater shows a different job, so I want to show how many candidates each job has

I am thinking that you want these counts to display at the start when the page loads.

What you will need to do is perform the calculation for each repeater, after all the data has loaded.

Something like the code below.

I did not know the name of the dataset on webpage so I used ‘#dataset’. You will need to put in the name of your actual dataset.

The rest of the code is yours, which I am assuming you have tried and works.
Note that I had to use $item, instead of $w for the field names.

When going through each of the repeaters, you access the items (objects) on that particular repeater by using $item.

The trick is that you will do this once the page has loaded, and all the data has arrived into the dataset. You therefore have wait not only for the page to load, but for the data to load, hence the need for the 2 onReady() statements. Only then, can you go through each repeater and use that data that was loaded.

$w.onReady(async function () {
    $w("#dataset").onReady(async () => {
        $w("#repeater1").forEachItem( ($item, itemData, index ) => { 
            // count # of cadidates for each job
            wixData.query("enterContest07")
            .contains("firstName", $item("#text34").text)
            .count()
            .then( (results) => {
                $item("#text42").text = results.toString()
            })
        });
    });
});

 

Thank you! Your solution worked! (for future references: you need to add export function repeater1_itemReady ( $item ) to it)