Readers: 40 | Updated: 2008

自定义will_paginage输出(转自rails2.cn)

Translate Into:

作者: qichunren  链接:http://javautils.javaeye.com/blog/207164  发表时间: 2008年06月23日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

自定义will_paginage输出

ill_paginate是Rails中比较常用的分页插件,但是有时候我们可能想要自定义它的输出,这可以通过扩展WillPaginate::LinkRenderer类来实现,比如,下面的renderer将会去除Next和Previous链接:
 class CustomPaginationRenderer < WillPaginate::LinkRenderer  def to_html      links = @options[:page_links] ? windowed_links : []          html = links.join(@options[:separator])  @options[:container] ? @template.content_tag(:div, html, html_attributes) : html  end  end 


要在view中使用这个自定义的renderer,只需要加上:renderer参数即可:

 <%= will_paginate @items, ;:renderer => ‘CustomPaginationRenderer’ %>  


下面给出一个更复杂的自定义Renderer,它会在分页链接后显示一个文本框,以及一个‘Goto’按钮,允许用户直接跳转到某一页:

 class CustomPaginationRenderer < WillPaginate::LinkRenderer    @@id = 1  def to_html      links = @options[:page_links] ? windowed_links : []  # previous/next buttons      links.unshift page_link_or_span(@collection.previous_page, ‘disabled’, @options[:prev_label])      links.push    page_link_or_span(@collection.next_page,     ‘disabled’, @options[:next_label])      html = links.join(@options[:separator])      html += goto_box  @options[:container] ? @template.content_tag(:div, html, html_attributes) : html  end    private  def goto_box      @@id += 1      @@id = 1 if @@id > 100    <<-GOTO      <input type="text" maxlength="5" size="3" id="page#{@@id}" />      <input type="submit" onclick="goto_page#{@@id}()" value="Goto"/>      <script type="text/javascript">        function goto_page#{@@id}()        {          page = Number($(’page#{@@id}’).value)          total = #{total_pages}  if(page < 1    page > total)          {            alert(’Please enter a number between 1 and ‘ + total + ‘!’)            return;          }          var link = ‘#{@template.url_for(url_options("_page"))}’          var new_link = link.replace("_page", page)          window.location.assign(new_link)        }      </script>      GOTO  end  end


@@id的作用是因为一个view中有可能多次调用will_paginate,需要对inputbox进行区分,这个renderer还用到了一些继承自WillPaginate::LinkRenderer的方法:

url_for(page), 返回指向某页的链接,比如url_for(1) => ‘/posts?page=1′
total_pages, 返回总页数
page_link_or_span,返回指向某页面的链接
更多方法可以在WillPaginate的view_helper.rb中找到。
本文的讨论也很精彩,浏览讨论>>


JavaEye推荐



From The Blogs

Hack a Day

2008
Notacon 2008: Circuit bending will get you laid
Filed under: cons[Pete Edwards] and [Fred Owsley] openly admitted that the title was the most thinly veiled audience-bait ever constructed. Nevertheless, they poured through a great talk covering the ... 查看全文

Culture, Geography, Science, Tourism

2008
Hong Kong medical experts pointed out: a sudden increase in the amount of exercise will lead to easier sickness
Physical exercise can enhance immunity. However, according to Hong Kong's "Ta Kung Newspaper" May 13, Hong Kong Medical experts pointed out that in order to resist diseases, sudden increase of physica... 查看全文

Personal Growth, Career Planning

2008
大学里能让你发财的8门课程
学生通过学校的学习并没能掌握应对残酷现实世界的技能,这是否已经成为所有人的共识了呢?      当然,你可以通过上大学获得一个高学位和一份高薪的工作,接着工作到60岁,然后开始享受退休的生活,享受迟来... 查看全文

Coolbuzz

2008
Star Wars themed Lego pens will influence what you pen down
Age no bar, a Star Wars fan will find it tough to get rid of its addiction at any time in life. And if you belong to that tribe then you’d surely know about the Lego theme which incorporates the Sta... 查看全文

Zen Habits

2008
Finding Health and Balance as a Blogger (or, Life Will Kill You, Not Blogging)
I was going through my daily RSS health reading when I came across an article in the NY Times. By now, you should know what it’s about: blogging being “bad” for your health. Being the curious soul tha... 查看全文

One Big Health Nut

2008
Five tips that will help you resist food cravings
As if sticking to a healthy nutritious daily diet isn’t difficult enough, along come cravings to take it from hard to impossible.  All day you avoid the usual snack traps; vending machines, candy dish... 查看全文

OhGizmo!

2008
DIY Calculator Watch Will Make You King Of The Nerds
When his Casio CFX-400 scientific calculator watch broke after 10 years of day-to-day use, David Jones went looking for a replacement. Unfortunately similar watches on the market today just didn’t cut... 查看全文

Coolbuzz

2008
Okao Catch will catch you smile and measure it for you
Care to know the nuance between a smirk and a slight smile? Then you gotta get this cool new smile measurement technology developed by Japanese electronics and health care company Omron Corp. Called t... 查看全文

World,Fashion, Entertainment

2008
Reprint: Ten Types of Food and Drink That Will Definitely Make You Fat
Most of the items on the list are drinks which contain high volume of sugar. If you drink them for a long period of time, you will put on weight without noticing.  Five typical examples of food or dri... 查看全文

OhGizmo!

2008
7-Inch LCD Monitor Tries To Make Your Wii Portable
By Andrew LiszewskiThe Nintendo Wii just doesn’t come across as a highly portable gaming system to me. I think it’s mostly because the majority of the games require you to be some distance from the sc... 查看全文
More Articles