#-----------------------------------------#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_countiftotal_pages>1options=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 chengangupdate=options.delete(:update)
suffix=options.delete(:url_suffix)
url=request.env['PATH_INFO']
url+=suffixifsuffix#add endmin=page-inner_windowmax=page+inner_windowifmax>total_pages thenmin-=max-total_pages
elsifmin<1thenmax+=1-minendcurrent=min..maxbeginning=1..(1+outer_window)
tail=(total_pages-outer_window)..total_pages
visible=[beginning,current,tail].map(&:to_a).flatten.sort.uniq
links,prev=[],0visible.eachdo|n|nextifn<1breakifn>total_pages
unless n-prev>1prev=n#change by chengangtext=(n==page?n:"[#{n}]")
links<'redoendend#change by chenganglinks.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 endcontent_tag:div,links.join(options.delete(:separator)),optionsendendprotecteddef page_link_remote_or_span(page,span_class,text,param,update,url)
unless page
content_tag:span,text,:class=>span_classelselink_to_remote text,:update=>update,:url=>"#{url}?#{param.to_sym}=#{page}",:method=>:getendend