Data Management

How to Sort and Filter Add-ons

Summary

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.

Filtering Add-ons

To filter add-ons, follow these steps:

  1. 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.
  2. 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.
  3. Query the main table: Use the `Query All Records` function to retrieve data from your main table (e.g., `users`).
  4. 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.
  5. Filter the add-on data:
  • Create an empty array variable to store the filtered data.
  • Use a `for` loop to iterate through the separated add-on data.
  • Within the loop, use the `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).
  • Update the filtered data array with the filtered add-on data.
  1. Filter the main data:
  • Create an empty array variable to store the filtered main data.
  • Use a `for` loop to iterate through the main data.
  • Within the loop, use the `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).
  • Update the filtered main data array with the matched data and filtered add-on data.
  1. Return the filtered data: Return the filtered main data array, which now includes the filtered add-on data.

Sorting Add-ons

Sorting add-ons is a simpler process than filtering, thanks to Xano's XenoTransform feature. Here's how you can sort add-ons:

  1. Create a variable: Create a variable and assign it the value returned from your main query (e.g., `users`).
  2. 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`).
  3. Apply the sorting expression:
  • To sort by name, use the expression `$$_products.of_company|sort('name')`.
  • To sort by release date, first convert the date field to a numeric timestamp using `$$_products.of_company|map(x=>x.release_date=Date.parse(x.release_date))`, then sort by the `release_date` field: `|sort('release_date')`.
  1. Return the sorted data: Return the variable containing the sorted add-on data.

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.

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free