4

I'm using Kaminari for pagination and under a certain situation need the first page to contain only 2 entries while each other to have 6. Thought this was achievable using padding(), but it doesn't seem to work like I'd expect (the documentation doesn't help much either):

a = (1..20).to_a
b = Kaminari.paginate_array(a).page(1).per(6).padding(2)
=> [3, 4, 5, 6, 7, 8]

Any ideas on how to accomplish this?

Clarity
  • 194
  • 3
  • 15
JJK
  • 179
  • 1
  • 2
  • 9

2 Answers2

4

this might help you:

a = (1..20).to_a
b = Kaminari.paginate_array(a).page(1).per(6).offset(2)
=> [3, 4, 5, 6, 7, 8]

tested with Kaminari(0.14.1)

Kuldeep
  • 884
  • 7
  • 12
  • Right, that's the (undesirable) output I got as well. The question is how I would get the first two entries to show up as the first page. In this case I'd hope the output to be: `b = Kaminari.paginate_array(a).page(1).per(6).padding(2)` `=> [1,2]` Thoughts? – JJK Aug 14 '13 at 21:57
  • Instead of padding, you can use take(2) on your result, like: `Kaminari.paginate_array(a).page(1).per(6).take(2) => [1, 2]` – Kuldeep Aug 15 '13 at 07:10
  • Using take() would mean that there are 4 records missing (between the first and second page). – bodacious Dec 30 '13 at 12:11
1

You can use a negative value for padding, lets say you normally display 6 items per page but for the first page you show only 4. You still setup the per value of 6. Then on pages 2+ you can use a padding of -2 to account for the unused records from page 1.

a = (1..20).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
b = Kaminari.paginate_array(a).page(1).per(6) # Get back 6 but only use 4
=> [1, 2, 3, 4, 5, 6]
c = Kaminari.paginate_array(a).page(2).per(6) # Get the next 6
=> [7, 8, 9, 10, 11, 12]
c.padding(-2) # Correct for the missing 2 on first page
=> [5, 6, 7, 8, 9, 10]

In your controller you would do something like this:

@products = Product.active.page(params[:page]).per(6)
@products = @products.padding(-2) if !params[:page].nil? and params[:page].to_i > 1
hwatkins
  • 1,416
  • 8
  • 11