自定义 will_paginate 渲染器
custom will_paginate renderer
Documentation 缺少 will_paginate 自定义渲染器:
There is no documentation how to write your own link renderer, but the source code is pretty self-explanatory. Dive into it, and selectively override methods of the LinkRenderer to adjust them to your needs.
是否有任何非官方文档?
找到了一个不错的博客 post 关于 custom will_paginate renderer
module ApplicationHelper
# change the default link renderer for will_paginate
def will_paginate(collection_or_options = nil, options = {})
if collection_or_options.is_a? Hash
options, collection_or_options = collection_or_options, nil
end
unless options[:renderer]
options = options.merge :renderer => MyCustomLinkRenderer
end
super *[collection_or_options, options].compact
end
end
然后在初始化程序中
class MyCustomLinkRenderer < WillPaginate::ActionView::LinkRenderer do
def container_attributes
{class: "tc cf mv2"}
end
def page_number(page)
if page == current_page
tag(:span, page, class: 'b bg-dark-blue near-white ba b--near-black pa2')
else
link(page, page, class: 'link ba b--near-black near-black pa2', rel: rel_value(page))
end
end
def gap
text = @template.will_paginate_translate(:page_gap) { '…' }
%(<span class="mr2">#{text}</span>)
end
def previous_page
num = @collection.current_page > 1 && @collection.current_page - 1
previous_or_next_page(num, @options[:previous_label], 'link ba near-black b--near-black pa2')
end
def next_page
num = @collection.current_page < total_pages && @collection.current_page + 1
previous_or_next_page(num, @options[:next_label], 'link ba near-black b--near-black pa2')
end
def previous_or_next_page(page, text, classname)
if page
link(text, page, :class => classname)
else
tag(:span, text, :class => classname + ' bg-dark-blue near-white')
end
end
end
感谢之前的回答,我写了这段代码来使用 will_paginate 和 materialize
application_controller.rb
def custom_paginate_renderer
# Return nice pagination for materialize
Class.new(WillPaginate::ActionView::LinkRenderer) do
def container_attributes
{class: "pagination"}
end
def page_number(page)
if page == current_page
"<li class=\"cyan active\">"+link(page, page, rel: rel_value(page))+"</li>"
else
"<li class=\"waves-effect\">"+link(page, page, rel: rel_value(page))+"</li>"
end
end
def previous_page
num = @collection.current_page > 1 && @collection.current_page - 1
previous_or_next_page(num, "<i class=\"material-icons\">chevron_left</i>")
end
def next_page
num = @collection.current_page < total_pages && @collection.current_page + 1
previous_or_next_page(num, "<i class=\"material-icons\">chevron_right</i>")
end
def previous_or_next_page(page, text)
if page
"<li class=\"waves-effect\">"+link(text, page)+"</li>"
else
"<li class=\"waves-effect\">"+text+"</li>"
end
end
end
end
your_controller.rb
# GET /articles/1
def articles
@articles = @articles.paginate(:page => params[:page], :per_page => 20).order(id: :desc)
@custom_paginate_renderer = custom_paginate_renderer
end
your_view.html.erb
<%= will_paginate @articles, renderer: @custom_paginate_renderer %>
不是最漂亮的 rails 代码,但它有效
感谢指导我为 Bootstrap 5.
编写此渲染器的答案
//config/initializers/bootstrap_paginate_renderer.rb
class BootstrapPaginateRenderer < WillPaginate::ActionView::LinkRenderer
def container_attributes
{ class: 'pagination' }
end
def html_container(html)
child = tag(:ul, html, container_attributes)
tag(:nav, child)
end
def page_number(page)
if page == current_page
'<li class="page-item active">' + link(page, page, rel: rel_value(page),class: 'page-link') + '</li>'
else
'<li class="page-item">' + link(page, page, rel: rel_value(page),class: 'page-link') + '</li>'
end
end
def previous_page
num = @collection.current_page > 1 && @collection.current_page - 1
previous_or_next_page(num, '<span aria-hidden="true">«</span>')
end
def next_page
num = @collection.current_page < total_pages && @collection.current_page + 1
previous_or_next_page(num, '<span aria-hidden="true">»</span>')
end
def previous_or_next_page(page, text)
if page
'<li class="page-item">' + link(text, page, class: 'page-link') + '</li>'
else
'<li class="page-item disabled">' + link(text, page, class: 'page-link') + '</li>'
end
end
end
//app/helpers/application_helper.rb
def will_paginate(coll_or_options = nil, options = {})
if coll_or_options.is_a? Hash
options = coll_or_options
coll_or_options = nil
end
unless options[:renderer]
options = options.merge renderer: BootstrapPaginateRenderer
end
super *[coll_or_options, options].compact
end
Documentation 缺少 will_paginate 自定义渲染器:
There is no documentation how to write your own link renderer, but the source code is pretty self-explanatory. Dive into it, and selectively override methods of the LinkRenderer to adjust them to your needs.
是否有任何非官方文档?
找到了一个不错的博客 post 关于 custom will_paginate renderer
module ApplicationHelper
# change the default link renderer for will_paginate
def will_paginate(collection_or_options = nil, options = {})
if collection_or_options.is_a? Hash
options, collection_or_options = collection_or_options, nil
end
unless options[:renderer]
options = options.merge :renderer => MyCustomLinkRenderer
end
super *[collection_or_options, options].compact
end
end
然后在初始化程序中
class MyCustomLinkRenderer < WillPaginate::ActionView::LinkRenderer do
def container_attributes
{class: "tc cf mv2"}
end
def page_number(page)
if page == current_page
tag(:span, page, class: 'b bg-dark-blue near-white ba b--near-black pa2')
else
link(page, page, class: 'link ba b--near-black near-black pa2', rel: rel_value(page))
end
end
def gap
text = @template.will_paginate_translate(:page_gap) { '…' }
%(<span class="mr2">#{text}</span>)
end
def previous_page
num = @collection.current_page > 1 && @collection.current_page - 1
previous_or_next_page(num, @options[:previous_label], 'link ba near-black b--near-black pa2')
end
def next_page
num = @collection.current_page < total_pages && @collection.current_page + 1
previous_or_next_page(num, @options[:next_label], 'link ba near-black b--near-black pa2')
end
def previous_or_next_page(page, text, classname)
if page
link(text, page, :class => classname)
else
tag(:span, text, :class => classname + ' bg-dark-blue near-white')
end
end
end
感谢之前的回答,我写了这段代码来使用 will_paginate 和 materialize
application_controller.rb
def custom_paginate_renderer
# Return nice pagination for materialize
Class.new(WillPaginate::ActionView::LinkRenderer) do
def container_attributes
{class: "pagination"}
end
def page_number(page)
if page == current_page
"<li class=\"cyan active\">"+link(page, page, rel: rel_value(page))+"</li>"
else
"<li class=\"waves-effect\">"+link(page, page, rel: rel_value(page))+"</li>"
end
end
def previous_page
num = @collection.current_page > 1 && @collection.current_page - 1
previous_or_next_page(num, "<i class=\"material-icons\">chevron_left</i>")
end
def next_page
num = @collection.current_page < total_pages && @collection.current_page + 1
previous_or_next_page(num, "<i class=\"material-icons\">chevron_right</i>")
end
def previous_or_next_page(page, text)
if page
"<li class=\"waves-effect\">"+link(text, page)+"</li>"
else
"<li class=\"waves-effect\">"+text+"</li>"
end
end
end
end
your_controller.rb
# GET /articles/1
def articles
@articles = @articles.paginate(:page => params[:page], :per_page => 20).order(id: :desc)
@custom_paginate_renderer = custom_paginate_renderer
end
your_view.html.erb
<%= will_paginate @articles, renderer: @custom_paginate_renderer %>
不是最漂亮的 rails 代码,但它有效
感谢指导我为 Bootstrap 5.
编写此渲染器的答案//config/initializers/bootstrap_paginate_renderer.rb
class BootstrapPaginateRenderer < WillPaginate::ActionView::LinkRenderer
def container_attributes
{ class: 'pagination' }
end
def html_container(html)
child = tag(:ul, html, container_attributes)
tag(:nav, child)
end
def page_number(page)
if page == current_page
'<li class="page-item active">' + link(page, page, rel: rel_value(page),class: 'page-link') + '</li>'
else
'<li class="page-item">' + link(page, page, rel: rel_value(page),class: 'page-link') + '</li>'
end
end
def previous_page
num = @collection.current_page > 1 && @collection.current_page - 1
previous_or_next_page(num, '<span aria-hidden="true">«</span>')
end
def next_page
num = @collection.current_page < total_pages && @collection.current_page + 1
previous_or_next_page(num, '<span aria-hidden="true">»</span>')
end
def previous_or_next_page(page, text)
if page
'<li class="page-item">' + link(text, page, class: 'page-link') + '</li>'
else
'<li class="page-item disabled">' + link(text, page, class: 'page-link') + '</li>'
end
end
end
//app/helpers/application_helper.rb
def will_paginate(coll_or_options = nil, options = {})
if coll_or_options.is_a? Hash
options = coll_or_options
coll_or_options = nil
end
unless options[:renderer]
options = options.merge renderer: BootstrapPaginateRenderer
end
super *[coll_or_options, options].compact
end