论坛首页 编程语言技术论坛

修改will_paginate支持ajax方式

浏览 3410 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-05-01  
网上关于will_paginate支持ajax的文章不少,可是看了一下,都比较旧了,在最新更新的will_paginate插件下用不了,所以就自己改了一下
在ViewHelper.rb中
 @@pagination_options = {
      :class        => 'pagination',
      :prev_label   => '« Previous',
      :next_label   => 'Next »',
      :inner_window => 4, # links around the current page
      :outer_window => 1, # links around beginning and end
      :separator    => ' ', # single space is friendly to spiders and non-graphic browsers
      :param_name   => :page,
      :params       => nil,
      :renderer     => 'WillPaginate::LinkRenderer',
      :page_links   => true,
      :container    => true
    }

增加update 修改为:
 @@pagination_options = {
      :class        => 'pagination',
      :prev_label   => '« Previous',
      :next_label   => 'Next »',
      :inner_window => 4, # links around the current page
      :outer_window => 1, # links around beginning and end
      :separator    => ' ', # single space is friendly to spiders and non-graphic browsers
      :param_name   => :page,
      :params       => nil,
      :renderer     => 'WillPaginate::LinkRenderer',
      :page_links   => true,
      :container    => true,
      :update       => nil
    }

page_link_or_span方法修改为:
def page_link_or_span(page, span_class = 'current', text = nil)
      text ||= page.to_s
      if page and page != current_page
        if(@options[:update] != nil)
          @template.link_to_remote text, :url=>url_options(page),:update=>@options[:update]
        else
          @template.link_to text, url_options(page)
        end
      else
        @template.content_tag :span, text, :class => span_class
      end
    end

在页面上:普通链接方式:
<%=will_paginate @users, {:class=>'pagination', :prev_label   => '上一页',:next_label   => '下一页'}%>  


ajax方式:
<%=will_paginate @users, {:class=>'pagination', :prev_label   => '上一页',:next_label   => '下一页',:update => 'content'}%>  


   发表时间:2008-05-01  
will_paginate接受一个renderer参数,默认是WillPaginate::LinkRenderer,更好的做法是subclass一个AjaxLinkRenderer,这样不需要修改它的源代码,在做plugin升级的时候会很方便。
6 请登录后投票
   发表时间:2008-05-01  
Quake Wang 写道
will_paginate接受一个renderer参数,默认是WillPaginate::LinkRenderer,更好的做法是subclass一个AjaxLinkRenderer,这样不需要修改它的源代码,在做plugin升级的时候会很方便。

是这样的,之前没怎么仔细读源码,惭愧
0 请登录后投票
   发表时间:2008-05-01  
AjaxLinkRenderer类:
class AjaxLinkRenderer < WillPaginate::LinkRenderer
  def page_link_or_span(page, span_class = 'current', text = nil)
    text ||= page.to_s
    if page and page != current_page
        @template.link_to_remote text, :url=>url_options(page),:update=>@options[:update]
    else
      @template.content_tag :span, text, :class => span_class
    end
  end
end

页面:
<%=will_paginate @users, {:class=>'pagination', 
  :prev_label   => '上一页',
  :next_label   => '下一页',
  :renderer => 'AjaxLinkRenderer',
  :update => 'content'}%>  
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics