Filter Dataset with 2 filters

Hello,

I have 2 dropbox that helps me filter a dataset.
I want to filter this according to dropbox1 or dropbox1 and dropbox2.
What is the best way to do that?

@oren There are several questions similar to this in the forum.

The dataset has a filter function called setFilter() . As you will see in the documentation for this API a filter is generated using wixData.filter(). In one of the examples provided in the API documentation the basic mechanism for managing multiple filters is show…


In the above example the filter function receives two arguments, low and high. The filter function starts by creating the filter needed by the setFilter() as shown in the last line of code.

Once a filter has been created you can build on it using multiple filter options. In the example two filter options are used:

  • .ge : This filters using values for the selected property being greater than or equal to the value provided. In the example price being greater than or equal to low

  • .le : This filters using values for the selected property being less than or equal to the value provided. In the example length being less than or equal to high
    The code also tests to make sure that a value for the filter exists (low or high) before setting the filter. If a value isn’t provided then the filter will not be set.

Lastly unless you use the .or() filter, each filter applied in the way shown above is combined as an .and() implicitly, so you don’t really need to use .and(). So they are additive and provides the effect you are looking for.

So in answer to your question you need to create a filter function that accepts the values from your drop downs as one of the parameters. The best way to accomplish this is to use the same event handler for each dropdown and then combine the results from each using the filter. So if we take the filter example above we might have a dropdown event handler for two dropdowns that is set up like this:

$w.onReady(() => {
    $w('#dropdown1').onChange(setDatasetFilter);
    $w('#dropdown2').onChange(setDatasetFilter);
}

function setDatasetFilter() {
    let low = $w('#dropdown1').value;
    let high = $w('#dropdown2').value;
    filter(low, high);
}

Hope this helps.

Yes. it helps. Thanks :slight_smile: