Data query with count and repeater

Hi, I have a repeater populated via 2 search fields, 2 dropdowns and 2 sliders, additionally the count of the filtered items is shown in a text box. All that functions pretty well. I had to copy the whole search for the two tasks (repeater and count) as I didn’t find a way to run it with one search only. I’m sure there is a way to do that, I just don’t know how as my coding skills are only two weeks young… Can anyone point me in the right direction, please?

function search() {
wixData.query( ‘Fahrzeuge’ )
.contains( ‘type’ , $w( “#searchBar1” ).value)
.and(wixData.query( ‘Fahrzeuge’ ).contains( ‘bundesland’ , $w( “#dropdown2” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).contains( ‘modell’ , $w( “#dropdown1” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).contains( ‘title’ , $w( “#searchBar2” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).ge( ‘baujahr’ , $w( “#slider1” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).le( ‘baujahr’ , $w( “#slider2” ).value))
.find()
.then(res => {
$w( ‘#repeater1’ ).data = res.items;
});
wixData.query( ‘Fahrzeuge’ )
.contains( ‘type’ , $w( “#searchBar1” ).value)
.and(wixData.query( ‘Fahrzeuge’ ).contains( ‘bundesland’ , $w( “#dropdown2” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).contains( ‘modell’ , $w( “#dropdown1” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).contains( ‘title’ , $w( “#searchBar2” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).ge( ‘baujahr’ , $w( “#slider1” ).value))
.and(wixData.query( ‘Fahrzeuge’ ).le( ‘baujahr’ , $w( “#slider2” ).value))
.count()
.then((num) => {
let numberOfItems = num;
$w( ‘#countText’ ).value = num;
})
}

Hi,

you don’t need to make a special query to count the query results.
after the first query you get WixDataQueryResult as a result object.
this object has a total count property (it counts your query results).

https://www.wix.com/corvid/reference/wix-data.WixDataQueryResult.html#totalCount

try to use this code:


function search() {
    wixData.query('Fahrzeuge')
        .contains('type', $w("#searchBar1").value)
        .and(wixData.query('Fahrzeuge').contains('bundesland', $w("#dropdown2").value))
        .and(wixData.query('Fahrzeuge').contains('modell', $w("#dropdown1").value))
        .and(wixData.query('Fahrzeuge').contains('title', $w("#searchBar2").value))
        .and(wixData.query('Fahrzeuge').ge('baujahr', $w("#slider1").value))
        .and(wixData.query('Fahrzeuge').le('baujahr', $w("#slider2").value))
        .find()
        .then(res => {
            $w('#repeater1').data = res.items;
            $w('#countText').value = res.totalCount.toString(); // or res.totalCount+"";
        });
}

Hi Gal,
perfect, thank you a lot!
Cheers