【问题标题】:Rails smart_listing configurationRails smart_listing 配置
【发布时间】:2014-08-13 22:18:06
【问题描述】:

我正在尝试在我的应用程序中设置 smart_listing gem。在哪里以及如何配置分页结果的默认每页数。在 smart_listing 的文档中提到它使用 kaminari。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 kaminari smart-listing


    【解决方案1】:

    如果您还没有 config/initializers/kaminari_config.rb 文件,请运行 Kaminari 的配置生成器:

    bundle exec rails generate kaminari:config
    

    这将使用默认内容创建config/initializers/kaminari_config.rb

    Kaminari.configure do |config|
      # config.default_per_page = 25
      # config.max_per_page = nil
      # config.window = 4
      # config.outer_window = 0
      # config.left = 0
      # config.right = 0
      # config.page_method_name = :page
      # config.param_name = :page
    end
    

    只需取消注释并编辑您有兴趣更改的配置选项。

    更新:

    SmartListing 在config/initializers/smart_listing.rb 中提供了自己的分页配置选项:

    SmartListing.configure do |config|
      config.global_options({
        #:param_names  => {                                              # param names
          #:page                         => :page,
          #:per_page                     => :per_page,
          #:sort                         => :sort,
        #},
        #:array                          => false,                       # controls whether smart list should be using arrays or AR collections
        #:max_count                      => nil,                         # limit number of rows
        #:unlimited_per_page             => false,                       # allow infinite page size
        #:paginate                       => true,                        # allow pagination
        #:memorize_per_page              => false,                       # save per page settings in the cookie
        #:page_sizes                     => DEFAULT_PAGE_SIZES,          # set available page sizes array
        #:kaminari_options               => {:theme => "smart_listing"}, # Kaminari's paginate helper options
      })
    
      config.constants :classes, {
        #:main                  => "smart-listing",
        #:editable              => "editable",
        #:content               => "content",
        #:loading               => "loading",
        #:status                => "smart-listing-status",
        #:item_actions          => "actions",
        #:new_item_placeholder  => "new-item-placeholder",
        #:new_item_action       => "new-item-action",
        #:new_item_button       => "btn",
        #:hidden                => "hidden",
        #:autoselect            => "autoselect",
        #:callback              => "callback",
        #:pagination_per_page   => "pagination-per-page text-center",
        #:pagination_count      => "count",
        #:inline_editing        => "info",
        #:no_records            => "no-records",
        #:limit                 => "smart-listing-limit",
        #:limit_alert           => "smart-listing-limit-alert",
        #:controls              => "smart-listing-controls",
        #:controls_reset        => "reset",
        #:filtering             => "filter",
        #:filtering_search      => "glyphicon-search",
        #:filtering_cancel      => "glyphicon-remove",
        #:filtering_disabled    => "disabled",
        #:sortable              => "sortable",
        #:icon_new              => "glyphicon glyphicon-plus",
        #:icon_edit             => "glyphicon glyphicon-pencil",
        #:icon_trash            => "glyphicon glyphicon-trash",
        #:icon_inactive         => "glyphicon glyphicon-circle",
        #:icon_show             => "glyphicon glyphicon-share-alt",
        #:icon_sort_none        => "glyphicon glyphicon-resize-vertical",
        #:icon_sort_up          => "glyphicon glyphicon-chevron-up",
        #:icon_sort_down        => "glyphicon glyphicon-chevron-down",
      }
    
      config.constants :data_attributes, {
        #:main                  => "smart-listing",
        #:confirmation          => "confirmation",
        #:id                    => "id",
        #:href                  => "href",
        #:callback_href         => "callback-href",
        #:max_count             => "max-count",
        #:inline_edit_backup    => "smart-listing-edit-backup",
        #:params                => "params",
        #:observed              => "observed",
        #:href                  => "href",
        #:autoshow              => "autoshow",
        #:popover               => "slpopover",
      }
    
      config.constants :selectors, {
        #:item_action_destroy   => "a.destroy",
        #:edit_cancel           => "button.cancel",
        #:row                   => "tr",
        #:head                  => "thead",
        #:filtering_icon        => "i"
      }
    end
    

    取消注释 page_sizes 行并将 DEFAULT_PAGE_SIZES 替换为类似 [10, 20, 50, 100] 的数组

    【讨论】:

    • 我已经试过了。我不工作。该视图仅显示前三行和分页。
    • 对我有用(重新启动服务器后..在更改配置文件中的某些内容后我偶尔会忘记做某事)!感谢您的帮助。
    • 这个问题有点老了,但是更改配置文件对我也不起作用,当我调用 smart_listing create 时,我仍然必须覆盖我想要更改的每个参数。对于页面大小: smart_listing_create( ... page_sizes: [42, 256, 1024], ...) 这可行,但默认值肯定会更好,我无法解释为什么不加载配置文件。
    • 此处相同,默认文件未加载
    【解决方案2】:

    无需在initializers/smart_listing.rb 中注释掉page_sizes。 您可以在控制器中定义per_page 值,例如:

    users_scope = User.all.includes(:bio)
    users_scope = users_scope.like(params[:filter]) if params[:filter]
    @users = smart_listing_create :users, users_scope, partial: "users/list", page_sizes: [5, 7, 13, 26]
    

    smart_listing v1.1.2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多