Pagy là libaray phân trang nhẹ nhất và nhanh nhất cho đường ray hiện nay. Đó là khoảng 100 dòng, Dưới đây là điểm chuẩn về so sánh hiệu suất so với Kaminari
và Will paginate
Để xem so sánh chi tiết hơn ở đây
Bản giới thiệu
Hãy tạo một blog.
1 2 3 4 5 6 7 | rails new blog <span class="token function">cd</span> blog rails g scaffold Post title content rails db:migrate |
Và cho phép thêm một số dữ liệu giả
Tạo một tệp pagy.rb
mới bên trong config/initializers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | <span class="token comment"># encoding: utf-8</span> <span class="token comment"># frozen_string_literal: true</span> <span class="token comment"># Pagy initializer file (3.7.3)</span> <span class="token comment"># Customize only what you really need and notice that Pagy works also without any of the following lines.</span> <span class="token comment"># Should you just cherry pick part of this file, please maintain the require-order of the extras</span> <span class="token comment"># Extras</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras</span> <span class="token comment"># Backend Extras</span> <span class="token comment"># Array extra: Paginate arrays efficiently, avoiding expensive array-wrapping and without overriding</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/array</span> <span class="token comment"># require 'pagy/extras/array'</span> <span class="token comment"># Countless extra: Paginate without any count, saving one query per rendering</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/countless</span> <span class="token comment"># require 'pagy/extras/countless'</span> <span class="token comment"># Pagy::VARS[:cycle] = false # default</span> <span class="token comment"># Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/elasticsearch_rails</span> <span class="token comment"># require 'pagy/extras/elasticsearch_rails'</span> <span class="token comment"># Searchkick extra: Paginate `Searchkick::Results` objects</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/searchkick</span> <span class="token comment"># require 'pagy/extras/searchkick'</span> <span class="token comment"># Frontend Extras</span> <span class="token comment"># Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/bootstrap</span> <span class="token comment"># require 'pagy/extras/bootstrap'</span> <span class="token comment"># Bulma extra: Add nav, nav_js and combo_nav_js helpers and templates for Bulma pagination</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/bulma</span> <span class="token comment"># require 'pagy/extras/bulma'</span> <span class="token comment"># Foundation extra: Add nav, nav_js and combo_nav_js helpers and templates for Foundation pagination</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/foundation</span> <span class="token comment"># require 'pagy/extras/foundation'</span> <span class="token comment"># Materialize extra: Add nav, nav_js and combo_nav_js helpers for Materialize pagination</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/materialize</span> <span class="token comment"># require 'pagy/extras/materialize'</span> <span class="token comment"># Navs extra: Add nav_js and combo_nav_js javascript helpers</span> <span class="token comment"># Notice: the other frontend extras add their own framework-styled versions,</span> <span class="token comment"># so require this extra only if you need the unstyled version</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/navs</span> <span class="token comment"># require 'pagy/extras/navs'</span> <span class="token comment"># Semantic extra: Add nav, nav_js and combo_nav_js helpers for Semantic UI pagination</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/semantic</span> <span class="token comment"># require 'pagy/extras/semantic'</span> <span class="token comment"># UIkit extra: Add nav helper and templates for UIkit pagination</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/uikit</span> <span class="token comment"># require 'pagy/extras/uikit'</span> <span class="token comment"># Multi size var used by the *_nav_js helpers</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/navs#steps</span> <span class="token comment"># Pagy::VARS[:steps] = { 0 => [2,3,3,2], 540 => [3,5,5,3], 720 => [5,7,7,5] } # example</span> <span class="token comment"># Feature Extras</span> <span class="token comment"># Headers extra: http response headers (and other helpers) useful for API pagination</span> <span class="token comment"># See http://ddnexus.github.io/pagy/extras/headers</span> <span class="token comment"># require 'pagy/extras/headers'</span> <span class="token comment"># Pagy::VARS[:headers] = { page: 'Current-Page', items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' } # default</span> <span class="token comment"># Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/support</span> <span class="token comment"># require 'pagy/extras/support'</span> <span class="token comment"># Items extra: Allow the client to request a custom number of items per page with an optional selector UI</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/items</span> <span class="token comment"># require 'pagy/extras/items'</span> <span class="token comment"># Pagy::VARS[:items_param] = :items # default</span> <span class="token comment"># Pagy::VARS[:max_items] = 100 # default</span> <span class="token comment"># Overflow extra: Allow for easy handling of overflowing pages</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/overflow</span> <span class="token comment"># require 'pagy/extras/overflow'</span> <span class="token comment"># Pagy::VARS[:overflow] = :empty_page # default (other options: :last_page and :exception)</span> <span class="token comment"># Metadata extra: Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc.</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/metadata</span> <span class="token comment"># you must require the shared internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels</span> <span class="token comment"># require 'pagy/extras/shared'</span> <span class="token comment"># require 'pagy/extras/metadata'</span> <span class="token comment"># For performance reason, you should explicitly set ONLY the metadata you use in the frontend</span> <span class="token comment"># Pagy::VARS[:metadata] = [:scaffold_url, :count, :page, :prev, :next, :last] # example</span> <span class="token comment"># Trim extra: Remove the page=1 param from links</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/trim</span> <span class="token comment"># require 'pagy/extras/trim'</span> <span class="token comment"># Pagy Variables</span> <span class="token comment"># See https://ddnexus.github.io/pagy/api/pagy#variables</span> <span class="token comment"># All the Pagy::VARS are set for all the Pagy instances but can be overridden</span> <span class="token comment"># per instance by just passing them to Pagy.new or the #pagy controller method</span> <span class="token comment"># Instance variables</span> <span class="token comment"># See https://ddnexus.github.io/pagy/api/pagy#instance-variables</span> <span class="token comment"># Pagy::VARS[:items] = 2 # default</span> <span class="token comment"># Other Variables</span> <span class="token comment"># See https://ddnexus.github.io/pagy/api/pagy#other-variables</span> <span class="token comment"># Pagy::VARS[:size] = [1,4,4,1] # default</span> <span class="token comment"># Pagy::VARS[:page_param] = :page # default</span> <span class="token comment"># Pagy::VARS[:params] = {} # default</span> <span class="token comment"># Pagy::VARS[:anchor] = '#anchor' # example</span> <span class="token comment"># Pagy::VARS[:link_extra] = 'data-remote="true"' # example</span> <span class="token comment"># Rails</span> <span class="token comment"># Rails: extras assets path required by the helpers that use javascript</span> <span class="token comment"># (pagy*_nav_js, pagy*_combo_nav_js, and pagy_items_selector_js)</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras#javascript</span> <span class="token comment"># Rails.application.config.assets.paths << Pagy.root.join('javascripts')</span> <span class="token comment"># I18n</span> <span class="token comment"># Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem</span> <span class="token comment"># See https://ddnexus.github.io/pagy/api/frontend#i18n</span> <span class="token comment"># Notice: No need to configure anything in this section if your app uses only "en"</span> <span class="token comment"># or if you use the i18n extra below</span> <span class="token comment">#</span> <span class="token comment"># Examples:</span> <span class="token comment"># load the "de" built-in locale:</span> <span class="token comment"># Pagy::I18n.load(locale: 'de')</span> <span class="token comment">#</span> <span class="token comment"># load the "de" locale defined in the custom file at :filepath:</span> <span class="token comment"># Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')</span> <span class="token comment">#</span> <span class="token comment"># load the "de", "en" and "es" built-in locales:</span> <span class="token comment"># (the first passed :locale will be used also as the default_locale)</span> <span class="token comment"># Pagy::I18n.load({locale: 'de'},</span> <span class="token comment"># {locale: 'en'},</span> <span class="token comment"># {locale: 'es'})</span> <span class="token comment">#</span> <span class="token comment"># load the "en" built-in locale, a custom "es" locale,</span> <span class="token comment"># and a totally custom locale complete with a custom :pluralize proc:</span> <span class="token comment"># (the first passed :locale will be used also as the default_locale)</span> <span class="token comment"># Pagy::I18n.load({locale: 'en'},</span> <span class="token comment"># {locale: 'es', filepath: 'path/to/pagy-es.yml'},</span> <span class="token comment"># {locale: 'xyz', # not built-in</span> <span class="token comment"># filepath: 'path/to/pagy-xyz.yml',</span> <span class="token comment"># pluralize: lambda{|count| ... } )</span> <span class="token comment"># I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory</span> <span class="token comment"># than the default pagy internal i18n (see above)</span> <span class="token comment"># See https://ddnexus.github.io/pagy/extras/i18n</span> <span class="token comment"># require 'pagy/extras/i18n'</span> <span class="token comment"># Default i18n key</span> <span class="token comment"># Pagy::VARS[:i18n_key] = 'pagy.item_name' # default</span> |
Chúng ta hãy sửa đổi PostContoder để bao gồm mô-đun pagy và sửa đổi hành động chỉ mục mặc định.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <span class="token keyword">class</span> <span class="token class-name">PostsController</span> <span class="token operator"><</span> <span class="token constant">ApplicationController</span> include <span class="token constant">Pagy</span> <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token constant">Backend</span> <span class="token comment"># <===============</span> before_action <span class="token symbol">:set_post</span> <span class="token punctuation">,</span> only <span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token symbol">:show</span> <span class="token punctuation">,</span> <span class="token symbol">:edit</span> <span class="token punctuation">,</span> <span class="token symbol">:update</span> <span class="token punctuation">,</span> <span class="token symbol">:destroy</span> <span class="token punctuation">]</span> <span class="token comment"># GET /posts</span> <span class="token comment"># GET /posts.json</span> <span class="token keyword">def</span> index <span class="token comment"># @posts = Post.all</span> <span class="token variable">@pagy</span> <span class="token punctuation">,</span> <span class="token variable">@posts</span> <span class="token operator">=</span> <span class="token function">pagy</span> <span class="token punctuation">(</span> <span class="token constant">Post</span> <span class="token punctuation">.</span> all <span class="token punctuation">)</span> <span class="token comment"># <===============</span> <span class="token keyword">end</span> <span class="token keyword">end</span> |
Bao gồm mô-đun lối vào bên trong application_helper.rb
1 2 3 4 | <span class="token keyword">module</span> <span class="token constant">ApplicationHelper</span> include <span class="token constant">Pagy</span> <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token constant">Frontend</span> <span class="token keyword">end</span> |
Và cuối cùng, hãy sửa đổi tệp xem để bao gồm các nút phân trang.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <span class="token operator"><</span> p id <span class="token operator">=</span> <span class="token string">"notice"</span> <span class="token operator">></span> <span class="token operator"><</span> <span class="token string">%= notice %></p> <h1>Posts</h1> <table> <thead> <tr> <th>Title</th> <th>Content</th> <th colspan=</span> <span class="token string">"3"</span> <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">/</span> th <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">/</span> tr <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">/</span> thead <span class="token operator">></span> <span class="token operator"><</span> tbody <span class="token operator">></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token variable">@posts</span> <span class="token punctuation">.</span> <span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span> post <span class="token operator">|</span> <span class="token string">%> <tr></span> <span class="token operator"><</span> td <span class="token operator">></span> <span class="token operator"><</span> <span class="token string">%= post.title %></td> <td><%=</span> post <span class="token punctuation">.</span> content <span class="token string">%></td></span> <span class="token operator"><</span> td <span class="token operator">></span> <span class="token operator"><</span> <span class="token string">%= link_to 'Show', post %></td> <td><%=</span> link_to <span class="token string">'Edit'</span> <span class="token punctuation">,</span> <span class="token function">edit_post_path</span> <span class="token punctuation">(</span> post <span class="token punctuation">)</span> <span class="token string">%></td></span> <span class="token operator"><</span> td <span class="token operator">></span> <span class="token operator"><</span> <span class="token string">%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <%=</span> <span class="token operator">=</span> <span class="token function">pagy_nav</span> <span class="token punctuation">(</span> <span class="token variable">@pagy</span> <span class="token punctuation">)</span> <span class="token string">%> # <=============================== <br></span> <span class="token operator"><</span> <span class="token operator">%</span> <span class="token operator">=</span> link_to <span class="token string">'New Post'</span> <span class="token punctuation">,</span> new_post_path <span class="token operator">%</span> <span class="token operator">></span> |
Và đó là nó. Đã được thực hiện.