Rails Pagination - first page items amount - ruby-on-rails

Is there a way to put more items on the first page than the rest of the pages, when using pagination in Rails?
I am using the Kaminari gem to paginate a list of items and have implemented infinite scroll to browse through the pages. I want to limit the number of listings loaded per page when scrolling, but would like the initial page to start with a significant amount. Below is the current call to Kaminari in my listings controller:
#listings = Listing.page(params[:page]).per(25)

Something like this?
#listings = Listing.page(params[:page]).per(params[:page].to_i == 1 ? 100 : 25)

I solved the problem like this (First page items is supposed 2 items and next pages 7 items):
per_page = 7
#comments = #content.comments.page(params[:page]).per(per_page).padding((per_page-2) * -1)

Related

Is there a way to do cursor pagination in searchkick?

I'm using kaminari with searchkick to paginate my data, getting my search results by calling the method like so:
Product.search(#query, where: conditions[:where], order: conditions[:order], page: #page, per_page: 20)
My data is updated frequently, with more products being added so if a client is on page 1 and navigates to page 2, they might see products that they already saw in the previous page. Is there a way around this? I'm not very sure how to handle this using searchkick and would appreciate any pointers.

rails pagy multiple tables on one page

I have two different tables on one page and I want to use pagy on each of them.
For sorting/searching I changed names of each pagy object so I did it like
#pagy_inv, #invoices = pagy #invoices.reorder(sort_column_show_city_invoices => sort_direction_show_city_invoices), items: params.fetch(:count, 10)
and
#pagy_ord, #orders = pagy #orders.reorder(sort_column_show_city_orders => sort_direction_show_city_orders), items: params.fetch(:count, 10)
which works fine for sorting and searching, but when I try to change page, it sends page parameter and tries to change page for every table and therefore if one table has like 5 pages and another 10 and I try to move to page 10 it fails because one of the table cannot move to not existing table.
I was thinking to change page param (for example page_inv and page_ord) but how to do that? Or is there some easy way how to change page only for table from selected pagy object?
As you already understood, pagy use a default :page_param (which is :page): if you use more than one instance in the same page you should differentiate the :page_param of different instances or they will use all the same.
You can pass that in the pagy method (as you did with :items).

Grails gsp pagination not breaks list item

Today when i customize list method of my controller,facing issue in list.gsp page.My problem is that all records are displayed on a single page.
for ex. Suppose i have 15 records then all 15 will show on first page and also there in next link at footer.when clicks on it all pages show same 15 records.
I was customize code because i need to run query and basis of result of query show records on list page.
def query = "from Book where isAvailable = 'true'"
def bookInstanceList = Book.findAll(query)
[bookInstanceList: bookInstanceList , bookInstanceTotal: bookInstanceList.size()]
it shows all records on a single page and records are not paginate on different page.
i want to show maximum 10 records on each page.
please reply me where i am missing.
Thanks in advance
So pagination will not work that way. You are getting your total of the resulting number of rows that were returned, not what would have been returned without pagination.
def bookQuery = Book.where{ isAvailable == 'true' }
[bookInstanceList: bookQuery.list(params), bookInstanceTotal: bookQuery.count()]
params should contain max and offset to implement pagination.

will_paginate on limite records

I have a problem to paginate the record that I request from other web service to get data. When i make a request i get some data(not all because if i get all it will be too big) and the total number of records. will_paginate seem can only display the first page and from the second page it show nothing.
page = params[:page]? params[:page]:1
#fields = ConflictCase.get_fields
sites = ConflictCase.get_paging_sites_from_service(10, (page.to_i - 1)) #this will request data from other webservice that I limit only 10 records
#conflict_cases = ConflictCase.transform(sites["sites"], #fields)
#conflict_cases = #conflict_cases.paginate(:page => page, :per_page => 10, :total_entries => sites["total"].to_i)
The problem is that i always get only 10 records so it can display on the first page but from the second page it show nothing. I though because will paginate will work on it own to ignore the first 10 records when we move to second page or more.
Have anyone can suggest any idea?
Thank for your help
This is a perfect scenario to use the ruby debugger, or spit out to the logger the values during each iteration of your objects.
Firstly I would assume will_paginate isn't the issue, so I would debug to look at the #conflict_cases object and what is being passed into the paginate() function, and the values each time for page and sites['total'].

How can I set default page to the last page when using pagination?

Assuming there are 115 communities.
Then it show 10 communities per page with pagination.
When I'm going to example.com/communities, it shows 10 oldest records.
However, I want it to link to example.com/communities?page=12 as default.
It should be linked to the last page of communities.
Is it possible?
Now my link is just like this
<%= link_to "Communities", communities_path %>
This is possible but you have to add some logic
communities = Community.page(params[:page])
communities.total_pages # 12
communities_path(page: communities.total_pages)
or you can just reverse the ordering of your query.
Another way of thinking about it. Change your order by so it shows the newest.
So on your arel query, do something like this:
Blog.all.order("id desc")
I left the pagination call out as i do not know what pagination library you are using :).

Resources