Rails 2.0.2以后,原来的分页类paginate没作为基准类了,而是依赖plug in——will paginate。

will_paginate的代码写得不错,不过没支持Ajax,动手改造了一下,哈,效果还行(其实以前paginate的时候已经自己封装了一个方法用于处理ajax分页)。

改动很小,大家不用惊呼,主要改动如下:

/plugins/will_paginate/lib/will_paginate/view_helpers.rb

原代码82行左右,加入传入参数:ajax_options = nil

def will_paginate(collection = nil, options = {}, ajax_options = nil)

原代码95行左右,创建新renderer_class实例,加入以下调用参数

renderer = renderer_class.new collection, options, ajax_options, self

原代码118行左右,renderer_class类构建,加入调用参数

def initialize(collection, options, ajax_options, template)

原代码122行左右,增减一个实例变量,用于存储传入的ajax_options:

@ajax_options = ajax_options

原代码187行左右,是输出页面翻页链接,增加对@ajax_options的判断:

if @ajax_options
  @ajax_options[:url] = url_options(page) if @ajax_options[:url].nil?
  @template.link_to_remote text, @ajax_options
else
  @template.link_to text, url_options(page)
end

源代码203行左右(因为上面加了几行,所以这里不准确,这里贴整段):

就是def url_options(page)这里:
def url_options(page)
  options = { param_name => page }
  # page links should preserve GET parameters
  options = params.merge(options) if @template.request.get?
  options.rec_merge!(@options[:params]) if @options[:params]
  options[:ajax] = true if !options.has_key?(:ajax) and !@ajax_options.nil?
  return options
end

嗯,如何调用呢?

<%= will_paginate @favorites, { :param_name => :fav_page }, { :update => "favorites_list", :method => "get" } %>

什么,这不就和link_remote_to差不多嘛,嗯,差不多就拿去用吧!HuHu~~

PS一下:这个方法还没彻底测试过,需要大家测试测试。



posted on 2008-03-05 02:02  Janpoem  阅读(536)  评论(2编辑  收藏  举报