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.
janvier 18th, 2009 at 3:53
Il faudrait forcer la méthode en GET dans la méthode page_link du view_helpers.rb sinon il fait un POST par défaut donc on arrive sur le formulaire d’insertion lorsqu’on travaille en RESTful
janvier 18th, 2009 at 9:37
2e modif importante : toujours dans le view_helpers.rb, dans la méthode page_link, le dernier paramètre doit être passé avec le symbole html_options ou sans symbole. Mais il ne s’appelle pas options (en tout cas dans Rails 2.2.2).