自定义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

你可能感兴趣的:(html,json,Blog,Ruby)