# How to calculate per\_page value based on browser window size for will\_paginate

**URL:** <https://www.ruby-forum.com/t/how-to-calculate-per-page-value-based-on-browser-window-size-for-will-paginate/185826>\
**Category:** Rails\
**Created:** [March 26, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/how-to-calculate-per-page-value-based-on-browser-window-size-for-will-paginate/185826 "2010-03-26T19:31:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [March 26, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/how-to-calculate-per-page-value-based-on-browser-window-size-for-will-paginate/185826/1 "2010-03-26T19:31:44Z")

</div>

def self.per\_page  
20  
end

Any suggestion on how I can calculate this value base on the size of  
the browser window?

Thanks

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [March 26, 2010, 7:56pm UTC](https://www.ruby-forum.com/t/how-to-calculate-per-page-value-based-on-browser-window-size-for-will-paginate/185826/2 "2010-03-26T19:56:55Z")

</div>

Why not allow the user to choose this value (i.e. via a drop down  
menu )?

If you go that route, don’t forget to store this preference somewhere  
so your users don’t have to set it every time. (I’m looking at you  
[Blockbuster.com](http://Blockbuster.com))

Eric

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [March 26, 2010, 11:54pm UTC](https://www.ruby-forum.com/t/how-to-calculate-per-page-value-based-on-browser-window-size-for-will-paginate/185826/3 "2010-03-26T23:54:01Z")

</div>

Ok, I tried as you suggested with:

\<%= select\_tag(:per\_page, options\_for\_select(%w{10 20 30 40 50},  
selected = session[:per\_page]), :onchange =\> “session[:per\_page] =  
this.value”) %\> Lines per page

I can’t seem to get the :onchange to set a new value for  
session[:per\_page]. I checked :onchange with an alert(this.value) and  
it did what I expected.

Can you proved a sample of what I should be doing in :onchange

Thanks.

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [March 27, 2010, 5:49am UTC](https://www.ruby-forum.com/t/how-to-calculate-per-page-value-based-on-browser-window-size-for-will-paginate/185826/4 "2010-03-27T05:49:51Z")

</div>

Thanks for your ideas.

I decided to make per\_page an attribute of the user and set it at  
login. That way it gives all my list pages a common look.

I had already put this inside a form\_tag I am using for a search box,  
so I didn’t want to add another form\_tag. The search box and the  
pagination boxes took up enough room on the page. Can you direct two  
different form\_tags to appear on the same line?

I still don’t get what I would put in the :onchange option. Can you  
give me an example?

---

<div class="post-metadata">

**Author:** ![RVRoadie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@RVRoadie](https://www.ruby-forum.com/u/RVRoadie)\
**Post date:** [March 27, 2010, 12:06am UTC](https://www.ruby-forum.com/t/how-to-calculate-per-page-value-based-on-browser-window-size-for-will-paginate/185826/5 "2010-03-27T00:06:18Z")

</div>

> Can you proved a sample of what I should be doing in :onchange

I’ve used an “Apply” button inside of form\_tag, which sends params  
back to the controller and I set the session there.

If you wanted to use the :onchange event instead of requiring the user  
to click on a button, you would probably have to send an  
XMLHttpRequest to the server and then refresh the page with the new  
paginated data.

Eric
