カスタムwill_paginage出力
3254 ワード
カスタムwill_paginage出力
from Let Rails by
yuanyi
will_パギナートは、Railsでよく使われている改ページプラグインですが、ときには、WillPaginateを拡張することによって、例えば、次のrenderは、NextとProviousリンク(ここですから)を削除します. url_for(page)は、あるページを指すリンクを返します.例えば、url_for(1)=>'/posts?page=1’ total_ページ総数 に戻る. page_link_or_spanは、あるページを指すリンク を返します.
もっと多い方法はWillPaginateのviewにあります.helper.rbで見つけました.
from Let Rails by
yuanyi
will_パギナートは、Railsでよく使われている改ページプラグインですが、ときには、WillPaginateを拡張することによって、例えば、次のrenderは、NextとProviousリンク(ここですから)を削除します.
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でこのカスタムrenderを使うには、追加する必要があります.renderパラメータで結構です.
<%= 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の役割は一つのビューで何度もwill_を呼び出すことができるからです.paginateは、inputboxを区別する必要があります.このrenderはWillPaginateから継承するいくつかの方法を使いました.もっと多い方法はWillPaginateのviewにあります.helper.rbで見つけました.