Database collection filter issue!

Hello,

I have a database collection and I have an option for user to filter the collection using a filter button and this code in the click event of the filter button : $w(“#datasetname”).setFilter(wixData.filter().eq(“parameter”, parameter_value));

But when I click on the filter button, the filtering option is working fine but other database properties like $w(“#dataset1”).getTotalCount(); is giving the total count of the database before filtering operation. And if i click on the filtering button again, $w(“#dataset1”).getTotalCount(); gives the correct value that corresponds to the number of elements after filtering operation.

That means the user need to click on the filter button 2 times to get the desired properties of my database . Is there any way to solve this issue so that as soon as the filter button is clicked, the database table is updated and the properties of the filtering operation are accessible? Thanks

Hey,

I recommend refreshing the dataset after filtering the dataset and before using the getTotalCount function. Should the issue persists, please provide us with the site URL and the page name so we can have a look.

Have a good day,
Tal.

Hey Tal,
Thank you for your recommendation but adding refresh function is not working everytime.It’s working when the number of database elements after filtering is less and is not working if this number is high(in my case more than 7)

But I got a solution that is working from stack overflow using a delay:

var delay = ( function() { var timer = 0; return function(callback, ms) { clearTimeout (timer); timer = setTimeout(callback, ms); }; })();

Usage:
delay(function(){ // do stuff }, 600 ); // end delay

I guess my database is not getting enough time to update the database properties after the filtering operation. By using the above code, the filtering operation is working as expected. thanks for your support.

I am having the exact same problem …
Both refreshing the dataset and the delay do not work.
Any other idea?
Thanks,

Hi sgcbooking ,
Have you tried to solution of nithinpanand ?

Tal

Hi Tal,
I finally did figure out with the delay solution.
Tks

Hey guys,
I was having this same problem, but I found the proper solution that doesn’t need a delay. The trick is to use “then” after you filter. In this case:

 $w("#datasetname").setFilter(wixData.filter().eq("parameter", parameter_value))
    .then( () => {
        // Do stuff with getTotalCount
    });

Hope that helps anybody else having this issue in the future!

Hi there,

Having the same issue here, and I’m actually using the “.then” solution, but without success! :frowning:

For whatever reason, my “.then” is being called before the count of items in the dataset gets updated.

(...)

$w('#dynamicDataset').setFilter(wixData.filter(
    .eq("minPlayers", 1) )
    .then( checkIfNoResults() )

(...)

export function checkIfNoResults() {
    console.log("Called the function / data = " + $w('#dynamicDataset').getTotalCount())
    
    if ($w('#dynamicDataset').getTotalCount() === 0)
        $w('#noResultsText').show()
    else
        $w('#noResultsText').hide()

}

Any idea of what i might be doing wrong? :confused:

Thanks,
M

Thank You very much. 3 days I try to solve this Issue with Dable Running of GETTOTALCOUNT…

I’m a begginer in WIX but many many years ago at school in Russia I studiing programming and have some expearence. I see that WIX has Isue with assinchronical of running som statements and functions

@mbessa28 I have the same issue and i can’t find solution…
i use the Then. solution but it doesn’t help. it doesn’t synchronize as it should be.