pagination in ajax

hi,   I am curently using rails version 2.1.1.   Currently i have done simple pagination using will_paginate   plugin.it works.but when i am trying pagination using ajax   with help of will_paginate plugin   it wont works.i am also using RemoteLinkRenderer helper,   but it gives an error uninitialized constant RemoteLinkRenderer.   can anyone provide any tutorial or sample code so that i can implement   pagination using ajax with will_paginate plugin or anything else.

                               Thanks.

Hi    Add the following code to view_helpers.rb .The file is in vendor/plugins/will_paginate/lib/will_paginate(if you are using plugin)

   def page_link_or_span(page, span_class = 'current', text = nil)     text ||= page.to_s     if page and page != current_page       if update = @options[:update]         @template.link_to_remote text, :update => update, :url => url_options(page)       else         @template.link_to text, url_options(page)       end     else      @template.content_tag :span, text, :class => span_class    end

   Then for ajax pagination in view do like <%= will_paginate @thecollectionyouwanttopaginate, :update=>'div', :params=>{:controller=>'controller_name',:action=>'action_name',:any_additional_parameters=>@any_additional_parameters}, :container => false %>

    And for normal pagination do what you are currenly doing

Sijo

Sijo Kg wrote:

Hi    Add the following code to view_helpers.rb .The file is in vendor/plugins/will_paginate/lib/will_paginate(if you are using plugin)

   def page_link_or_span(page, span_class = 'current', text = nil)     text ||= page.to_s     if page and page != current_page       if update = @options[:update]         @template.link_to_remote text, :update => update, :url => url_options(page)       else         @template.link_to text, url_options(page)       end     else      @template.content_tag :span, text, :class => span_class    end

   Then for ajax pagination in view do like <%= will_paginate @thecollectionyouwanttopaginate, :update=>'div', :params=>{:controller=>'controller_name',:action=>'action_name',:any_additional_parameters=>@any_additional_parameters}, :container => false %>

    And for normal pagination do what you are currenly doing

Sijo

Hi,sijo.     Thanks for ur suggestion,but i am not using will_paginate plugin.     I use mislav will_paginate,can i use same code as above,     since there is no plugin inside vendor/plugin.any suggestion required?                                                 Thanks.

Hi     If u use gem install to install will_paginate the library files you can find inside /usr/lib/ruby/gems/#versionNo/gems/ (For linux) for windows you have to find out where the location..I think in ProgramFiles(right?)      You can find all the installed gems there .And go to will_paginate directory there Inside that go to lib directory .There you can find out the required file..

Sijo

Sijo Kg wrote:

Hi     If u use gem install to install will_paginate the library files you can find inside /usr/lib/ruby/gems/#versionNo/gems/ (For linux) for windows you have to find out where the location..I think in ProgramFiles(right?)      You can find all the installed gems there .And go to will_paginate directory there Inside that go to lib directory .There you can find out the required file..

Sijo

Thanks,Sijo,

   i apply all these code ,but it is not working as i need.    i want only particular div is replaced but total page    is refresh.here is my code.     in controller::        @user = User.paginate :page => params[:page], :per_page => 2     in view ::       <table>   <% for usr in @user%>   <tr><td><%=usr.user_name%></td>    <td>   <%= link_to_remote 'Edit User', :update => 'GeneralDiv', :complete => "Element.show('GeneralDiv');", :url => "/userinfo/edituser/#{usr.id}" %></td> <td>   <%= link_to_remote 'Delete User', :confirm => 'Are you sure?', :update => 'GeneralDiv', :complete => "Element.show('GeneralDiv');", :url => "/userinfo/deleteuser/#{usr.id}" %></td></tr> <% end %>

<%= will_paginate @user, :update=>'GeneralDiv', :params=>{:controller=>'userinfo',:action=>'showuser'}, :complete=>"Element.show('GeneralDiv');", :container => false %> </table>    and i also apply ur code in view_helpers.rb.    i attach this file.

   any suggestion,                                  thanks.

Attachments: http://www.ruby-forum.com/attachment/2861/view_helpers.rb

Hi

  Which version of gem r u using? In controller you can do like @user = User.paginate :page => params[:page], :per_page => 2   and in view you can do <%= will_paginate @user,:update=>'GeneralDiv',:params=>{:controller=>'userinfo',:action=>'showuser',:sd_id=>@sd_ticket}, :container => false %>

I dont know why you use :complete=>"Element.show('GeneralDiv');"

   Also pass the params (if any) to will_paginate in view..And restart your server also.

Sijo

I just don’t understand why people want to make it so hard on themselves. I know for a fact that will_paginate wraps the pagination links in a

with a class you can use, called “pagination”. In your application.js file, you just put the following (I’ve also provided a link to a syntax highlighted pastie below, easier to read):

document.observe(“dom:loaded”, function() {

// pagination is the class will_paginate wraps around the links

// let’s keep the number of observers limited to one, your browser will thank you

$$(‘.pagination’).observe(‘click’, function(e) {

var clicked = $(e.target);

// time to see if the clicked item was indeed one of the links

if(clicked.match(‘a’)) {

// if it was, stop the default page refresh and fire an ajax request instead

// let’s not forget a RESTful design wants use to use GET instead of the default POST method

e.stop();

new Ajax.Request(clicked.href, {method: ‘get’});

}

});

}

http://pastie.org/303029

The code is untested, but looks to me like I didn’t make a mistake. You could even take the event delegation a bit further and put it on the window element, but that’s up to you guys to experiment with.

Best regards

Peter De Berdt

Memo to self: don’t write code while being ill, you make stupid mistakes. Updated code that should work better (pastie has also been updated):

document.observe(“dom:loaded”, function() {

// pagination is the class will_paginate wraps around the links

$$(‘.pagination’).each(function(pagination) {

// let’s put the observer on the div element and not all the links, your browser will thank you

pagination.observe(‘click’, function(e) {

var clicked = $(e.target);

// time to see if the clicked item was indeed one of the links

if(clicked.match(‘a’)) {

// if it was, stop the default page refresh and fire an ajax request instead

// let’s not forget a RESTful design wants use to use GET instead of the default POST method

e.stop();

new Ajax.Request(clicked.href, {method: ‘get’});

}

})

});

}

Best regards

Peter De Berdt