Last active
January 4, 2017 11:28
-
-
Save acrolink/dba8a0a5e62dc49ee3c95d81e5139443 to your computer and use it in GitHub Desktop.
Filtering results in Phoenix using an exposed filters' form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.. | |
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