Filtering a dataset

Hello there and thx for the support.
Let’s say i have some collection and i want a repeater to display only those that contain the word lets say “cat” and those that contain lets say “dog”. This cannot be done with the editor so we need a little bit of coding… Can someone provide me with the right way to do this?

thx in advance…

Hi Bilisanas,

Are you using a dropdown (with the word “cat”) for this or a textfield where the user can type “cat”?

kind regards,
Kristof

Thx for the reply… No i want this to be done on page load… Sorry i forgot to mention it above… When the page loads i want the dataset to be filtered out…
thx

@bilisanas
Oke,
You won’t need any code for this.
You have a dataset on your page,
You can go to settings of this dataset and add a filter to it.
select the field to be filtered, and select manual input and fill in “cat”
this should work.

kind regards,
Kristof.

@volkaertskristof But i want the repeater to be filtered with those collections containing the word “cat” and with those containing the word “dog” on load. If i do it through the editor and add filter with the word containing the word “cat” and then add another filter containing the word “dog” it doesnt work…that’s what i am talking about…it’s a little more complicated what i am asking…

Can anyone help?

@bilisanas see below snippet:

var newFilter = wixData.filter();
newFilter = newFilter.eq('your_field', "cat");
newFilter = newFilter.eq('your_field', "dog");
$w('#dataset').setFilter(newFilter);

You beat me to it :smiley: ,
Don’t forget to use “cat” and “dog” .
if you don’t use “” it will give an error.

@volkaertskristof well noticed, I’ll edit.

1 Like

@vervoortyves would this be a correct syntax?

import wixData from ‘wix-data’;

$w.onReady(function () {

var newFilter = wixData.filter(); 

newFilter = newFilter.contain(‘your_field’, cat);
newFilter = newFilter.contain(‘your_field’, dog)
$w(‘#dataset’).setFilter(newFilter);

})

and what do i put in ‘your_field’
also it gives an error cat is not defined

sorry new to velo
thx in advance…

@bilisanas
The 'your_field ’ is the column value in the dataset where the “cat” or “dog” should be.

1 Like

About the error, thats what i just said, you should use “cat” instead of just cat

1 Like

seems to be working but there is a problem…First brings all collection and then filters it…

Anyone has an idea?

So i guess we have an unsolved problem by displaying the whole collection and filters after???

@volkaertskristof My friend can you help with the filtering problem i’m having? I managed to filter but then i 'm having a display problem of showing the whole database first…and then filters…can you provide a solution?

Can you show a peace of your database (Screenshot)?

Don’t think its necessary…I see a lot of people have the same issue…

You can use a button and button click you can filter it

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#btnFilter").onClick(()=>{

    var newFilter = wixData.filter().contain('your_field', cat).contain('your_field', dog);

    $w('#dataset').setFilter(newFilter);

});
    

})

@salman-hammed yes i could but i don’t want to show 80 collections and then use a button to filter it… this should happen load… i have accomplished that but the problem is that first it shows the whole database and then gets filtered…is there a way to hide it until filtered?