Only display 5 items at a time with more button

I have the following code:

Matt Royer wrote:

----

<div id="order">

<ul>

<% @orders.reverse.each do |order| %>

<li><%= link_to_remote( "#{order.id} #{order.name}" , :url => {:controller => 'orders', :action => 'show', :id => order.id }, :update => "order_div", :method => 'get' ) %></li>

<% end %>

</ul>

</div>

----

I want to create a button or link to scroll these order items by 5 at a time. So it should display the first 5 by default. I will then be able to click a button below and it should scroll the next 5, hiding the previous.

Nevermind. I know I should post this at one of the JS framework forums. Only posted here to see if there is any way to take care of the Rails side of things to cut down on the JS work.

Matt Royer wrote:

<% @orders.reverse.each do |order| %>

First off; rather than reversing the @orders; why don't you have the finder that populates it order how you want? Seems like an extra, redundant step to me.

Nevermind. I know I should post this at one of the JS framework forums. Only posted here to see if there is any way to take care of the Rails side of things to cut down on the JS work.

Then for your scrolling; you could use the commonly-used Will Paginate to return "pages" of five orders at a time. How you render the pages (with funky JS fading, etc) is your concern; but it's not uncommon to use RJS (or your own JS) to replace the content of a div with the return from a controller.

Michael Pavling wrote:

First off; rather than reversing the @orders; why don't you have the finder that populates it order how you want? Seems like an extra, redundant step to me.

Thank you very much for the tip! I'm still getting the hang of Rails and Ruby. It was a way that worked quickly for me, but your way makes a lot more sense.

Then for your scrolling; you could use the commonly-used Will Paginate to return "pages" of five orders at a time. How you render the pages (with funky JS fading, etc) is your concern; but it's not uncommon to use RJS (or your own JS) to replace the content of a div with the return from a controller.

Perfect! Thank you very much. I've been working on this problem forever. Honestly had no idea how to achieve that functionality. I mainly wanted 5 items at a time. Didn't really care how I got there (thought JS would be the quickest, that's why I posted this), so thank you very much for the solution!

-- Matt Royer

there is a function called in_groups_of .. there is a railscast episode about it. I have used a manual scrolling slider similar to what you want at the bottom of http://www.conikitv.com/ where it will grab the first hundred videos and display them in groups of 10 using the slider.

Basically the thinking is - Fetch the items you want to show - display them in groups of 10       render how each group is displayed - render the container for each group

I hope this helps.

Links: #28 in_groups_of - RailsCasts : http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html#M001216

Matenia Rossides wrote:

there is a function called in_groups_of .. there is a railscast episode about it. I have used a manual scrolling slider similar to what you want at the bottom of http://www.conikitv.com/ where it will grab the first hundred videos and display them in groups of 10 using the slider.

Basically the thinking is - Fetch the items you want to show - display them in groups of 10       render how each group is displayed - render the container for each group

For efficiencies sake you shouldn't fetch all the items up-front. The user may never scroll/page past the first couple of groups. Better to use will_paginate to fetch the additional groups.