如果一页有两处分页......

如果在同一个页面中,有两处分页,用这样的代码:

<%= will_paginate @applies %>
<%= will_paginate @friends %>

 

这样两个分页会产生“关联”。

实现一

<%= will_paginate @applies %>
<%= will_paginate @friends, :param_name => 'fpage' %>

 

在controller中,也要相应地写成:

 

  @applies = Apply.paginate(:page => params[:page], :per_page => 10)
  @friends = Friend.paginate(:page => params[:fpage], :per_page => 10)

 

实现二

在application_helper.rb中自定义一个方法:

 

 

  def will_paginate_remote(entries = @entries, p = nil, 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
      update = options.delete(:update)
      suffix = options.delete(:url_suffix)
      url = request.env['PATH_INFO']
      url += suffix if suffix
      url = url + "?" + p
      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
          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
      links.unshift page_link_remote_or_span(entries.previous_page, 'disabled prev_page', options.delete(:prev_label), param, update, url)
      links.push page_link_remote_or_span(entries.next_page, 'next_page', options.delete(:next_label), param, update, url)
      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, :html => {:class => span_class}
    end
  end

 

index.rhtml代码如下:

 

<div id="applies">
  <%= render :partial => "apply" %>
</div>
<div id="friends">
  <%= render :partial => "friend" %>
</div>

 

_apply.rhtml代码如下:

 

<% (@applies || []).each do |apply| %>

<% end %>
<%= will_paginate_remote @applies , "kind=apply", {:update => 'applies'} %>

 

_friend.rhtml代码如下:

 

<% (@friends || []).each do |friend| %>

<% end %>
<%= will_paginate_remote @friends , "kind=friend", {:update => 'friends'} %>

 

相应的控制器代码如下:

 

    @applies = Apply.paginate(:page => params[:page], :per_page => 10)
    @friends = Friend.paginate(:page => params[:page], :per_page => 10)
    if params[:kind] == "apply"
      render :partial => "apply"
    elsif params[:kind] == "friend"
      render :partial => "friend"
    end

 

你可能感兴趣的:(分页)