简体   繁体   English

使用 Pagy gem 的 Rails 7 下拉页面大小项目

[英]Rails 7 dropdown page size items with Pagy gem

In my Rails 7 app I'm using Pagy gem for pagination.在我的 Rails 7 应用程序中,我使用 Pagy gem 进行分页。 The pagination works well but now I want to give the user the ability to choose how many items they will see in the paginated list.分页效果很好,但现在我想让用户能够选择他们将在分页列表中看到多少项目。 According to the docs the implementation should be fairly easy:根据文档,实现应该相当简单:

# initializers/pagy.rb

require 'pagy/extras/items'

# Instance variables
Pagy::DEFAULT[:items]  = 10
Pagy::DEFAULT[:items_param] = :items

with sample controller:样本 controller:

class PaymentsController < ApplicationController
  def index
    @pagy, @payments = pagy_array(fetch_payments)
  end

  private

  def client
    @client ||= Test::Client.new
  end

  def fetch_payments
    client.payments.list(
      page: 1,
      per_page: 100,
      platform_id: current_user.platform_id,
    )
  end
end

But now I don't know how to create working dropdown (like this one) with paginated results inside the view.但是现在我不知道如何在视图中创建带有分页结果的工作下拉列表(像这个)。

in /config/initializers/pagy.rb在 /config/initializers/pagy.rb

require 'pagy/extras/bootstrap'
PAGE_SIZES = [10, 15, 20, 25]

in your /app/controllers/your_controller.rb在你的 /app/controllers/your_controller.rb

def index
    @pagy, @rooms = pagy(Room.all, items: params[:per_page] || "set_default")
end

in your app/view在你的应用程序/视图中

<% PAGE_SIZES.each do |size| %>
   <%= link_to size, url_for(per_page: size), class:"dropdown-item"%>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM