rails will_paginate ajax

http://www.tricksonrails.com/2010/04/rails3-and-will_paginate-doing-easy-remote-links-rails3-tricks-02/


As you know, Rails3 use only UJS (unobtrusive javascript), so for every remote link, Rails3 just add the data-remote attribute to links :

<a href="ajax_page.html" data-remote="true">A remote link ! </a>

If you want to do ajaxed pagination, there is no easy way with will_paginate to do remote link (or I didn’t find any one), but with Rails3 UJS, there is a little tricks do to this easily !

Here is the haml and jquery code :

= will_paginate (@users )
:javascript
  $ ( '.pagination a' ). attr ( 'data-remote', 'true' );

This snippet of code just add the attribute data-remote to pagination links. And that’s it ! Our pagination will now be ajaxed :)



------------------------------------------------------------

$$('.pagination a').each(function(e){
var link=e.getAttribute('href');
e.setAttribute(
'onclick',
'new Ajax.Request(\'' + link + '\', {asynchronous:true, evalScripts:true})');
e.writeAttribute('href',null);
}



你可能感兴趣的:(JavaScript,jquery,Ajax,function,Rails,asynchronous)