Merge tables to a single table - html

Relevant snippets
Show.html.erb
<% outbound_messages.each do |outbound_message| %>
<h5>Outbound Message</h5>
<%= render "trace/display_tabular_data", :data => outbound_message %>
<% end %>
Display_tabular_data.html.erb
<table border="1px solid black">
<thead>
<tr>
<%data.each do |key,value|%>
<th><%=key.capitalize%></th>
<%end%>
</tr></thead><tr>
<%data.each do |key,value|%>
<td><%=value%></td>
<%end%>
</tr>
</table>
So what happens is that each row of data, gets printed in a unique table.
So one has something like http://imgur.com/1gskRvX
But clearly a much better result would be as a single table (Desired outcome)
Outbound Message
Message ID, Exchange, Relayed
Row1
Row2
Row3
Row4
...
....
Any ideas how I can go about this? Display_tabular_data is called at-least 15 times in different places in show.html.erb, so it would be much easier if it was somehow possible to get this final result by making changes only in display_tabular_data, and not in show.html.erb. If not possible, please give me the best possible way?

If you don't want to render a separate table for every object, how about something like this in show.html.erb:
<% unless outbound_messages.empty? %>
<%= render 'trace/display_tabular_data', :data => outbound_messages %>
<% end %>
And then in the partial:
<h5>Outbound Messages</h5>
<table border="1px solid black">
<thead>
<tr>
<% data.first.each do |key,value| %>
<th><%=key.capitalize%></th>
<% end %>
</tr>
</thead>
<% data.each do |outbound_message| %>
<tr>
<% outbound_message.each do |key,value|%>
<td><%=value%></td>
<% end %>
</tr>
</table>
This only works if you are confident that every outbound_message has the same set of keys.

Here you go..
<% if outbound_messages.count > 0 %>
<h5>Outbound Message</h5>
<table border="1px solid black">
<thead>
<tr>
<td>Message ID</td>
<td>Exchange</td>
<td>Relayed</td>
</tr>
</thead>
<% outbound_messages.each do |outbound_message| %>
<tr>
<td>
<%= outbound_message[:message_id] %>
</td>
<td>
<%= outbound_message[:exchange] %>
</td>
<td>
<%= outbound_message[:relayed] %>
</td>
</tr>
<% end %>
</table>
<% end %>
You can eliminate the partial entirely

Related

How to get first data from the array?

I have an issue in the node (html file). I need to get the first data instead of everything. I used this
<% if(tblData) {%>
<% tblData.forEach(function(res,row) {%>
<tr>
<td>
<%= res.name %>
</td>
</tr>
<% }) %>
<% } %>
I got all the names. But instead of getting all the names I want only the first name. Is there any solution for this?
You can get the first element of a table by putting its index in square brackets (myTable[0]):
<% if(tblData) {%>
<tr>
<td>
<%= tblData[0].name %>
</td>
</tr>
<% } %>

Each nested attributes in a table in Rails

I've got a form with nested attributes, now I'm wanting to show on a table, but I'm not able to break the line it gets interacting as a column. How can I do? What can I do?
Here's an image of my current table.
<% #salmonella.process_salmonellas.each do |process| %>
<tr align="center">
<td><%= process.title %></td>
<table>
<% process.stages.each do |stage| %>
<tr>
<td><%= stage.title %></td>
<td>
<table>
<% stage.lines.each do |line| %>
<tr>
<td><%= line.material %></td>
<td><%= line.indicator_name %></td>
</tr>
<% end %>
</table>
</td>
</tr>
<% end %>
</table>
</tr>
And this is how it should look like
That would be more or less the table template I drew in my head:
<table>
<!-- Each for principal object -->
<tr>
<td><!-- fields of principal object --></td>
<td>
<!-- each nested attributes from principal object and the model -->
<tr>
<td><!-- fields of second object--></td>
<td>
<!-- each from model and another model that is nested too from principal object and model -->
<tr>
<td><!-- fields of third object --></td>
</tr>
</td>
</tr>
</td>
</tr>
</table>
that's how nested this is in my controller:
def salmonella_params
params.require(:salmonella).permit(:title,
process_salmonellas_attributes: [
:id,
:title,
:_destroy,
stages_attributes: [
:id,
:title,
:_destroy,
lines_attributes:[
:id,
:material,
:indicator_name,
:_destroy
]
]
])
end
I did as Jeremy recommended and it looked like this:
<% #salmonella.process_salmonellas.each do |process| %>
<tr>
<% process.stages.each do |cont| $b = cont.lines.count end %>
<td rowspan="<%= $b %>"><%= process.title %></td>
<% process.stages.each do |stage| %>
<td rowspan="<%= stage.lines.count %>"><%= stage.title %></td>
<% stage.lines.each_with_index do |line, o| %>
<% if o == 0 %>
<td><%= line.material %></td>
<td><%= line.indicator_name %></td>
<% else %>
<tr>
<td><%= line.material %></td>
<td><%= line.indicator_name %></td>
</tr>
<% end %>
<% end %>
</tr>
<% end %>
Now it's working the way the image works.

