getTotalCount gives count of previous filter

Hi

I’m new to Javascript , indeed new to web development. and am struggling with what I thought would be a simple scenario :

  • Successfully filter a dataset using SetFilter()

  • Records correctly display in an associated repeater

  • Attempt to get a count of rows after the filter using getTotalCount()

but getTotalCount() ALWAYS returns the number of records prior to applying the filter.

I’ve read that I need to ensure dataset is ‘ready’ and can do this using OnReady , which I have tried to incorporate into my code:

function filter(SearchCriteria, exactmatch) {
if (exactmatch) {
$w(“#dataset1”).setFilter(wixData.filter()
.startsWith(“composer”, SearchCriteria));
}
else {
$w(“#dataset1”).setFilter(wixData.filter()
.contains(“workName”, SearchCriteria));
}

$w("#dataset1").onReady( () => {     
	console.log("dataset is now filtered.");            
       	let totalCount = $w("#dataset1").getTotalCount();      
	$w('#text17').text = totalCount.toLocaleString('en');     
} ); 

totalCount is always the number of records prior to the filter.

Any ideas?

Many thanks
Gareth

@garethdunn When is the filter method called? If it’s triggered by the user interacting with an element (clicking a button), then the dataset is surely loaded by then. The dataset onReady is useful to use within a page onReady when you want to filter a dataset immediately after all of the page elements and the dataset are loaded.

The setFilter does not take long to execute, but the next lines of code are normally already evaluated by the time the setFilter has done its thing. So what you could do is use .then() condition on it, so that you know that the setFilter is complete before trying to obtain the new count.

function filter(SearchCriteria, exactmatch) {
 let totalCount = 0;
 if (exactmatch) {   
    $w("#dataset1").setFilter(wixData.filter()
    .startsWith("composer", SearchCriteria))
    .then(() => {
        totalCount = $w("#dataset1").getTotalCount();
        $w('#text17').text = totalCount.toLocaleString('en');    
     })          
  } else {
     $w("#dataset1").setFilter(wixData.filter()
     .contains("workName", SearchCriteria))
     .then(() => {
        totalCount = $w("#dataset1").getTotalCount();
        $w('#text17').text = totalCount.toLocaleString('en');    
      })
    }
}

Hi Anthony,

The above code is triggered from an OnChange event from a dropdown list. The dataset will (should) be fully populated by then, moreso as it only contains a few records at present

As a test case, I placed a button on the page and when clicked to execute the below code it always gives the correct result.

let totalCount = $w( “#dataset1” ).getTotalCount();
$w( ‘#text17’ ).text = totalCount.toLocaleString( ‘en’ );

So in line with your own thinking, I suspect it must be some asynchronicity (if such a word !) aspect of javascript (which is new to me) which is causing my pain.

I will explore .then and report back, many thanks for your help.

Your code works :slight_smile:

Thank you so much , I was tearing my hair out with this. All part of the learning curve.

Cheers,
Gareth

Glad it’s working for you. It can take a bit of time to wrap your head around how things work in web development if all of your previous coding experience was elsewhere.