Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination is show older rows again #95

Open
sahilpaudel-pe opened this issue Mar 23, 2021 · 5 comments
Open

Pagination is show older rows again #95

sahilpaudel-pe opened this issue Mar 23, 2021 · 5 comments

Comments

@sahilpaudel-pe
Copy link

Below is my implementation I have used order_by condition
The data on the first page is fine but when I paginate to other pages I get 1-2 rows every time from previous pages.

{:phoenix_ecto, "~> 4.0.0"},
 {:ecto, "~> 3.4.0"},
 {:ecto_sql, "~> 3.3"},
 {:postgrex, ">= 0.0.0"},
 {:scrivener_ecto, "~> 2.7"},
def all(%{"rule_set_id" => rule_set_id, "is_archived" => is_archived, "page" => page_number}) do
    page = from(rsv in @model,
      where: rsv.rule_set_id == ^rule_set_id and rsv.is_archived == ^is_archived,
      order_by: [desc: rsv.is_live, desc: rsv.start_time]
    )
    # |> Repo.all()
    # |> preload(@default_preloads)
    |> Repo.paginate(page: page_number, page_size: 5)

    {:ok, %{
           "rule_set_versions" => page.entries |> preload(@custom_preloads),
           "page_number" => page.page_number,
           "page_size" => page.page_size,
           "total_pages" => page.total_pages,
           "total_rows" => page.total_entries
          }
    }
  end
@la-jose
Copy link

la-jose commented Sep 23, 2021

encountering the same issue

@jtomaszewski
Copy link

It might that be that when you ask for a page larger than the last one, it'll give you the last one.

So for example if there's 3 pages, and you ask for 100th page, it'll give you results from the 3rd page.

... which I agree, is surprising to me. Maybe it should return no results in such a case?

@sahilpaudel-pe
Copy link
Author

yes, it should return no data in such case.

@ricardoebbers
Copy link

ricardoebbers commented Aug 10, 2022

I had this problem also. The solution is a bit hidden, but possible with the current implementation.

When you use the Scrivener on your Repo, just pass the allow_overflow_page_number: true option, like this:

defmodule MyRepo.Repo do
  use Scrivener, options: [allow_overflow_page_number: true]
  # ...
end

The next time you query a page greater than the total pages, it will return an empty list for entries:

iex(1)> page_params                                               
%{page: 999, page_size: 50}
iex(2)> Actors.get_by_movie(movie, page_params)
%Scrivener.Page{
  entries: [],
  page_number: 999,
  page_size: 50,
  total_entries: 2,
  total_pages: 2
}

You can also pass that config when calling Scrivener.paginate/2 directly.

@frm
Copy link

frm commented Dec 10, 2022

Thanks @ricardoebbers, was having a hard time with this, Scrivener.__using__/1 is lacking in docs for the options. Since you came up with the solution, maybe you could open a PR for others that find the same problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants