1

I want to dynamically display a list of items, but the number of items by row depends on the width of the screen. the number inside each_slice depends on the screen size... if large screen:

<% @posts.each_slice(3) do |s| %>
  <div class="row">
    <% s.each do |p| %>  
     <div class="small-12 medium-6 large-4 columns">

if medium screen:

<% @posts.each_slice(2) do |s| %>
  <div class="row">
    <% s.each do |p| %>  
      <div class="small-12 medium-6 large-4 columns">

Anyone knows how to have a js variable in

each_slice("javascript vairable") 

Thank you

3 Answers 3

2

I found a way. I used Jquery to check the size of the window with: (coffee script)

n=2
if $('body').width() > 1025
  n = 3
if $('body').width() > 640
  $(".break:nth-child("+n+"n").addClass "row"

the html.erb looks like this:

<div class="row">
  <% @posts.each do |p| %>
    <div class="break">
      <div class="small-12 medium-6 large-4 columns">

and then with jquery I select the nth div and add a class row. It is not prefect but it works.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not possible for JavaScript to interact with your ERB the way you're describing. The server doesn't know or care what the screen size of the client is.

You're using ZURB Foundation markup, right? If you REALLY want the server to be changing the markup based on screen size, consider using Foundation's Interchange functionality.

You'd need to reference a separate URL for each screen size that you're targeting. (Either via a URL parameter or create a separate action for each size.) Rails would output different markup depending on the URL that Interchange calls.

3 Comments

What about with gon ?
I have never used gon. This is the approach that I know.
Interchange is also nice in this scenario because it handles the event where the window size CHANGES after the server request (i.e., user changes orientation of their mobile device or resizes their browser window).
0

This really sounds like a job for CSS media queries. Judging by the CSS class names you are already using some sort of grid system.

<% @posts.each do |s| %>
  <div class="small-12 medium-6 large-4 columns">

Then in CSS:

@media (screen and max-width: 800px) {
  .medium-6 {
    width: 200px;
  }
}

@media (screen and max-width: 1000px) {
  .large-4 {
    width: 400px;
  }
}

You'll have to fiddle with the width values to get the desired effect.

1 Comment

I have experience working with this sort of problem. Your suggestion falls apart because of those .row containers. You'd end up with columns that are the correct width but then get pushed down to new rows at inopportune times. But without the .row containers, you may be on to something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.