Reputation: 205
I'm using rails 3.2.13
I have this array of hashes:
[{"name" => 1, "foo" => "bar"},{"second" => 2, "foo" => "bar"}]
I put them in a variable called @names and display them in the view like this:
<% @names.each do |name| %>
<%= name["name"] %>
<%= name["foo"] %>
<% end %>
The big question is how do i "paginate" them in such a way that there's button NEXT that shows me the first entry, then when i press next came the second and so on.
I have considered will_paginate but it didn't make sense since I still have to process in the view what I have to display in the page. Any ideas? Thanks a lot!
Upvotes: 0
Views: 95
Reputation: 3709
Use jQuery for this.. I hope this will give an idea how to do with jQuery.
<% @names.each do |name| %>
<div id="<%= name["name"] %>" style="display: none;">
<%= name["name"] %>
<%= name["foo"] %>
<%= link_to_function "Next" , "$(this).parent('div').hide();$(this).next().show();" %>
</div>
<% end %>
<script type="text/javascript">
$(document).ready(function(){
$("#<%= @names.first["name"] %>").show();
});
</script>
Upvotes: 1
Reputation: 14038
You could use kaminari, which allows you to paginate an array object very easily.
Keep in mind that paginating the array will change the content of the array so your view code doesn't change. You just need to add a pagination helper to allow moving through the array pages:
<%= paginate @names %>
Upvotes: 3
Reputation: 8638
will_paginate handles it all for you. Normally it uses ActiveRecord
relations, but it can paginate array as well see this stackoverflow question
Upvotes: 3