Ash FrameworkAF
Ash Framework9mo ago
18 replies
Joan Gavelán

Guidance on Dynamic Filtering, Sorting, and Pagination

Hello Ash community,

I'm implementing standard filtering, sorting, and pagination features for my contacts module and have defined a read action as follows:
define :list_contacts, action: :read

In my controller, I parse incoming parameters with default values:
search_query = Map.get(params, "search", "")
current_page = Map.get(params, "page", "1") |> String.to_integer()
sort_by = Map.get(params, "sort_by", "created_at")
sort_dir = Map.get(params, "sort_dir", "desc")

I invoke the function as:
Contacts.list_contacts!(actor: current_user)

I'd like to understand how to pass these parameters to effectively filter, sort, and paginate the contacts list. Specifically:

- How can I apply a case-insensitive search filter to multiple attributes and only when search_query is present?
- What's the recommended way to implement dynamic sorting based on sort_by and sort_dir?
- How should I handle pagination using current_page?
- Are there best practices for sanitizing and validating these parameters before applying them?

Any guidance or examples on implementing these features would be greatly appreciated.

Thanks in advance!
Was this page helpful?