Skip to content

Commit

Permalink
Fixes #19 - restrict pagination links to groups of 10
Browse files Browse the repository at this point in the history
  • Loading branch information
darinwilson committed Sep 19, 2016
1 parent e21c49d commit 6277669
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<% distance = Map.get(@conn.assigns, :distance, 5) %>
<ul class="pagination">
<li>
<%= prev_link(@page_number, @total_pages) %>
</li>
<%= if @total_pages > 1 do %>
<%= for num <- 1..@total_pages do %>
<%= for num <- start_page(@page_number, distance)..end_page(@page_number, @total_pages, distance) do %>
<li>
<a href="?<%= querystring(@conn, page: num) %>" class="<%= if @page_number == num, do: "active", else: "" %>">
<%= num %>
Expand Down
20 changes: 20 additions & 0 deletions apps/torch/lib/torch/views/pagination_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@ defmodule Torch.PaginationView do
link "Next >", to: "?page=#{current_page + 1}"
end
end

defp start_page(current_page, distance) when current_page - distance < 1 do
current_page - (distance + (current_page - distance - 1))
end
defp start_page(current_page, distance) do
current_page - distance
end

defp end_page(current_page, 0, _distance) do
current_page
end
defp end_page(current_page, _total, distance) when current_page <= distance do
distance * 2
end
defp end_page(current_page, total, distance) when current_page + distance >= total do
total
end
defp end_page(current_page, _total, distance) do
current_page + distance - 1
end
end
4 changes: 3 additions & 1 deletion apps/torch/priv/templates/elixir/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule <%= module %>Controller do
@filtrex []
<% end %>
@pagination [page_size: 10]
@pagination_distance 5

def index(conn, params) do
{:ok, filter} = Filtrex.parse_params(@filtrex, params["<%= singular %>"] || %{})
Expand All @@ -33,7 +34,8 @@ defmodule <%= module %>Controller do
page_number: page.page_number,
page_size: page.page_size,
total_pages: page.total_pages,
total_entries: page.total_entries
total_entries: page.total_entries,
distance: @pagination_distance
end

def new(conn, _params) do
Expand Down

0 comments on commit 6277669

Please sign in to comment.