How to filter a database query?

Ok so it’s not really hard to do this.
On your page you need a text input and a select:

I named them name and category but you can add whatever names you need.

Your select needs an option without a value <option>Show all</option>

Screenshot 2021-05-25 at 10.38.19

Then in your server action create two GET variables for the filters, these GET vars will get the values from the two inputs on the page:

Screenshot 2021-05-25 at 10.38.42

And in the query builder create two condition groups:

Some explanation here, the ? icon represents a condition - i.e. where should the filter be applied. In your case there you bind the same as selected for the filter values. I.e. when the input has a value, then this filter will be applied, otherwise it will show all the records.

Make sure to select OR, as when AND is used, then the two inputs are always required and in your case you need to be able to use them separately.

And finally on your page, bind the two inputs value to the server connect component:

4 Likes