Hi Ron:
OK - so I should have asked before - what are you trying to do and why isn’t it working? The problem is that you have targeted us at a narrow problem - which isn’t your real problem
You need to understand how data collections work and how these handlers do what they do.
When you set a filter on a dataset it creates a Promise for you. Think of the Promise as an egg timer which goes off at some point when your eggs are cooked. In this instance the Promise takes a function that gets called when the setFilter activity is complete.
This is where you need to do your count updates like this:
$w('#dataset1').setFilter( wixData.filter().contains(srchFld,v))
.then(() => {
let count = $w("#dataset1").getTotalCount();
// Insert count into display string making sure to convert number to string
$w("#txtRecCount").text = "There were " + count.toString() + " books found";
})
.catch((error) => {
console.log(error);
});
Now having had a look at your code you may want to consider some refactoring to simplify the way you have written your code and make it easier to manage and debug.
- Consider using a switch statement in your search function instead of if-then-elseif-elseif-else-endif,
- In your conditional statements consider only building up the filters by combining wixData.filters into a common filter variable.
- With a completed common filter variable you only need to make a single setFilter call which will mean you only have to perform the getTotalCount in one place.
- Since you probably want to set the total count on page load as well as when you perform a search you probably want to create an updateDatasetCount() function or similar to perform the element value update. Then you just call it when you need it, on dataset ready and after a filter event or any event that changes the dataset collection.
So
if (srchFld === "pages" || srchFld === "copies") {
v = Number(v);
$w('#dataset1').setFilter( wixData.filter()
.eq(srchFld,v));
}
else if (srchFld === "author") {
let Auth1 = wixData.filter().contains("author",v);
let Auth2 = wixData.filter().contains("author2",v);
let Auth3 = wixData.filter().contains("author3",v);
let Auth4 = wixData.filter().contains("author4",v);
let Auth5 = wixData.filter().contains("author5",v);
let Auth6 = wixData.filter().contains("author6",v);
$w("#dataset1").setFilter(Auth1.or(Auth2).or(Auth3).or(Auth4).or(Auth5).or(Auth6));
}
else if (srchFld === "category") {
let Cat1 = wixData.filter().contains("cat1",v);
let Cat2 = wixData.filter().contains("cat2",v);
let Cat3 = wixData.filter().contains("cat3",v);
$w("#dataset1").setFilter(Cat1.or(Cat2).or(Cat3));
}
else {
$w('#dataset1').setFilter( wixData.filter()
.contains(srchFld,v));
}
$w('#dataset1').onReady(() => {
let count = $w("#dataset1").getTotalCount().toString();
$w("#txtRecCount").text = "There were " + count + " books found";
});
Can become [typos permitting ;-)]…
export function btnSearch_click(event, $w) {
let v = $w('#txtSearchTerm').value;
let srchFld = $w("#ddSearchField").value;
// Master filter variable
let masterFilter = null;
switch(srchFld) {
case "pages":
case "copies":
{
v = Number(v);
// Configure the filter
masterFilter = wixData.filter()
.eq(srchFld,v);
}
break;
case "author":
{
let Auth1 = wixData.filter().contains("author",v);
let Auth2 = wixData.filter().contains("author2",v);
let Auth3 = wixData.filter().contains("author3",v);
let Auth4 = wixData.filter().contains("author4",v);
let Auth5 = wixData.filter().contains("author5",v);
let Auth6 = wixData.filter().contains("author6",v);
// Configure the filter
masterFilter = Auth1
.or(Auth2)
.or(Auth3)
.or(Auth4)
.or(Auth5)
.or(Auth6);
}
break;
case "category":
{
let Cat1 = wixData.filter().contains("cat1",v);
let Cat2 = wixData.filter().contains("cat2",v);
let Cat3 = wixData.filter().contains("cat3",v);
// Configure the filter
masterFilter = Cat1.or(Cat2).or(Cat3);
}
break;
default:
{
// Configure the filter
masterFilter = wixData.filter()
.contains(srchFld,v));
}
break;
}
// Execute the filter and set up post processing in the Promise
$w('#dataset1').setFilter(masterFilter)
.then(() => {
updateDatasetCounter();
})
.catch((error) +. {
console.log(error);
});
}
// Function to set the value of the number of items in the current dataset
function updateDatasetCounter() {
let count = $w("#dataset1").getTotalCount().toString();
$w("#txtRecCount").text = "There were " + count + " books found";
}
Hope this helps!
Steve