`
机器人
  • 浏览: 83425 次
  • 性别: Icon_minigender_2
  • 来自: Google
社区版块
存档分类
最新评论

自定义will_paginate分页插件的html显示

阅读更多
will_paginate分页插件有一个WillPaginate::LinkRenderer,这个用来定义分页的html输出,我们可以继承这个,从而达到自定义分页输出。

例如:我现在想去掉分页中显示的“上一页”和“下一页”,我们可以在lib中定义一个子类:
class CustomPaginationRenderer < WillPaginate::LinkRenderer
  def to_html
    links = @options[:page_links] ? windowed_links : []    
    html = links.join(@options[:separator])
    @options[:container] ? @template.content_tag(:div, html, html_attributes) : html
  end  
end


页面上<%= will_paginate @items, :renderer => 'CustomPaginationRenderer' %>


而我正想达到的一需求是在原来分页显示的基础上,加上一些分页信息,如每页显示多少条数据,一共有多少页等:
class PaginationDetailLinkRenderer < WillPaginate::LinkRenderer

  def to_html
    links = @options[:page_links] ? windowed_links : []
    links.unshift page_link_or_span(@collection.previous_page, 'disabled prev_page', @options[:previous_label])
    links.push    page_link_or_span(@collection.next_page,     'disabled next_page', @options[:next_label])
    html = links.join(@options[:separator])
    html = "每页显示<b>#{@collection.per_page}</b>条数据,共有<b>#{total_pages}</b>页,  共有<b>#{@collection.total_entries}</b>条数据" + html
    @options[:container] ? @template.content_tag(:div, html, html_attributes) : html
  end

end

效果:


参考资料:
http://thewebfellas.com/blog/2008/8/3/roll-your-own-pagination-links-with-will_paginate
http://zilkey.com/2008/3/16/rendering-will_paginate-links-without-previous-and-next-buttons
分享到:
评论
4 楼 Hooopo 2009-10-30  
>> @users = User.paginate :per_page => 2, :page => 3
>> @users.per_page
=> 2
>> @users.current_page
=> 3
>> @users.total_pages
=> 23
>> @users.next_page
=> 4
>>


只有数据是好看的...我只看到你写了一坨东东...
3 楼 机器人 2009-10-30  
你难道不觉得我的方法好看一些吗? 
2 楼 Hooopo 2009-10-29  
引用
而我正想达到的一需求是在原来分页显示的基础上,加上一些分页信息,如每页显示多少条数据,一共有多少页等:


没有那么麻烦。。。

你用pagiate查出来的model实例里面都有你要的这些方法,看看我写的把分页信息附加到json的方法:
page_info = {}

    [:total_pages, :current_page, :next_page, :previous_page].each do |info|
      page_info[info] = @places.send(info)
    end

    @places << page_info

    render :json => @places

1 楼 机器人 2009-10-29  

相关推荐

Global site tag (gtag.js) - Google Analytics