Skip to content

Instantly share code, notes, and snippets.

@acrolink
Last active January 4, 2017 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrolink/dba8a0a5e62dc49ee3c95d81e5139443 to your computer and use it in GitHub Desktop.
Save acrolink/dba8a0a5e62dc49ee3c95d81e5139443 to your computer and use it in GitHub Desktop.
Filtering results in Phoenix using an exposed filters' form
<h2>Listing policies</h2>
<form action="/policies" method="get">
<%= select nil, :client_id, @clients, name: 'client_id' %>
<input type="submit" value="Submit">
</form>
<%= pagination_links @page %>
<!-- HTML displaying the data rows -->
..
def index(conn, _params) do
page =
Policy
|> QueryFilter.filter(%Policy{}, _params, [:client_id])
|> join(:left, [p], c in Client, p.client_id == c.id)
|> where([p, c], c.user_id == ^conn.assigns.current_user.id)
|> preload([p, c], [client: c])
|> Repo.paginate(_params)
render conn, :index,
policies: page.entries,
page_number: page.page_number,
page_size: page.page_size,
total_pages: page.total_pages,
total_entries: page.total_entries,
page: page,
end
..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment