I'm thinking of customizing pagination using kaminari in Ruby on Rails.
The current pagenation is below.
The customization I want to do will be displayed on 5 pages as shown below.
(Previous) (1) (2) (③) (4) ・ (10) (Next)
(Previous) (1) ・ (3) (④) (5) ・ (10) (Next)
* ○ is the display page.
If i have a solution here, I would appreciate it if you could teach me.
If i have any necessary files, please let us know.
<ul> <li><% = paginate @admins%></li> </ul>
def news @admins = Admin.order ("created_at DESC"). page (params [: page]). per (1) end
ja: time: time: formats: formats: default: "% Y% m month% d day" views: pagination: previous: "previous" next: "Next" truncate: "・"
#frozen_string_literal: true Kaminari.configure do | config | config.default_per_page = 8 config.max_per_page = nil # config.window = 3 # config.outer_window = 0 # config.left = 0 # config.right = 0 # config.page_method_name =: page # config.param_name =: page # config.max_pages = nil # config.params_on_first_page = false end
What I tried
<% = paginator.render do%> <nav role = "navigation" aria-label = "pager"> <% = prev_page_tag%> <% each_page do | page |%> <% if page.display_tag?%> <% = page_tag page%> <% elsif! page.was_truncated?%> <% = gap_tag%> <% end%> <% end%> <% unless current_page.out_of_range?%> <% = next_page_tag%> <% end%> </nav> <% end%>
I tried to touch views and config with reference to the article, but there was no change.
Answer # 1
Refer to the article
There is, but I think that it is better to get into the habit of seeing the formula as much as possible.
It looks like the following in the default state without using a template.
After that, I think that I should arrange the layout, translate it into Japanese, and delete First and Last.
<% = paginate @admins, window: 1, outer_window: 1%>
* I couldn't find a way to delete first and last, but if you're not particular about it, you may delete it with css.
When the first page is displayed, it will look like the one below, so it seems necessary to take some measures such as making a conditional branch and calling the one with the outer increment or decrement.
It is a further addition.
Premise: With gem installed (kaminari config is not set, paginator is not customized)
With the code belowAlmostIt can be realized. (Actually, it is better to write else properly)
<% n = 1 n = 3 if @ pages.current_page == 1 || @ pages.current_page == @ pages.total_pages n = 2 if @ pages.current_page == 2 || @ pages.current_page == @ pages.total_pages --1 %> <% = paginate @pages, window: n, outer_window: 1%>
AlmostBecause when you display page 4,
Prev 1 2 3 4 5 ... 25 NextIt will be. (* Same for page 22)
If you skip two or more characters, it will be ..., but if you skip one character, it will not be ... It seems to be the specification of kaminari.
Answer # 2
The settings here are still commented out, what happens if I comment them out?
#frozen_string_literal: true Kaminari.configure do | config | config.default_per_page = 8 config.max_per_page = nil config.window = 3 # Comment out here config.outer_window = 1 # Comment out here # config.left = 0 # config.right = 0 # config.page_method_name =: page # config.param_name =: page # config.max_pages = nil # config.params_on_first_page = false end
- ruby on rails 6 - about validation (number of characters) of rails tweet posting
- ruby on rails - rails render: about the matter that the url after new is different
- i want to move to the second page about the pagination function of laravel
- about description of manifest file of ruby on rails
- ruby - about accelerating heroku's rails app
- ruby on rails 5 - about lint/syntax error of rubocop
- ruby - about the problem of duplicate devise error messages (allow_blank: ture doesn't work!)
- ruby - about the error content displayed in "rails" rspec
- ruby on rails - about image posting page argumenterror
- ruby - [rails] about partial templates
- ruby on rails - about multi-word search to multiple columns using ransack
- [ruby on rails] about header setting using bootstrap
- ruby on rails - about screen transition from rails top page to calendar page
- ruby on rails 6 - about rails error "wrong number of arguments (given 1, expected 0)"
- ruby on rails - about rails create action
- ruby - about record duplication
- ruby on rails - about nomethoderror in skilscontroller # create
- about ruby exception method stackrace
- ruby - about rails controller new action
- ruby - i want to be able to see "liked posts" by the person on the rails development user detail page
- ruby - about viewport introduction
- ruby - i want to display new posts i made on the top page only for myself
- ruby - nomethoderror at post time with tag function implementation
- ruby - rails6 active_strage cannot be installed
- ruby - data including rails hyphen becomes # and is not displayed
- ruby - i don't feel that the sql issued by rails has a different format from the sql that i usually write
- html - about boot strap free introduction
- ruby - [rails6] i get an error on heroku when i write