How to filter a database query?

I should do a filter on a query using two fields.
One is a text field (“testofrm”), the other is a select that passes a numeric value (“odcfrm”). If I use both of them (the value from select and then I write a text), it works.
But I would like it to pass the value “all” by default so that I can search a text without using menu or search only through the drop-down menu



25

Have a look at creating groups in your query, and then use the Toggle Condition (?) to create an expression that when true will include that group in the query, and when false, the group will be ignored.

For example the following is a group that searches country_name, but only if the search_text GET var is provided.

With groups you can likely change your strategy so that the value ‘all’ is no longer needed, but even if it is, you can use it in the toggle condition.

Hello 4poli

If I understand your goal correctly, then you can achieve it in the following way:

  1. All your filters in the request are in the same group. Divide them into different groups.
  2. Then, in those groups that are responsible for the filter for “testofrm” and “odcfrm”, specify the condition (highlighted the icon on your screen):
  3. In the conditions, just specify the appropriate fields. This will mean that a group with a filter will only work if the field contains data. If the field is empty, the filter group will not work.

If I understand you correctly, this will do exactly what you need.

I still have a problem, how do I get the first value in the menu (select all) to be “all”? Values ​​are numeric. Now I have written% but I know it is wrong

26|417x322

Well, combining two functions in one select (a separate selection and the selection of all values at once) requires a different, more complex approach.

The most rational solution that comes to mind is as follows:

  1. To get all the values in the input without overloading the client part with unnecessary actions, you will need to slightly supplement the server action, which is responsible for supplying values for the select. You will need to add another Set Value step with all the values that you use in the select combined into a string. It looks like this:

  2. Then in the select, instead of %, you select the value Set Value, which will come in parallel with the query for the select:

  3. After that, you will have to redo your “odcfrm " filter. Because in it you use equal, which works as a strict equality and will not work in the case of the “select all” option. For the filter to work correctly, you need to change the condition from equal to in, and specify the following values in the value: {{$_GET.odcfrm. split(",")}}

In this case, you will get what you need

Sorry but after doing that I can’t find the value “allselect” in DataBindings.
What I’m doing wrong?

1
2
3
4

You need to tick the output box in the properties of the api action.

ok thanks… but unfortunately the filter does not work


6

Why do you have mixed up the filtering conditions in places? Where contains {{$_GET.testform}} is used, the conditions must contain {{$_GET.testform}}. And in the group with in {{$_GET.odcfrm.split(",")}} in the conditions should be {{$_GET.odcfrm}}. In this case, everything should work correctly.

Yes it was an error… but after i correct that, nothing changes
it’s even worse because the search filter doesn’t give any results :frowning:

Not sure if i am more confused than by exactly are you trying to achieve or the replies you got here.
So can we start from the beginning?

What i understand is - you have a text field and a select menu.

My question is - how do you want to use them to filter your query? So - when nothing is selected in the select menu, or entered in the text field - show all?
And then - how should the two fields work? Separately? Or do you always expect to have values in both?
Should the user be able to filter the data using any of the two fields and also both together?

I should be able to use the two search filters separately, but if one of them is not used it must have the value “all” (both the menu and the text field).
Let me explain:
the user must be able to search for the name of a company (by text) or the type to which it belongs (with the drop-down menu, which gives the query a numerical value)
If the user searches only for the type, the text will necessarily be “all”.
If the user searches for the text, the type will necessarily be “all”

1 Like

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

Good job! thank you very much
I made two little variations (my select menu is dynamic) but it works.

1 Like

Apologize. I misunderstood your original task. The solution that I proposed is aimed at the ability to filter both by individual fields, and create a cross-search. As it turned out, you needed a simpler solution in the form of the ability to search for individual fields.