子在川上曰

  逝者如斯夫不舍昼夜
随笔 - 71, 文章 - 0, 评论 - 915, 引用 - 0
数据加载中……

让will_paginate的分页支持ajax

关于will_paginate插件,请看这个帖子: http://mmm.javaeye.com/blog/116931。
文/陈刚 (www.chengang.com.cn)
但一直搜不到它支持ajax分面的方法 ,于是我参考它分页方法的源代码(位于:vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb),稍微改写,变成了一个支持ajax的分页方法。以下代码复制到application_helper里即可。


  
#-----------------------------------------
  # will_paginate插件的ajax分页
  #-----------------------------------------
  @@pagination_options = { :class => 'pagination',
        
:prev_label   => '上一页',
        
:next_label   => '下一页',
        
: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,
        
#add by chengang
        :update =>nil, #ajax所要更新的html元素的id
        :url_suffix => ''  #url的后缀,主要是为了补全REST所需要的url
        #add end
        }
  mattr_reader 
:pagination_options

  def will_paginate_remote(entries 
= @entries, options = {})
    total_pages 
= entries.page_count

    
if total_pages > 1
      options 
= options.symbolize_keys.reverse_merge(pagination_options)
      page
, param = entries.current_page, options.delete(:param_name)
      
      inner_window
, outer_window = options.delete(:inner_window).to_i, options.delete(:outer_window).to_i
      
#add by chengang
      update =  options.delete(:update)
      suffix 
=  options.delete(:url_suffix)
      url 
= request.env['PATH_INFO'
      url 
+= suffix if suffix
      
#add end

      
min = page - inner_window
      
max = page + inner_window
      
if max > total_pages then min -= max - total_pages
      elsif 
min < 1  then max += 1 - min
      
end
      
      
current   = min..max
      beginning 
= 1..(1 + outer_window)
      tail      
= (total_pages - outer_window)..total_pages
      visible   
= [beginning, current, tail].map(&:to_a).flatten.sort.uniq
      links
, prev = [], 0

      visible
.each do |n|
        
next if n < 1
        
break if n > total_pages

        unless n 
- prev > 1
          
prev = n
          
#change by chengang
          text = (n==page ? n : "[#{n}]")
          links 
<< page_link_remote_or_span((n != page ? n : nil), 'current', text, param, update, url)
        
else
          
prev = n - 1
          links 
<< ''
          redo
        
end
      
end
      
      
#change by chengang
      links.unshift page_link_remote_or_span(entries.previous_page, 'disabled', options.delete(:prev_label), param, update, url)
      links
.push    page_link_remote_or_span(entries.next_page,     'disabled', options.delete(:next_label), param, update, url)
      
#change end

      content_tag 
:div, links.join(options.delete(:separator)), options
    
end
  
end
  
protected

  def page_link_remote_or_span(page
, span_class, text, param, update, url)
    unless page
      content_tag 
:span, text, :class => span_class
    
else
      link_to_remote text
, :update => update, :url => "#{url}?#{param.to_sym}=#{page}", :method=>:get
    
end
  
end


在view中的使用如下所示:
          <%=will_paginate_remote @topics, :update => 'topicList', :url_suffix => url_suffix%>


posted on 2007-09-02 15:42 陈刚 阅读(4540) 评论(14)  编辑  收藏 所属分类: Rails&Ruby

评论

# re: 让will_paginate的分页支持ajax  回复  更多评论   

怎么没反应。。。
2007-09-05 22:12 | 陈老师好

# re: 让will_paginate的分页支持ajax  回复  更多评论   

不行啊,你试过能行?
2007-09-06 11:43 | 你自己试过?

# re: 让will_paginate的分页支持ajax  回复  更多评论   

url_suffix有没有值,Ruby局域变量必须给一个初值,如果没值让他为nil
2007-09-07 17:09 | 陈刚

# re: 让will_paginate的分页支持ajax  回复  更多评论   

我就是让他为nil
算了,已解决
2007-09-22 15:18 | 陈老师好

# re: 让will_paginate的分页支持ajax  回复  更多评论   

不行,出错了,咋回事呢?
undefined method `to_i' for {:page=>nil}:Hash
RAILS_ROOT: ./script/../config/..

2007-10-17 10:08 | skatefish

# re: 让will_paginate的分页支持ajax  回复  更多评论   

您好,调试了一下还是蛮好用的了,想问一下能加上:loading=>Element.show这样的参数么
2007-10-17 13:36 | skatefish

# re: 让will_paginate的分页支持ajax  回复  更多评论   

参照了您的修改,现在加上了loading和complete的效果,还是不错的,谢谢:)
2007-10-17 13:48 | skatefish

# re: 让will_paginate的分页支持ajax  回复  更多评论   

不错,很好用,谢谢!
2008-05-23 14:17 | 轩辕武

# re: 让will_paginate的分页支持ajax  回复  更多评论   

正想找一个支持AJAX的分页呢,多谢分享!
2008-11-14 22:36 | yanghuan

# re: 让will_paginate的分页支持ajax  回复  更多评论   

请问一下:mattr_reader 这个是怎么用的??
2008-11-14 22:48 | yanghuan

# re: 让will_paginate的分页支持ajax  回复  更多评论   

假如使用RJS的话,普通的分页就可以实现无刷新页面的效果了。
2008-11-15 01:00 | yanghuan

# re: 让will_paginate的分页支持ajax[未登录]  回复  更多评论   

找不到page_count這個方法 這是wii_paginate內建的方法嗎?
2008-11-15 15:58 | YY

# re: 让will_paginate的分页支持ajax  回复  更多评论   

楼上的,你把
total_pages = entries.page_count

换成

total_pages = entries.total_pages
就没问题了,这是因为WillPaginate::Collection中没有page_count方法。
2008-11-16 01:15 | yanghuan

# re: 让will_paginate的分页支持ajax  回复  更多评论   

过时了吧........
2009-08-21 15:03 | fl1429

只有注册用户登录后才能发表评论。


网站导航: