Chinaunix首页 | 论坛 | 博客
  • 博客访问: 472432
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:28:23

自定义will_paginage输出 ill_paginate是Rails中比较常用的分页插件,但是有时候我们可能想要自定义它的输出,这可以通过扩展WillPaginate::LinkRenderer类来实现,比如,下面的renderer将会去除Next和Previous链接: Ruby代码
    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
    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

    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 要在view中使用这个自定义的renderer,只需要加上:renderer参数即可: Html代码
    <%= will_paginate @items, ;:renderer => ‘CustomPaginationRenderer’ %>
    view plaincopy to clipboardprint?
    <%= will_paginate @items, ;:renderer => ‘CustomPaginationRenderer’ %>

    <%= will_paginate @items, ;:renderer => ‘CustomPaginationRenderer’ %>
    下面给出一个更复杂的自定义Renderer,它会在分页链接后显示一个文本框,以及一个‘Goto’按钮,允许用户直接跳转到某一页: Ruby代码
    class CustomPaginationRenderer < WillPaginate::LinkRenderer
      @@id = 1
    def to_html
        links = @options[:page_links] ? windowed_links : []
    # previous/next buttons
        links.unshift page_link_or_span(@collection.previous_page, ‘disabled’, @options[:prev_label])
        links.push    page_link_or_span(@collection.next_page,     ‘disabled’, @options[:next_label])
        html = links.join(@options[:separator])
        html += goto_box
    @options[:container] ? @template.content_tag(:div, html, html_attributes) : html
    end
      private
    def goto_box
        @@id += 1
        @@id = 1 if @@id > 100
      <<-GOTO
       
       
       
        GOTO
    end
    end
 

[1]  

【责编:landy】

--------------------next---------------------

阅读(116) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~