Exploring and organizing data for customized lists in Ruby/ERB

I am looking to create a list layout similar to the one found here:

This is the code I have so far:

<div class="wrapper">
 <ol>
  <% CraigslistZipCode.select("distinct region, fee").each do |record| %>
   <li><%= record.region %> <%= record.fee %></li>
  <% end %>
 </ol>
</div>

I am struggling to format the data into three columns as desired while looping through the records. I want to prevent the list from becoming excessively long and instead split it into multiple columns after a certain number of items.

Answer №1

If you want to limit each column to a maximum of 5 entries, here's a simple solution:

max_columns = 5

elements = (1..17).to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

# Fill the array with nil values so that it can be evenly divided by max_columns
elements.fill(nil, elements.size, ((max_columns - elements.size) % max_columns))
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, nil, nil, nil]

# Divide the array into groups of max_columns and then transpose them
elements.each_slice(max_columns).to_a.transpose
# => [[1,  6,  11, 16],
#     [2,  7,  12, 17],
#     [3,  8,  13, nil],
#     [4,  9,  14, nil],
#     [5, 10,  15, nil]]

Now you have an array of rows ready for displaying in columns, just ensure to handle any empty values inserted through padding.

The reason behind adding empty values is because Array#transpose requires all entries to be of equal length.

Answer №2

If you want to create groups of items, you can utilize the each_slice method. Ensure that each group contains enough items to fill three columns when displayed.

<%
data = CraigslistZipCode.select("distinct region, fee")
group_size = (data.count / 3.0).ceil
%>
<% data.each_slice(group_size) do |group| %>
  <div class="wrapper">
    <ol>
      <% group.each do |zip_code| %>
        <li><%= record.region %> <%= record.fee %></li>
      <% end %>
    </ol>
  </div>
<% end %>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to loop through an array of JSON data in order to consistently retrieve the initial JSON object?

How can I retrieve only the full highlight videos from the JSON object in the video array? { "response": [ { title: "United vd Chelsea", "videos": [ { "title": "Highlights&quo ...

Height of border not being displayed accurately

I have been searching for a solution to my unique issue with inserting borders in HTML and CSS. When I try to add a border to three images, the height does not display correctly. This is all part of my learning process to improve my skills in coding. Belo ...

Modify input attribute by selecting a label

What I'm looking for: I am trying to add a checked status to the input field when a label is clicked. However, when I attempted this, I received two alerts displaying "true." This behavior is incorrect, and upon inspection in Firebug, the input fiel ...

What is the best way to shift a text element within the footer section?

I have arranged two columns within the container, but I am looking to position the .company and .copyright text at the bottom left of the footer column. The Follow Us heading should be on the right side with the .justify-text below it, followed by social m ...

When jQuery adds new elements, their CSS may not be applied accurately

One issue I encountered is using jQuery to dynamically add new elements, only to find that the CSS isn't being applied correctly to the newly added element. To illustrate this problem, I created a jsFiddle. In the example, there are differences in sp ...

Issue with margin:auto in Internet Explorer 5

I have been experimenting with IE11's developer tools and discovered that when changing the browser mode, everything appears as expected in Edge, 10, 9, 8, and 7. However, in IE5, the div is forced to align to the left instead of the middle. Could so ...

Emphasize the center row within a moving table

I am interested in developing a scrolling table where only 10 rows are visible at any given time, with the middle row set to stand out even during scrolling. The concept is that as the user scrolls down, the highlighted row changes progressively as they c ...

Unable to correctly declare and display UTF-8 character within JSON syntax

In my JSON object, there is an attribute that contains a unique special character - I've attempted to save the string in encoded UTF-8 as "\xF0\x9F\x94\x94" or tried displaying it using its HEX value - String.fromCharCode(0x1F514 ...

Limitations of jQuery: onClick function not transferable

I am struggling to achieve the following functionality for my navbar dropdown menus: When the screen size is greater than 992px, I want the navbar dropdowns to open on mouse enter. For screens smaller than 992px, I want the dropdowns to open on ...

What is the method of generating an HTML tag solely using CSS?

Looking to style HTML tags without altering them in any way? Consider this example: <div id="Code_G" class="editor-group"> editor-group<br /> <div id="Code_L" class="editor-label "> editor-label </div> < ...

Getting table data from a Django database and displaying it on an HTML page

Currently, I am working with a Django table that has 3 fields. I have a total of 4 entries in this table, but I only want to display 2 of the 3 fields in a table. However, I am struggling with achieving this as I have only been able to display 1 row instea ...

Guide on dragging and dropping without losing the item, allowing for continuous drag and drop functionality

function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.target.id); } function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); ev.target.appendChild(document.getElementB ...

Contrasting views when viewing content on localhost versus accessing it over a network

After creating a bootstrap web portal that appeared fine when viewed on my pc using Wampserver localhost, I encountered issues when accessing it through the office network. Apparently, elements that should have spanned horizontally were now spanning vertic ...

Adding a see-through Bootstrap Navbar to a Fullpage.js website: A step-by-step guide

I am trying to incorporate a transparent Bootstrap Navbar into my page that is using fullpage.js. The desired outcome is for the navbar to stay on top of the page without affecting the layout of the website content. I have referenced an example here, but ...

What is the best way to extract user input from a bootstrap search bar and integrate it into an ajax request to fetch information?

I am currently utilizing HTML, Javascript, and bootstrap to develop a web application. However, I have encountered an obstacle. When using the code document.getElementById("input here"), it only returned an array of 0. My goal is to retrieve data from an A ...

Please fill out and submit a form by clicking on a button in HTML

I am currently developing a software program that needs to automatically submit a form on a webpage by "clicking" on a button: <button class="form" type="submit">Send</button> Based on my limited knowledge, I understand that in order to submi ...

Is it time to ditch Internet Explorer for EDGE?

Have you ever noticed that when attempting to access the stackoverflow website on Internet Explorer, the tab mysteriously closes and Microsoft Edge opens with stackoverflow loaded? What is the secret behind this strange phenomenon on stackoverflow's ...

Python Selenium struggles to locate element via id

Below is an example of the html element: <button role="button" title="Join Meeting" id="interstitial_join_btn" class="style-rest-1IrDU style-theme-green-22KBC style-join-button-yqbh_ style-size-huge-3dFcq style-botton- ...

Tips for aligning slick slider and a div on the same line

I've been working on incorporating an HTML slider with heading information at the top. Although I created a jsfiddle example, I'm struggling to align the Heading column and slider on the same line without any line breaks. I attempted to use col ...

Could it be that v-show does not function properly on elements that are not dynamic

I'm not experienced in web development and this is my first attempt at using a web framework. The framework I am currently learning is vue.js version 3. My goal: I want to create 4 app instances (nav, defaultApp, bootstrapApp, vueApp). When I select ...