How to make an HTML table, which is automatically create new row after six item in column?

I'm trying to make my own site and I don't know how to better dispay images from database. I want to display six images in one row, but the next six item I want to display in second row. How can I make it with this code?
<table width="80%">
<% #products.each do |product| %>
<td>
<%= product.price %>"> <%= image_tag(product.image_url) %> </td>
<% end %>
</table>
Sorry, if this question is really stupid. But I'm just learning and It will help me understand Ruby better.
You could use Enumerable#each_slice to divide them into sub arrays like so:
<table width="80%">
<% #products.each_slice(6) do |row| %>
<tr>
<% row.each do |product| %>
<td><%= product.price %>"> <%= image_tag(product.image_url) %> </td>
<% end %>
</tr>
<% end %>
</table>

Send html table data has same name

My table is showing two columns, "#" and "String". I have made the table with each td having its value shown through a text_field so the user can change the value and submit the changes.
The problem is how to send all the values with the form since each td has the same id and name making only the last td sent.
How do I send all the td values?
The table:
<%= form_for(#skein) do |s| %>
<table width="100%" class="tablesorter">
<thead>
<tr>
<th width="20%" data-placeholder="Search">#</th>
<th width="80%" data-placeholder="Search">String</th>
</tr>
</thead>
<tbody>
<% #split_order_content.zip(#split_content).each do |number, string| %>
<tr>
<td><%= s.text_field :string_id_bk, :value => number %></td>
<td><%= s.text_field :string_id, :value => string %></td>
</tr>
<% end %>
</tbody>
</table>
<%= s.submit "Save String File", id: "commit", class: "btn btn-lg btn-primary" %>
HTML td rendered:
<td><input value="Ende"" type="text" name="skein[string_id]" id="skein_string_id"></td>
Notice how the name and id will be the same, causing the data to overwrite the data sending just the last data.
The table has thousands of records. I cannot send each td as an attribute parameter, a parameter should be a long array.
Try this
<% #split_order_content.zip(#split_content).each do |number, string| %>
<tr>
<td> <%= text_field_tag 'string_id_bk[]', number %> </td>
<td> <%= text_field_tag 'string_id []', string %> </td>
</tr>
<% end %>
Give unique id for the text fields as shown below
<% #split_order_content.zip(#split_content).each do |number, string| %>
<tr>
<td><%= s.text_field :string_id_bk, :value => number ,:input_html => { :id => '#{s.id}'+'bk' } %></td>
<td><%= s.text_field :string_id, :value => string ,:input_html => { :id => s.id }%></td>
</tr>
<% end %>
Hope that will help you

Two Column HTML table using Rails 3

I seriously searched for 3 days on this topic, but I am unable to find anything that can help me solve my issue.
I have a table that prints out Kanban cards for the factory I work in, but the Avery sheet where I need to print them is 2 col by 3 rows, and my table is displaying 3 col X 2 rows.
card 1|card 2|card 3
card 4|card 5|card 6
I need this layout to be 2 col by X rows.
card 1|card 2
card 3|card 4
card 5|card 6
This is my Rails view:
<% count = #card.start%>
<% #card.finish.times do %>
<table border="0" cellspacing="5" cellpadding="5" class="table">
<tr>
<tr>
<td id="part_no_card_no" class="all">
<%= #card.part_no %>
&nbsp
&nbsp
<%= count %> /
<%= #card.finish %>
</td>
</tr>
<tr>
<td>
<%= image_tag "#{#card.part_no}" + "-" + "#{count}" + "-" + "#{#card.finish}" + ".png" %>
</td>
</tr>
<tr>
<td>
<%= #card.description %>
</td>
</tr>
<tr>
<td>
<%= #card.from_loc %>
-
<%= #card.to_loc %>
</td>
</tr>
<tr>
<td>
Cantidad en Bin: <%= #card.bin_qty %> EA
</td>
</tr>
</td>
</tr>
</tr>
</table>
<% count += 1 %>
<% end %>
I hope someone could provide some direction on this task.
First off, I'm not quite understand about your card system and it's attribute, since you just show some simple layout but your view looks more advance about it. So I give you an example about my code, and I will edit whenever it's needed.
I have a table named user_menus and I want to show it like: ([] mean checkbox)
[] Products [] Suppliers
[] Categories [] Units
I use .in_groups_of() for it.
<table class="table table-condensed">
<% #user_menus.in_groups_of(2, false) do |user_menu_array| %>
<tr>
<% for user_menu in user_menu_array %>
<td>
<label class="checkbox">
<%= check_box_tag "user_group[user_menu_ids][]", user_menu.id, #user_group.user_menus.include?(user_menu), :checked => true %>
<%= user_menu.name %>
</label>
</td>
<% end %>
</tr>
<% end %>
</table>
With this code, I can produced that easily.
this is more of a HTML thing. To have two columns in a row, keep two 's inside a tag eg.
e.g for 2 columns
<tr>
<td>col-1</td>
<td>col-2</td>
</tr>
for 4 columns
<tr>
<td>col-1</td>
<td>col-2</td>
<td>col-3</td>
<td>col-4</td>
</tr>