六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 47|回复: 0

will_paginate ajax

[复制链接]

升级  31.33%

25

主题

25

主题

25

主题

秀才

Rank: 2

积分
97
 楼主| 发表于 2013-1-29 11:38:51 | 显示全部楼层 |阅读模式
两种方法
一,
@@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  # => will_paginate Ajax效果扩展Helper方法  def will_paginate_remote(entries = @entries, options = {})    total_pages = entries.total_pages    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
<%= will_paginate_remote @task_logs, :update => 'task_logs_list', :url_suffix => nil %>
二,
<%= will_paginate @task_logs, {      :renderer => 'WillPaginate::LinkRenderer',      :update => 'task_logs_list',      :url => task_url(@task),      :create => '',      :complete => ''}%>
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  endend
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表