Generate an HTML Table from an Array of Hashes in Ruby

Generate an HTML table from an array of hashes in Ruby

Use the XMLBuilder for this:

data = [{"col1"=>"v1", "col2"=>"v2"}, {"col1"=>"v3", "col2"=>"v4"}]
xm = Builder::XmlMarkup.new(:indent => 2)
xm.table {
xm.tr { data[0].keys.each { |key| xm.th(key)}}
data.each { |row| xm.tr { row.values.each { |value| xm.td(value)}}}
}
puts "#{xm}"

Output

<table>
<tr>
<th>col1</th>
<th>col2</th>
</tr>
<tr>
<td>v1</td>
<td>v2</td>
</tr>
<tr>
<td>v3</td>
<td>v4</td>
</tr>
</table>

Dynamic display of an array of hashes in a html table?

Hope this can help:

Solution 1: based on same .data content # Deprecated

Conditions:

  • All product.data have the same amount of pairs of key/value.
  • The order of the pairs is the same for all the products.

The code:

# headers
%tr
- @products.first.data.keys.each do |attribute_name|
%th= attribute_name
# body
- @products.each do |product|
%tr
- product.data.attributes.each do |attribute_value|
%td= attribute_value

This code will render properly if each product.data has the same amount of key/value pairs AND stored in the same order.


Solution 2: fully dynamic (.data varying) # Recommended

Conditions:

  • The number of pairs (of key/val) of product.data is varying between each product
  • The order of the pairs is not the same for all the products.

The code:

# we gather all possible attribute's name in the data hash:
- headers = @products.map(&:data).flat_map(&:keys).uniq

%tr
# we make a table-head cell for each attribute's name:
- headers.each do |key|
%th= key

- @products.each do |product|
%tr # for each attribute's name, we display a TD cell and try to display it's value if exists
- headers.each do |key|
%td= product.data[key]

Feel free to ask for details, I feel like I just gave you a piece of code without any explanation...

Hashes made into tables

You can simple get your values like this:

%table
%thead
%tr
%th
Name
%th
Type
%th
Price
%tbody
- @products.each do |product|
%tr
%td
= product.name
%td
= product.data["type"] unless product.data.nil?
%td
= product.data["price"] unless product.data.nil?

Sinatra: Array into HTML table

In the controller do

get '/something' do
@ary = ['a','b','c']
erb :'something'
end

In the view named something you can do

<table>
<% @ary.each do |elem| %>
<tr>
<td>
<%= elem %>
</td>
</tr>
<% end %>
</table>

Ruby on rails array to HTML table

I don't think you can use an instance variable outside of the scope of the controller instance. In the way you are using it, you could only declare it as a class variable (i.e. @@my_arr), which you probably don't want to do.

Instead, you should wrap the declaration of @my_arr in a method:

class PagesController < ApplicationController

def listing
@my_arr = [{ville_id: '1', ville_name: 'Paris', population: 2000000, pays: 'France'},
{ville_id: '2', ville_name: 'Madrid', population: 3000000, pays: 'Espagne'},
{ville_id: '3', ville_name: 'Marrakech', population: 1000000, pays: 'Maroc'},
{ville_id: '4', ville_name: 'Tokyo', population: 9000000, pays: 'Japon'},
{ville_id: '5', ville_name: 'Istanbul', population: 15000000, pays: 'Turquie'}]
end

end

The method is named listing to correspond with the view.

Also, as someone else mentioned, you are creating Hash objects, and properties of those objects are accessed via the [] method (yes, [] is a method!), i.e. myarr[:ville_id], myarr[:ville_name], etc.).



Related Topics



Leave a reply



Submit