---
title: "让will_paginate的分页支持ajax "
author: "夜鸣猪"
date: "2008-05-10T13:37:19Z"
tags: []
slug: "will-paginate-ajax"
draft: false
lastmod: "2008-05-10T13:37:19Z"
id: 191723
url: "https://hlee.iteye.com/blog/191723"
---

## [让will\_paginate的分页支持ajax](http://www.blogjava.net/chengang/archive/2007/09/02/142077.html)

关于will\_paginate插件，请看这个帖子: http://mmm.iteye.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 =&gt; 'pagination',  
        :prev\_label   =&gt; '上一页',  
        :next\_label   =&gt; '下一页',  
        :inner\_window =&gt; 4, # links around the current page  
        :outer\_window =&gt; 1, # links around beginning and end  
        :separator    =&gt; ' ', # single space is friendly to spiders and non-graphic browsers  
        :param\_name   =&gt; :page,  
        #add by chengang  
        :update =&gt;nil, #ajax所要更新的html元素的id  
        :url\_suffix =&gt; ''  #url的后缀，主要是为了补全REST所需要的url  
        #add end  
        }  
  mattr\_reader :pagination\_options

  def will\_paginate\_remote(entries = @entries, options = {})  
    total\_pages = entries.page\_count

    if total\_pages &gt; 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 &gt; total\_pages then min -= max - total\_pages  
      elsif min &lt; 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 &lt; 1  
        break if n &gt; total\_pages

        unless n - prev &gt; 1  
          prev = n  
          #change by chengang  
          text = (n==page ? n : "\[#{n}]")  
          links &lt;&lt; page\_link\_remote\_or\_span((n != page ? n : nil), 'current', text, param, update, url)  
        else  
          prev = n - 1  
          links &lt;&lt; '...'  
          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 =&gt; span\_class  
    else  
      link\_to\_remote text, :update =&gt; update, :url =&gt; "#{url}?#{param.to\_sym}=#{page}", :method=&gt;:get  
    end  
  end

在view中的使用如下所示:

          &lt;%=will\_paginate\_remote @topics, :update =&gt; 'topicList', :url\_suffix =&gt; url\_suffix%&gt;