Paging problem along with search field

Usually pagination links include all the query params of the search form as well.

Not sure if this is the right way to go about it:

search_term = get_in(params, ["query"])
page = Expense
  |> Expense.search(search_term)
  |> Repo.paginate(params)

render(conn, "index.html", expenses: page.entries, page: page, query: search_term)

I render the search_term value using “query: search_term” in the render.
Then in the template, I call it using @query:

<%= pagination_links @page, query: @query %>

So now the pagination links get the value of @query

And it now works as wanted.
Since this is Phoenix 1.3, I moved the search to the context.

Final code is:

MyApp.Context

  def list_expenses(params) do
    search_term = get_in(params, ["query"])
    Expense
    |> Expense.search(search_term)
    |> order_by(desc: :date)
    |> Repo.paginate(params)
  end

Which is then called in the Controller

 def index(conn, params) do
    expenses = Tools.list_expenses(params)
    search_term = get_in(params, ["query"])
    render(conn, "index.html", expenses: expenses.entries, page: expenses, query: search_term)
  end
1 Like

You can always use turn_the_page, which is fast and simple app to paginate :slight_smile:

which is fast and simple app to paginate

It seems that it is offset-based, which is not exactly fast for bigger datasets …

1 Like

It’s for small data then :slight_smile: