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>
<% } %>
Related
I'm not sure how to word this exactly...but basically in my code I am looping through my MongoDB collection using forEach and displaying the objects within them with ejs in a table. I want to make it so that when a user clicks on one of those objects, it takes them to a page with details for just the item they selected, but can't do that because they are displayed via a loop.
For example:
My collection, called stores, has objects like environment, store, and version. When I display them with a loop, this is how I do it:
<table>
<tr>
<th>Environment</th>
<th>Store Name</th>
<th>Code Version</th>
</tr>
<% stores.forEach(function(store){ %>
<tr>
<td> <%= store.environment %> </td>
<td> <%= store.store %> </td>
<td> <%= store.version %> </td>
</tr>
<% }); %>
</table>
and the resulting table looks something like this:
Environment | Store Name | Code Version
-----------------------------------------
QA1 | Oakdale | 2019.09
QA2 | Westminster | 2020.03
QA3 | Garden Grove | 2020.05
I want the user to be able to click on, for example, QA1 and it will direct them to another page for details on QA1. My question is, with the code being displayed as a group via <%= store.environment %>, how do I create a link on JUST QA1, or just QA2? Right now if I put a link in the same td as <%= store.environment %>, that link shows up in every instance of environment.
Is there maybe a way through jQuery or something to select the specific environments?
Yes, you can, you'll just have to check the environment property, or whatever property you're using to define if should show the link.
Here's a quick example of how you could do that:
<table>
<tr>
<th>Environment</th>
<th>Store Name</th>
<th>Code Version</th>
</tr>
<% stores.forEach(function(store){ %>
<tr>
<td>
<% if (store.environment === "QA1") { %>
<%= store.environment %>
<% } else { %>
<%= store.environment %>
<% } %>
</td>
<td> <%= store.store %> </td>
<td> <%= store.version %> </td>
</tr>
<% }); %>
</table>
Sorry for any code mistake, I'm doing direct from StackOverflow.
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>
I'm currently scraping product information from a website and am attempting to output it into a table on a webpage.
I'm stuck because the only code that I can get to properly format as
ITEM
IMAGE
PRICE
Is clearly wrong, and ends up replicating the data many times.
The partially functioning code is:
<table>
<tr>
<% #items.each do |title| %>
<% #images.each do |image| %>
<% #prices.each do |price| %>
<br>
<td><%= title %></td>
<br>
<td><%= image %></td>
<br>
<td><%= price %></td></tr>
<br>
</table>
<% end %>
<% end %>
<% end %>
I've tried formatting the code like below, but it tends to drop the three sets of data down like
TITLE TITLE TITLE IMAGE IMAGE IMAGE PRICE PRICE PRICE.
in a variety of very incorrect ways.
<table>
<tr>
<td>
<% #items.each do |title| %>
<%= title %>
<% end %>
</td>
<br>
<td>
<% #images.each do |image| %>
<%= image %>
<% end %>
<br>
<td>
<% #prices.each do |price| %>
<%= price %>
<% end %>
</td>
<br>
</table>
Is anyone able to spot the mistake I'm making or know what I need to do to properly output this code? Do I need to show you any more code?
If you want to iterate over the three arrays together you need to zip them. This will enable you to render each triplet together (the first item with the first image and the first price; the second item with the second image and the second price; etc.):
<table>
<% #items.zip(#images, #prices).each do |title, image, price| %>
<tr>
<td><%= title %></td>
<td><%= image %></td>
<td><%= price %></td>
</tr>
<% end %>
</table>
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
I have a hash of images that i'd like to output via a html table.
It's a basic task that I've done in PHP but I'd like to see what magical stuff ruby can do to achieve it easily.
I've looked at helpers and tutorials and they're all very complicated.
Here's how I am currently printing the images.. How would I convert it to output to something like a 4 x n table?
<% #photos.each do |photo| %>
<%= image_tag photo["images"][4]["source"] %><br/>
<% end %>
Take a look at the each_slice method, which lets you grab your array in groups of a given size. You should be able to do something like
<table>
<% #photos.each_slice(4) do |group| %>
<tr>
<%group.each do |photo|%>
<td>
<%= image_tag photo["images"][4]["source"] %>
<td>
<% end %>
</tr>
<% end %>
</table>
I think in_groups_of might help you out:
<table>
<% #photos.in_groups_of(4, false) do |photos| %>
<tr>
<% photos.each do |photo| %>
<td><%= image_tag photo["images"][4]["source"] %></td>
<% end %>
</tr>
<% end %>
</table>
By passing false as the second argument, the last row might be incomplete. If you want to show a blank image instead, you can pass an empty photo object (assuming its URI points to the blank image your website is using) or simply the direct URI, if the photos array is only an array of URIs.