Welcome to another Xano tutorial! Today, we'll explore a powerful feature called "external filtering" that allows you to dynamically assign columns and operators in your custom queries. This comes in handy when you need to filter your data based on user input or changing conditions. Let's dive in!
First, let's create some inputs that will represent the different parts of our filter:
left for the column nameoperator for the operator (e.g., =, >, <, etc.)right for the value to filter byNext, we'll create a variable that will hold our dynamic filter expression. Follow these steps:
filterfilter variableleft, operator, and right inputs to their respective placeholders in the JSON modelYour filter variable should now look something like this:
{
"filters": [
{
"left": "{{left}}",
"op": "{{operator}}",
"right": "{{right}}"
}
]
}
Now that we have our dynamic filter set up, we can use it in our custom query:
filter variable thereThat's it! You can now test your dynamic filter by providing different values for the left, operator, and right inputs. For example, if you set left to "userId", operator to "=", and right to "2", your query will only return records where the userId column is equal to 2.
But what if you need to filter based on multiple expressions? No problem! Here's how you can do it:
filter2 with the following value:{
"external": {
"expression": [
{
"left": "{{left}}",
"op": "{{operator}}",
"right": "{{right}}"
},
{
"left": "{{left2}}",
"op": "{{op2}}",
"right": "{{right2}}"
}
]
}
}
left2, op2, and right2filter and filter2:{{filter}}.addition.{{filter2.external}}
Now, you can filter based on multiple expressions by providing values for all the input fields.
By default, multiple expressions are combined using the AND operator. However, you can change this to OR by adding the following line to your filter2 variable:
"or": true
This way, your query will return records that match either of the provided expressions.
That's it! You now know how to dynamically assign columns and operators in your custom queries using external filtering. This powerful feature opens up a world of possibilities for building flexible and user-friendly applications. Keep exploring and happy coding!
Join 100,000+ people already building with Xano.
Start today and scale to millions.