Paging problem along with search field

My application’s pagination has a small problem …
When I put the vagas: active_search, the pagination stops running …
In my page there is a search field … active_search
can you help me?

Controller

  def index(conn, params, user) do
    page = Vaga
            |> Repo.paginate(params)
   active_search = Registro.list_search(params)
    render(conn, "index.html", user: user, vagas: page.entries, page: page, vagas: active_search)
# render(conn, "index.html", user: user, vagas: page.entries, page: page) <<<<<<<< Paging works this way
  end

Sorry for the lack of knowledge …
I was able to resolve with active_search: active_search

I was just mistaken …

Search method, still with problem.

Only 1 works.

You are going to need to give us more information in order to help. Do you have an error message you could show us?

In fact,
There is no mistake.

It simply does not list the search field, in the case active_search

I believe it’s because of the
active_search = Log.list_search (params)
|> Repo.paginate (params)

or
Template

   <form class="form-inline my-2 my-lg-0">
    <%= form_for @conn, vaga_path(@conn, :index), [method: :get], fn f -> %>
      <%= search_input f, :query, placeholder: "Buscar", class: "form-control mr-sm-2" %>
      <%= submit "Buscar", class: "btn btn-outline-success my-2 my-sm-0" %>
      <% end %>
    </form>

It would only be a deduction

render(conn, "index.html", user: user, vagas: page.entries, page: page, vagas: active_search)

You seem to have passed in the :vagas option twice. Once for page.entries and once for active_search. Did you mean to call one of them differently, or maybe remove one of them?

You are listing them both in vagas … You can probably merge them into one.

Would you have an example of what it would look like?

vagas = 
  page.entries
  |> Enum.filter(fn entry -> entry.id in active_search end)

Would need to know how page.entries and active_search actually look like to be more specific. What exactly you are trying to accomplish, why are you both querying both for pages and performing a search?

I currently have a list of 20 records

I wanted to add the pagination in that list;
That’s why I put the

  page = Vaga
        |> order_by([p], desc: p.datapostada_vaga)
        |> Repo.paginate(params)

Then I had to add the search field to the controller
active_search = Registro.list_search(params)

I tried to separate the search method, but I did not succeed.
That’s why I’m insisting on this index

I used this tutorial
to add the search field.

For pagination

Do you want to paginate the search results?

necessary, page in the initial query and also in the pagination of each result

If you want to paginate the search result, then you don’t need

page = Vaga
            |> Repo.paginate(params)

Just add MyApp.Repo.paginate(params) to your search query.

But without it I can not run my application.
Because of the template index

<%= pagination_links @page %>

I meant you don’t need the query Vaga |> Repo.paginate(params). You can store you paginated search results into page.

1 Like

Thanks a lot for the help.

I was able to solve it by adding the whole query in the page.

Hi gilbertosj!
Mind sharing the final code? Trying to solve the exact same problem but I’m finding it difficult to follow the conversation in this thread.

No problem.
Just add as follows.

  search_term = get_in(params, ["query"])
      page = Vaga
            |> Vaga.search(search_term)
            |> order_by([p], desc: p.datapostada_vaga)
            |> Repo.paginate(params)

Thanks @gilbertosj, It’s paginating my search queries now!:blush:

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

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

However, did you get your Scriverner_html pagination links to work with your paginated search results?

In this screenshot you can see that it successfully provided a result set based on query.

But as soon as I click on any of the links generated by <%= pagination_links @page %>, the query parameter gets dropped

@page doesn’t appear to include the query parameters.
This would be fine if the application was just an API, but I’m wondering if you got this work with scrivener_html paging links.

1 Like

Huuum …
This would be new to me …
Actually, I had not even done this kind of test.
More like you commented …
I’ll find out how to solve it.