Xano's add-ons feature simplifies the process of retrieving related data from reference tables, eliminating the need for multiple API requests or database functions. However, sometimes you may want to filter or sort the data returned by these add-ons. Let's explore how to accomplish this in Xano.
To filter add-ons, follow these steps:
Set up your database: In this example, we have a users table with a column referencing the company table. The company table contains a name column, and the products table has a name, a reference to the company, and a release_date column.
Create an input: Add a text input field to your API endpoint. This input will be a list of text strings representing the product names you want to filter.
Query the main table: Use the Query All Records function to retrieve data from your main table (e.g., users).
Separate the add-on data: Create a variable to separate the add-on data (e.g., company) from the main query results. This will make it easier to work with the add-on data later.
Filter the add-on data:
for loop to iterate through the separated add-on data.Array Find All Elements function to filter the add-on data based on the input text strings (e.g., filter products where the name is in the input list).for loop to iterate through the main data.Array Find First Element function to match the filtered add-on data with the corresponding main data (e.g., match the company with the user based on the company_id field).Sorting add-ons is a simpler process than filtering, thanks to Xano's XenoTransform feature. Here's how you can sort add-ons:
Create a variable: Create a variable and assign it the value returned from your main query (e.g., users).
Use the Set Filter function: Within the Set Filter function, define the path to the add-on data you want to sort (e.g., _products.of_company).
Apply the sorting expression:
$$_products.of_company|sort('name').$$_products.of_company|map(x=>x.release_date=Date.parse(x.release_date)), then sort by the release_date field: |sort('release_date').By following these steps, you can effectively filter and sort add-ons in Xano, ensuring that you retrieve the desired data in the preferred order.
Join 100,000+ people already building with Xano.
Start today and scale to millions.