Total Count of Filtered Search Result

Hello, I need a little help. I am not sure where to add the below code within my original code. My goal is to display a text box that has the total number of search results once the user has filter their search. ie. (35 results.) I have added a text box that is label results: with an ID called searchcount.
This is the code I am trying to add:

$w(‘dataset2’).setFilter(wixData.Filter).then((num) => {
console.log($w(‘#dataset2’).getTotalCount(), “heres count”);
$w(‘#searchcount’).text =
$w(‘#dataset2’).getTotalCount().tostring();

This is my original filtered search code that is working properly.

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#noResText”).hide();
});

export function searchbutton_click_1(event, $w) {
$w(“#dataset2”).setFilter(wixData.filter()

    .contains("category", $w('#vendordropdown').value) 
    .and(wixData.filter() 
    .contains("cityState", $w('#cityandstatedropdown').value) 
) 
).then((results) => { 

let count = $w(“#dataset2”).getTotalCount();
if (count === 0){
$w(“#noResText”).show();
}
if (count >= 1) {
$w(“#noResText”).hide();
}
console.log(“Dataset2 is now filtered”);
$w(‘#Vendorlist’).data = results.items;
}). catch ((err) => {
console.log(err);
});

$w(‘#Vendorlist’).expand();
}

Any help would be greatly appreciated. Thanks

like this…

console.log(“Dataset2 is now filtered”);
$w(’ #Vendorlist ').data = results.items;
let numberOfResults = results.totalCount;
console.log(numberOfResults );

Hi Mike, Thank you for replying… The code you provided didn’t work.
Did I do something incorrect with my code? am I missing something?

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#noResText”).hide();
});

export function searchbutton_click_1(event, $w) {
$w(“#dataset2”).setFilter(wixData.filter()

    .contains("category", $w('#vendordropdown').value) 
    .and(wixData.filter() 
    .contains("cityState", $w('#cityandstatedropdown').value) 
) 
).then((results) => { 

let count = $w(“#dataset2”).getTotalCount();
if (count === 0){
$w(“#noResText”).show();
}
if (count >= 1) {
$w(“#noResText”).hide();

    } 
      console.log("Dataset2 is now filtered"); 
      $w('#Vendorlist').data = results.items; 

let numberOfResults = results.totalCount;
console.log(numberOfResults);
}). catch ((err) => {
console.log(err);
});

$w(‘#Vendorlist’).expand();
}

try this instead

console.log(“Dataset2 is now filtered”);
$w(’ #Vendorlist ').data = results.items;
let numberOfResults = $w(“#dataset2”).getTotalCount();
console.log(numberOfResults );

I tried the other code you provided. nothing happens, no result number is displayed when I preview. I am not sure what I am missing within the code. I feel like I am missing the link between my text box and the code. Thank you for your help.

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#noResText”).hide();
});

export function searchbutton_click_1(event, $w) {
$w(“#dataset2”).setFilter(wixData.filter()

    .contains("category", $w('#vendordropdown').value) 
    .and(wixData.filter() 
    .contains("cityState", $w('#cityandstatedropdown').value) 
) 
).then((results) => { 

let count = $w(“#dataset2”).getTotalCount();
if (count === 0){
$w(“#noResText”).show();
}
if (count >= 1) {
$w(“#noResText”).hide();
}
console.log(“Dataset2 is now filtered”);
$w(‘#Vendorlist’).data = results.items;
let numberOfResults = $w(“#dataset2”).getTotalCount();
console.log(numberOfResults );
}). catch ((err) => {
console.log(err);
});

$w(‘#Vendorlist’).expand();
}

what does the console log ?

TypeError: Cannot read property ‘items’ of undefined
line 23 which is
$w(‘#Vendorlist’).data = results.items;

well that would be the reason it is not console logging numberOfResults . Your code is broken before it reaches the console log.

ok thanks I guess I will research how to fix it. I appreciate your help.

just start by temporally disabling that line of code //

// $w(’ #Vendorlist ').data = results.items;

That works but the number of the items show in the console not on the page. I need it to show on my page in the text box . It should show like this: 25 results

Yes that is beacuse you need to set your on screen element to numberOfResults. All we have done in the code is calculate the value.

$w(’ # textID’).text = numberOfResults;

Or

$w(’ # inputID’).value = numberOfResults;

Ah!. That makes sense. Ok will do thank you

Hi there,
When I type the code in I get this output in the console. Do I need to add # in the coding? like (19) after numberof results?

Wix code SDK error: The text parameter that is passed to the text method cannot be set to the value 19. It must be of type string.

console.log(“Dataset2 is now filtered”);
let numberOfResults = $w(“#dataset2”).getTotalCount()
$w(‘#searchcount’).text = numberOfResults;
console.log(numberOfResults );
}). catch ((err) => {
console.log(err);
});
$w(‘#Vendorlist’).expand();
}

You need to convert the results to String

$w("#searchcount").text = String(numberOfResults); 

Hi Shan,
Thank you that worked… I was close to getting it… I was just missing some parts. I appreciate everyone help and time .

text does not change with dynamic value. How it will be solved. Please share your suggestions