获取 will_paginate 在第一页上定义自定义偏移量

发布于 2024-12-06 05:20:15 字数 355 浏览 0 评论 0原文

我正在我的 rails 网站 上构建一个新闻部分,它使用 will_paginate 进行分页。现在我想知道如何使用 will_paginate 为第一页指定自定义偏移。像这样的事情:

@featured_news = Post.first
@news = Post.offset(1).paginate(:page => params[:page])

我需要最新新闻条目是特殊的并且不包含在@news对象中。

我怎样才能做到这一点?

感谢您的宝贵时间!

i'm building a news section on my rails website and it uses will_paginate for pagination. now i was wondering how i can specify a custom offset for the first page with will_paginate. something like this:

@featured_news = Post.first
@news = Post.offset(1).paginate(:page => params[:page])

i need the latest news-entry to be special and not be included in the @news objects.

how i can achieve this?

thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独夜无伴 2024-12-13 05:20:15

will_paginate 重新定义每个 offsetlimit 查询条件,以获取特定页面的行。我可以为你看到两个选择:

丑陋的一个:利用 will_paginate 在集合上工作的事实,并使用这种语法(它会加载你的所有表格)

@news = Post.offset(1).all.paginate(:page => params[:page])

较长的一个:分叉 will_paginate gem,以便它可以处理自定义偏移量。我还没有尝试过,但是类似的东西应该可以工作(对 gem 的更改突出显示)

# will_paginate / lib / will_paginate / active_record.rb

module Pagination
  def paginate(options)
    options  = options.dup
    pagenum  = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
    per_page = options.delete(:per_page) || self.per_page
    total    = options.delete(:total_entries)

    #######################################
    custom_offset = options.delete(:offset)
    ####################################### 

    count_options = options.delete(:count)
    options.delete(:page)

    #######################################################
    # rel = limit(per_page.to_i).page(pagenum)
    rel = limit(per_page.to_i).page(pagenum, custom_offset)
    #######################################################

    rel = rel.apply_finder_options(options) if options.any?
    rel.wp_count_options = count_options    if count_options
    rel.total_entries = total.to_i          unless total.blank?
    rel
  end

  ################################
  # def page(num)
  def page(num, custom_offset = 0)
  ################################
    rel = scoped.extending(RelationMethods)
    pagenum = ::WillPaginate::PageNumber(num.nil? ? 1 : num)
    per_page = rel.limit_value || self.per_page
    ##################################################################
    # rel = rel.offset(pagenum.to_offset(per_page).to_i)
    rel = rel.offset(pagenum.to_offset(per_page).to_i + custom_offset)
    ##################################################################
    rel = rel.limit(per_page) unless rel.limit_value
    rel.current_page = pagenum
    rel
  end
end

这应该允许您使用以下语法:

@news = Post.paginate(:page => params[:page], :offset => 1)

will_paginate redefines every offset and limit query conditions, to get the rows of a specific page. I can see two options for you :

The ugly one : take advantage of the fact that will_paginate works on collections, and use this syntax (it will load all your table though)

@news = Post.offset(1).all.paginate(:page => params[:page])

The longer one : fork the will_paginate gem so that it can handle custom offsets. I haven't tried it, but something like this should work (the changes to the gem are highlighted)

# will_paginate / lib / will_paginate / active_record.rb

module Pagination
  def paginate(options)
    options  = options.dup
    pagenum  = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
    per_page = options.delete(:per_page) || self.per_page
    total    = options.delete(:total_entries)

    #######################################
    custom_offset = options.delete(:offset)
    ####################################### 

    count_options = options.delete(:count)
    options.delete(:page)

    #######################################################
    # rel = limit(per_page.to_i).page(pagenum)
    rel = limit(per_page.to_i).page(pagenum, custom_offset)
    #######################################################

    rel = rel.apply_finder_options(options) if options.any?
    rel.wp_count_options = count_options    if count_options
    rel.total_entries = total.to_i          unless total.blank?
    rel
  end

  ################################
  # def page(num)
  def page(num, custom_offset = 0)
  ################################
    rel = scoped.extending(RelationMethods)
    pagenum = ::WillPaginate::PageNumber(num.nil? ? 1 : num)
    per_page = rel.limit_value || self.per_page
    ##################################################################
    # rel = rel.offset(pagenum.to_offset(per_page).to_i)
    rel = rel.offset(pagenum.to_offset(per_page).to_i + custom_offset)
    ##################################################################
    rel = rel.limit(per_page) unless rel.limit_value
    rel.current_page = pagenum
    rel
  end
end

This should allow you to use this syntax:

@news = Post.paginate(:page => params[:page], :offset => 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文