Paginator in Rails

paginator 绝对是任何web app必备之物,rails 1.1.6 内建的paginator很好用,很傻瓜,但是貌似它的处理方式也确实很傻,有很多感觉不爽的地方。有一个find plugin可以完成paginator的活,但是在我的系统上没跑起来,也没研究直接换Bruce的paginator gem,用起来还算顺手。View上面的表现需要自己完成,找到了一个方法,修改一下与paginator gem整合起来,跑的不错。

windowed_pagination_links这个helper让我改成了下面的样子:

  1. def windowed_pagination_links(pagingEnum, pager, options)
  2.     link_to_current_page = options[:link_to_current_page]
  3.     always_show_anchors = options[:always_show_anchors]
  4.     padding = options[:window_size]
  5.  
  6.     current_page = pagingEnum.number
  7.         html = ''
  8.  
  9.     #Calculate the window start and end pages
  10.     padding = padding < 0 ? 0 : padding
  11.         first = current_page-padding<1 ? 1 : current_page-padding
  12.       last    = current_page+padding>pager.number_of_pages ? pager.number_of_pages : current_page+padding   
  13.  
  14.     # Print start page if anchors are enabled
  15.     html << yield(1) if always_show_anchors and not first == 1
  16.  
  17.     # Print window pages
  18.     first.upto(last) do |page|
  19.       (current_page == page && !link_to_current_page) ? html << page : html << yield(page)
  20.     end
  21.  
  22.     # Print end page if anchors are enabled
  23.     html << yield(pager.number_of_pages) if always_show_anchors and not last == pager.number_of_pages
  24.     html
  25.   end

Tags: ,

One Response to “Paginator in Rails”

  1. hector Says:

    谢谢你热心地上传了很多有用的MAXTHON插件,但是 WEB DOCUMENT PACKAGER以前在 2.0版本以下很好使用的,我也一直在用,现在升级了版本以后(我用的傲游(Maxthon)2.0 版 本 RC3(2.0.2.1360),这个插件就无法运行了。

Leave a Reply