will_paginate自定义渲染器缺少Documentation:

没有的文档,没有的文档来介绍如何编写自己的链接渲染器,但是源代码是不言自明的。深入研究它,并选择性地覆盖LinkRenderer的方法,以根据需要进行调整。

有非官方文件吗?

最佳答案

找到了关于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) { '&hellip;' }
    %(<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

关于ruby-on-rails - 自定义will_paginate渲染器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44680975/

10-13 01:49