Counting Filtered Dataset Records

I am very new (and getting on years) to coding and am struggling to code a count of filtered dataset and displaying the results on the page. I have written some code which seems to work in filtering a dataset (#dataset13) by a combination of 4 dropdown boxes. The filter is undertaken by clicking on button3. I have also coded a reset which sets the values in the dropdown boxes to “” and loads the complete dataset. I wish to display the number of filtered records in three separate ways

  1. All the dataset records when the page loads
  2. The filtered dataset records on clicking button3
  3. All the dataset records when resetting the dropdown boxes by clicking button5

The code I have written is outlined below and is not working in the following way

  1. The #text2 box displays “text” when the page loads not the total number of records
  2. The total number of records is displayed if button3 is clicked with no filter parameters entered
  3. When I put a filter parameter in any of the dropdown boxes and click button3 then the number of filtered records displayed remains the same eg the total records
  4. If I click button3 a second time then the filtered number of records is displayed
  5. The same process happens with the reset button5
    I hope you can help as I have spent hours on this trying to resolve it. As I mentioned earlier I am very very new to coding
    Thank You
    Trevor
import wixData from "wix-data";
$w.onReady(function () {

});
export function button3_click(event) {
//Add your code for this event here:
$w("#dataset13").setFilter(wixData.filter()
.contains("location", $w('#dropdown1').value)
.contains("type", $w('#dropdown2').value)
.contains("year", $w('#dropdown4').value)
.startsWith("distance", $w('#dropdown3').value, )
);
let count = $w("#dataset13").getTotalCount().toString();
$w('#text2').show();
$w('#text2').text = count + " results found";
}
export function button5_click(event) {
//Add your code for this event here:
$w('#dropdown1').value = "";
$w('#dropdown2').value = "";
$w('#dropdown3').value = "";
$w('#dropdown4').value = "";
$w("#dataset13").setFilter(wixData.filter()
);
let count = $w("#dataset13").getTotalCount().toString();
$w('#text2').show();
$w('#text2').text = count + " results found";
}

In case with filters:
Since you are filtering you can just count the items in the dataset after you are done filtering using .then() .
When you have a .setFilter on your dataset, add a .then and get the total count, then just put this value as the text of the text box
It would look something like this:

$w('#dataset1').setFilter(newFilter).then((num) => {
  console.log($w('#dataset1').getTotalCount(), "heres count");
  $w('#SearchCount').text = $w('#dataset1').getTotalCount().toString();
});;

Also check out these forum posts on the similar issue:

Hi Jevheniia - Thank you for your help it is greatly appreciated - By using one of the links you supplied i have managed to code the total records being displayed in the textbox on the page loading - I am now working on the filtered number of records being displayed when button3 or button5 is clicked. One of the links recommended using the .then command after the filter has been completed - I have tried to code this but its not working its showing an error - Got a feeling it is my coding

import wixData from "wix-data";
$w.onReady(function () {
$w("#dataset13").onReady( () => {
let count = $w("#dataset13").getTotalCount().toString();
$w('#text2').show();
$w('#text2').text = count + " results found";
} );
});
export function button3_click(event) {
//Add your code for this event here:
$w("#dataset13").setFilter(wixData.filter()
.contains("location", $w('#dropdown1').value)
.contains("type", $w('#dropdown2').value)
.contains("year", $w('#dropdown4').value)
.startsWith("distance", $w('#dropdown3').value, )
.then(() => {
let count = $w("#dataset13").getTotalCount().toString();
$w('#text2').show();
$w('#text2').text = count + " results found";
})
}
export function button5_click(event) {
//Add your code for this event here:
$w('#dropdown1').value = "";
$w('#dropdown2').value = "";
$w('#dropdown3').value = "";
$w('#dropdown4').value = "";
$w("#dataset13").setFilter(wixData.filter()
);
let count = $w("#dataset13").getTotalCount().toString();
$w('#text2').show();
$w('#text2').text = count + " results found";
}

Hi Jevheniia - Thank you for your help - I have resolved my coding and it now works