will_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中找到。
相关推荐
will_paginate-bootstrap, 将 Twitter Bootstrap 分页组件与will_paginate集成 will_paginate-bootstrap不再维护我不再使用 Bootstrap 来使用 Rails,所以不幸的是,我不再接受请求请求或者维护这个库。 为了发布你...
rails 分页插件will_paginate 2.2.2
will_paginate分页插件 博文链接:https://foxgst.iteye.com/blog/168489
this code will be part for Ab intio calculation
rails 分页插件will_paginate 2.1.0
Unit_1_Will_people_have_robots课件.ppt
rails 分页插件will_paginate
jooy311/itwill_java_oracle_javascript
DMIEDIT_utility_DMIEDIT_utility_DMIUTILITY_DMIEDIT_Will_源码.zip
「安全防护」Will_your_application_be_secure_enough_when_Robots_produce_code_for_you_ - 业务安全 金融安全 NGFW 安全运维 安全管理 安全知识
人教新版小学英语四年级下册课件Unit5_what_will_you_do_this_weekend_Lesson27.ppt
will_paginate 带无限滚动示例应用程序 使用gem 'will_paginate', '~> 3.0.7'实现无限/无限滚动的工作 Rails CRUD 应用程序示例。 gem 'faker'被用来为项目gem 'faker'虚假数据。 演示 现场演示: : 在本地开发中...
NULL 博文链接:https://liuyi-act-buaa-edu-cn.iteye.com/blog/506503
in this documrnt i will show you something
Email Generator that will send data via outlook COM
This project will test bandwidth in webrtc.
Here you will find everything to build the supercnc.dxf stl files sample files are included in this project.
will make program for game