After initialize ejs variable, i cant change value and increment operator not working.
Here is my code:
<% results1.forEach(function(result1, index){ %>
<% results2.forEach(function(result2, index1){ %>
<% if(result1.id==result2.parent)
{
var i=1; %>
<tr>
<td><%= index+1 %>.<%= i %></td>
<td><%= result2.title %></td>
<td><%= result1.title %></td>
<td align="center"><%= result2.views %></td>
</tr>
<% i++;
} %>
<% }); %>
<% }); %>
In the above code I declared a variable i and bottom I added i++.
i should be declared outside forEach statement, otherwise is redeclared at every cicle
correct the code as above
<% results1.forEach(function(result1, index){ %>
<% var i=1; results2.forEach(function(result2, index1){ %>
<% if(result1.id==result2.parent)
{
<tr>
<td><%= index+1 %>.<%= i %></td>
<td><%= result2.title %></td>
<td><%= result1.title %></td>
<td align="center"><%= result2.views %></td>
</tr>
<% i++;
} %>
<% }); %>
<% }); %>
Related
The issue is on the 3rd row below. With in the href""'s, there is text and an a js script syntax, <%=. for some reason, the <%= is not being recognised as a script insert.
Any idea's on why this is happening and how to fix? I'm using node.js, Express, MongoDB, Mongoose
<% if(users.length > 0) { %>
<% users.forEach(function(user){ %>
<a href="/getUsers/<%= user._id %>">
<tr>
<td><%= user.userId %></td>
<td><%= user.userName %></td>
<td><%= user.userOperator %></td>
<td><%= user.userEmail %></td>
<td><%= user.userMobile %></td>
<td><%= user.userAdminYN %></td>
</tr>
</a>
<% }) %>
<% }else{ %>
I tried adding the script syntax in, and was expecting to see it change colour as it does elsewhere in the core.
Use a template string:
<a href="<%= `/getUsers/${user._id}` %>">
i am a student trying to run this code on the ide lab environment, but it keeps showing me an error, but i can't find where i went wrong. It keeps showing me the same error, i have read through the whole code but still don't see any missing syntax.
error message
Here's the ejs that causes errors. What is the problem with this code?
<meta charset="UTF-8">
<label for="heat">Temperature related devices</label>
<table id="heat">
<tr>
<td>ID</td>
<td>Type</td>
<td>Status</td>
<td>State</td>
<td>Temperature</td>
<td>Select this device</td>
<td>Delete this device</td>
</tr>
<% myDevices.forEach(function(item) { %>
<% if(field.name == "Heater" || field.name == "Dishwasher" || field.name == "Refrigerator - Freezer" || field.name == "Oven" || field.name == "Microwave Oven" || field.name == "Steamer Oven") { %> <tr>
<td><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.status %></td>
<td><%= item.state %></td>
<td><%= item.temperature %></td>
<td>Edit this device</td>
<td>Delete this device</td>
</tr>
<% } %>
<% }) %>
</table>
<br><br>
<label for="volume">Sound related devices</label>
<table id="volume">
<tr>
<td>ID</td>
<td>Type</td>
<td>Status</td>
<td>State</td>
<td>Volume</td>
<td>Select this device</td>
<td>Delete this device</td>
</tr>
<tr>
<% myDevices.forEach(function(item) { %>
<% } else if(field.name == "Music Player" || field.name == "Television" || field.name == "Radio") { %> <tr>
<td><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.status %></td>
<td><%= item.state %></td>
<td><%= item.volume %></td>
<td>Edit this device</td>
<td>Delete this device</td>
</tr>
<% } %>
<% }) %>
</tr>
</table>
<br><br>
<label for="speed">Speed related devices</label>
<table id="speed">
<tr>
<td>ID</td>
<td>Type</td>
<td>Status</td>
<td>State</td>
<td>Speed</td>
<td>Select this device</td>
<td>Delete this device</td>
</tr>
<tr>
<% myDevices.forEach(function(item) { %>
<% } else if(field.name == "Hob" || field.name == "Fan") { %> <tr>
<td><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.status %></td>
<td><%= item.state %></td>
<td><%= item.speed %></td>
<td>Edit this device</td>
<td>Delete this device</td>
</tr>
<% } %>
<% }) %>
</tr>
</table>
<br><br>
<label for="speedTemperature">Speed & Temperature related devices</label>
<table id="speedTemperature">
<tr>
<td>ID</td>
<td>Type</td>
<td>Status</td>
<td>State</td>
<td>Speed</td>
<td>Temperature</td>
<td>Select this device</td>
<td>Delete this device</td>
</tr>
<tr>
<% myDevices.forEach(function(item) { %>
<% } else if(field.name == "Air-Conditioner" || field.name == "Heat Pump Tumble Dryer" || field.name == "Washing Machine") { %> <tr>
<td><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.status %></td>
<td><%= item.state %></td>
<td><%= item.speed %></td>
<td><%= item.temperature %></td>
<td>Edit this device</td>
<td>Delete this device</td>
</tr>
<% } %>
<% }) %>
</tr>`enter code here`
</table>
<br><br>
<label for="intensity">Intensity related devices</label>
<table id="intensity">
<tr>
<td>ID</td>
<td>Type</td>
<td>Status</td>
<td>State</td>
<td>Intensity</td>
<td>Select this device</td>
<td>Delete this device</td>
</tr>
<tr>
<% myDevices.forEach(function(item) { %>
<% } else if(field.name == "Air-Conditioner" || field.name == "Heat Pump Tumble Dryer" || field.name == "Washing Machine") { %> <tr>
<td><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.status %></td>
<td><%= item.state %></td>
<td><%= item.intensity %></td>
<td>Edit this device</td>
<td>Delete this device</td>
</tr>
<% } %>
<% }) %>
</tr>
</table>
<br><br>
<label for="NA">Other devices</label>
<table id="NA">
<tr>
<td>ID</td>
<td>Device type</td>
<td>Device status</td>
<td>Device state</td>
<td>Select this device</td>
<td>Delete this device</td>
</tr>
<tr>
<% myDevices.forEach(function(item) { %>
<% } else if(field.name == "Door" || field.name == " Blinds" || field.name == "Curtains" || field.name == " Automated Laundry Rack ") { %>
<tr>
<td><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.status %></td>
<td><%= item.state %></td>
<td>Edit this device</td>
<td>Delete this device</td>
</tr>
<% } %>
<% }) %>
</tr>
</table>
starting at L40 there's <% } else if( throughout:
<% myDevices.forEach(function(item) { %>
<% } else if(field.name == "Music Player" || field.name == "Television" || field.name == "Radio") { %>
First loop doesn't have it, i.e. has only a single if so I guess removing } else should do it
looks like you are missing an extra - at the end of the include statement
<%- include("partials/deviceProperties"); -%>
reference: have a look at "Layouts" here https://ejs.co
I'm trying to get selected rows from a table in express js.
Here is a html+ejs table code I have in my ejs view file. I'm trying to get only the rows where the checkbox is checked.
<table class="table">
<thread class = "thread-inverse">
<tr>
<th>Item Id</th>
<th>Name</th>
<th>Category</th>
<th>Manufacturer</th>
<th>Supplier</th>
<th>Buying Price</th>
<th>Selling Price</th>
<th>Action</th>
</tr>
<% for(var i =0; i < itemList.length; i++){%>
<tr>
<td><%= itemList[i].productId %></td>
<td><%= itemList[i].itemName %></td>
<td><%= itemList[i].category %></td>
<td><%= itemList[i].manufacturer %></td>
<td><%= itemList[i].supplier %></td>
<td><%= itemList[i].buyingPrice %></td>
<td><%= itemList[i].sellingPrice %></td>
<td><input type="text" name="quantity""></td>
<td><input type="checkbox" name="productId" value="<%= itemList[i].productId %>"> | ADD </td>
</tr>
<% } %>
</thread>
</table>
I have tried several things but nothing is working..
Can anyone please give me a solution ?
Modify your code a bit,
//HTML
//add a **onclick** listener on checkbox
<td><input type="checkbox" class="checkbox" name="productId" value="<%= itemList[i].productId %>"> | ADD </td>
//In java script file
$('.checkbox').click(function() {
var productId = $(this).closest("tr").find('td:eq(0)').text();
//changing td:eq(x) will give you corresponding clicked row's cell
}
I tried with the following way, and works well.
Modify the code snippet by adding class name to :
<% for(var i =0; i < itemList.length; i++){%>
<tr class="tbl-item">
...
<% } %>
And implement the click function:
$(".checkbox").each(function(index) {
$(this).click(function(e) {
let item = $(".tbl-item")[index];
console.log(item);
}) ;
})
Then the console will show the desired tag data and you can get every item like:
item.children[4].innerText
Thanks.
I am unable to debug my post submitted data in browser like we do in PHP function:
print_r($_POST);
I'm going to assume you don't need a pretty solution (I think you'd use this for anything other than debugging). Then you can simply do this:
Webforms
<h1>Posted values</h1>
<table>
<% foreach (string key in Request.Form.Keys) { %>
<tr>
<td><%= key %></td>
<td><%= Request.Form[key] %></td>
</tr>
<% } %>
</table>
MVC (put in view file)
<h1>Posted values</h1>
<table>
#foreach (var key in Request.Form.AllKeys) {
<tr>
<td>#key</td>
<td>#Request.Form[key] %></td>
</tr>
}
</table>
I hope that helps.
I have codes in EJS below,
<table>
<% for(var i=0; i < data.length; i++) { %>
<tr>
<td><%= data[i].id %></td>
<td><%= data[i].name %></td>
</tr>
<% } %>
</table>
My data is like this:
[{"id":1,"name":"bob",description:[{"name":"carol","url":"www.hp.com"},{"name":"katy","url":"www.desktop.com"}]},{"id":2,"name":"john"description:[{"name":"raya","url":"www.usus.com"},{"name":"fat","url":"www.mia.com"}]}]"
I can manipulate the row to populate the table in JS => id and name but i cannot do that to description: name , url
This will output the following table (using the example data from above):
<table>
<tr>
<td>1</td>
<td>bob</td>
<td>name</td>
<td>url</td>
</tr>
<tr>
<td>2</td>
<td>john</td>
</tr>
<tr>
<td>3</td>
<td>jake</td>
<td>name</td>
<td>url</td>
</tr>
</table>
Helps appreciated.
Regards
This is because the "description" is an array. So you can use a nested loop to access its data:
<table>
<% for(var i=0; i < data.length; i++) { %>
<tr>
<td><%= data[i].id %></td>
<td><%= data[i].name %></td>
<td><% for(var j=0; j < data[i].description.length; j++) { %>
<p><%=data[i].description[j].name%></p>
<% } %></td>
</tr>
<% } %>
</table>