Voici un petit hack pour ajouter Ajax au plugin will_paginate :
Dans le fichier /lib/will_paginate/view_helpers.rb ajoutez/modifiez les lignes suivantes :
module ViewHelpers
# default options that can be overridden on the global level
@@pagination_options = {
:class => 'pagination',
:previous_label => '« Previous',
:next_label => 'Next »',
:inner_window => 4, # links around the current page
uter_window => 1, # links around beginning and end
:separator => ' ', # single space is friendly to spiders and non-graphic browsers
:param_name => :page,
:params => nil,
:renderer => 'WillPaginate::LinkRenderer',
:page_links => true,
:container => true,
# Ligne a ajouter
:update => nil # DOM element to update
}
mattr_reader :pagination_options
# suite du code ...
Puis plus bas, on modifie la fonction page_link
def page_link(page, text, attributes = {})
if @options[:update].nil?
@template.link_to text, url_for(page), attributes
else
# Ajax update
@template.link_to_remote text, {:url => url_for(page), :update => @options[:update]}, :html => attributes
end
end
Et voilà maintenant vous n’avez plus qu’à utiliser
will_paginate @collections, :update => "mon_id"
dans vos vues.
Bien sûr si vous ne spécifiez pas l’attribut :update, will_paginate fonctionnera comme avant.
Voici un lien vers le fork de will_paginate que j’ai créé :
http://github.com/solisoft/will_paginate/tree/master
Enjoy.
Recent Comments