Forum: Ruby on Rails pagination in ajax

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Subhadip C. (Guest)
on 2008-10-27 12:44
 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.
Sijo k. (Guest)
on 2008-10-28 06:22
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
Subhadip C. (Guest)
on 2008-10-28 18:40
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.
Sijo k. (Guest)
on 2008-10-29 05:44
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
Subhadip C. (Guest)
on 2008-10-29 07:23
Attachment: view_helpers.rb (0 Bytes)
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.
Sijo k. (Guest)
on 2008-10-29 09:00
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
Subhadip C. (Guest)
on 2008-10-29 10:56
Sijo Kg wrote:
> 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

  thanks,Sijo,
     but the problem is still pending.when i click on next page link,
     total page is refresh like normal pagination.
     here is my code,

  <%= will_paginate @user, :update=>'GeneralDiv',
:params=>{:controller=>'userinfo',:action=>'showuser'},
:container => false %>
    can i call the same function 'showuser' every time .
    the div is replaced after refreshing of total page,can i stop
    this page refresing,this is not possible using this code.
    beside that i am using rails version 2.1.1.
                             Any suggestion?
                             Thanks.
Peter De Berdt (Guest)
on 2008-10-29 12:42
(Received via mailing list)
On 29 Oct 2008, at 09:56, Subhadip Chakraborty wrote:

>> ',:params
>  thanks,Sijo,
>    beside that i am using rails version 2.1.1.
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 <div> 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
Peter De Berdt (Guest)
on 2008-10-29 12:47
(Received via mailing list)
On 29 Oct 2008, at 11:41, Peter De Berdt wrote:

> will thank you
>     }
>   });
> }
>
> 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.
>

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
This topic is locked and can not be replied to.