Why for loop is not working , but on individual data it works.
I'm trying to fetch 3 inputs from the api, book name , author name and subject.
<table class="table table-bordered">
<h1 align="center">List of Science Book Records</h1>
<thead>
<tr>
<th>Book Name</th>
<th>Author Name</th>
<th>Publish Date</th>
</tr>
</thead>
<tbody class="mypanel">
<script>
$.getJSON('https://openlibrary.org/subjects/science.json', function(data) {
for (var i=0;i<data.length;i++) {
console.log("hii");
var text = `<tr><td>${data.works[i].title}</td>
<td>${data.works[i].authors[i].name}</td>
<td>${data.name}</td></tr>`
$(".mypanel").html(text);
}
});
</script>
</tbody>
</table>
You should looping data.works instead. Check the following snippet.
I'm assuming that data.works[i].autors has only one position, if not, you can loop them as well.
Finally prepend to the div with class myPanel.
Hope it helps.
$.getJSON('https://openlibrary.org/subjects/science.json', function(data) {
for (var i=0;i<data.works.length;i++) {
var text = `<tr><td>${data.works[i].title}</td>
<td>${data.works[i].authors[0].name}</td>
<td>${data.name}</td></tr>`
$(".mypanel").append(text);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered">
<h1 align="center">List of Science Book Records</h1>
<thead>
<tr>
<th>Book Name</th>
<th>Author Name</th>
<th>Genre</th>
</tr>
</thead>
<tbody class="mypanel">
Judging from JSON structure that you have, you're iterating over the length of an object data which is undefined.
In order to show the list of works you need to iterate over the length of works so your loop will look like this
for (var i = 0; i < data.works.length; i++) {
// do stuff with data.works[i]
}
And also you can use append in each iteration of the loop OR you can summarize all text, and then append it after the loop, by doing
text += `<tr><td>${data.works[i].title}</td>
<td>${data.works[i].authors[i].name}</td>
<td>${data.name}</td></tr>`;
But don't forget to define variable text before the loop.
Related
I am trying to create a table of unique barcodes using node, bootstrap, and ejs. Here are the contents of my .ejs file:
<head>
<script src="https://cdn.jsdelivr.net/npm/jsbarcode#3.11.4/dist/JsBarcode.all.min.js"</script>
</head>
<body>
<table>
<thead>
<tr>
<th scope="col">Employee</th>
</tr>
</thead>
<tbody>
<% employeeObj.forEach(function(obj) { %>
<tr>
<td><%= obj.id %><svg id="barcode"></svg><script>var employee = '<%= obj.id %>'; JsBarcode("#barcode", employee);</script></td>
</tr>
<% }); %>
</tbody>
</table>
</body>
</html>
For some reason in each row of the table the barcode is being generated, but every barcode is the exact same value. I tried just putting the string value of obj.id in each row and it displays the different values fine.
What am I doing wrong?
I realized my issue was that the id for every barcode was the same which resulted in the same barcode being display multiple times. The solution to this problem is to have a unique id which could be accomplished like this:
id="<%=obj.id%>"
I am bringing data from my mongoose to display on my HTML table. I am able to filter a JSON object normally but unable to filter nested JSON object?
I have used "npm i ng2-search-filter --save " to import filter pipe, but this only works for the first level. Doesn't do filter for the nested JSON object.
My JSON object is:
{
packageName:test,
packagePrice:200,
userid:{
userName:John,
userAge: 27
}
}
<input type="text" class="form-control" [(ngModel)]="searchtext"
placeholder="Search">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Package Name</th>
<th scope="col">Package Price</th>
<th scope="col">Customer Name</th>
<th scope="col">Customer Age</th>
</tr>
</thead>
<tbody *ngFor="let use of user | filter:searchtext">
<tr class="table-active">
<td> {{use.packageName}}</td>
<td>{{use.packagePrice}}</td>
<td>{{use.userid.userName}}</td>
<td>{{use.userid.userAge}}</td>
</tr>
</tbody>
</table>
When I enter packageName and packagPrice in a textbox for search filter it filters out and shows me correct result, but on userName and userAge its not working.
Please help.
Thank you.
I solved this with by just installing the latest version ng2 search filter.
Run below command:
npm i ng2-search-filter#0.5.1
You can use custom search pipe for this
I have create demo on Stackblitz
searchPipe.ts
transform(value: any, searchText?: any): any {
if(!value) return [];
if(!searchText) return value;
searchText = searchText.toLowerCase();
return value.filter( it => {
return it.packageName.toLowerCase().includes(searchText) || it.packagePrice.toString().includes(searchText) || (it.userid && it.userid.userAge.toString().includes(searchText) || it.userid.userName.toLowerCase().includes(searchText));
});
}
component.html
<tr class="table-active" *ngFor="let use of user | search:searchtext">
<td> {{use.packageName}}</td>
<td>{{use.packagePrice}}</td>
<td>{{use.userid.userName}}</td>
<td>{{use.userid.userAge}}</td>
</tr>
Try the below:-
function getResult(filterBy, objList) {
return objList.hightlights.filter(function(obj) {
return obj.queries.some(function(item){
return item.indexOf(filterBy) >= 0;
});
});
}
I tried to bind dynamically partial view its working but for jquery
datatable against first row what I specified below its not working.
#model IEnumerable<PurModelClass>
<table id="stock-audit-table" class="table table-striped table-bordered dt-responsive nowrap">
<thead>
<tr>
#*<th>Purchase From</th>
<th>Purchase Date</th>*#
<th>Item Name</th>
<th>Qty</th>
<th>Unit</th>
<th>Cost</th>
<th>Net Amount</th>
<th>Discount</th>
<th>Vat</th>
<th>Gross</th>
</tr>
</thead>
<tbody id="stock-audit-tbody">
#foreach (var item in Model)
{
<tr><td colspan="8">Invoice No: #item.PurInvNo Inv Date: #($"{#item.PurDate:MMM-dd-yyyy}") Vendor: #item.VendorName </td></tr>
foreach (var detail in item.PurDetails)
{
<tr>
<td>#detail.StockName</td>
<td>#detail.PurQty</td>
<td>#detail.UnitName</td>
<td>#detail.UnitPrice</td>
<td>#detail.Total</td>
<td>#detail.Discount</td>
<td>#detail.VAT</td>
<td>#detail.NetAmount</td>
</tr>
}
}
</tbody>
<tfoot>
</tfoot>
</table>
This the datatable script, is there any way to implement jquery datatable by demanding specific row with colspan properties.
<script type="text/javascript">
$(document)
.ready(function() {
$('#stock-audit-table').DataTable({
"autoWidth": false,
"order": [[0, "desc"]]
});
}
);
</script>
Try the rowGroup extension. You can use startRender and endRender to customize the summary data
$('#stock-audit-table').DataTable({
"autoWidth": false,
"order": [[0, "desc"]],
"rowGroup": {
dataSrc: 0,
startRender: function(groupName){
return $('<tr/>').append("<td colspan="8">TODO put data here</td>");
}
}
});
<tbody id="stock-audit-tbody">
#foreach (var item in Model)
{
foreach (var detail in item.PurDetails)
{
<tr>
<td>
<!-- Define a key to be referenced by the
'datasrc' property in dataTables -->
#item.GroupName
</td>
<td>#detail.StockName</td>
<td>#detail.PurQty</td>
<td>#detail.UnitName</td>
<td>#detail.UnitPrice</td>
<td>#detail.Total</td>
<td>#detail.Discount</td>
<td>#detail.VAT</td>
<td>#detail.NetAmount</td>
</tr>
}
}
</tbody>
I have a list of objects. Every Object has a name with a specific prefix. Now I want to replace this prefix with an empty String.
My Code:
Controller:
#Secured("ROLE_ADMIN")
#RequestMapping(value = "/admin/userManagement", method = RequestMethod.GET)
public String showUserManagement(Model model) {
model.addAttribute("userList", userDelegate.getAllUserForClient(getCompanyAuthority().getName()));
model.addAttribute("companyName", getCompanyAuthority().getName());
return "admin/user_management";
}
My html snippet:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>E-mail</th>
<th th:text="#{adminArea.userManagementReport}">Reports</th>
<th th:text="#{adminArea.userManagementRole}"></th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr th:each="user,itrStat : ${userList}">
<td th:text="${itrStat.index}"></td>
<td th:text="${user.username}"></td>
<td>
<small th:each="report : ${user.reports}"
th:utext="${#strings.replace(report.name,companyName + '_','')} : ${companyName}"></small>
<br th:each="report : ${user.reports}"></br></td>
</tr>
</tbody>
</table>
the Important part is:
<small th:each="report : ${user.rep orts}"
th:utext="${#strings.replace(report.name,companyName + '_','')} : ${companyName}"></small>
For every report name, I want to remove the companyName prefix. So TEST_filename.txt -> filename.txt
Can someone help me?
Use substringAfter:
<small th:each="report : ${user.reports}"
th:utext="${#strings.substringAfter(report.name,'_'}></small>
Of course, you'll want to make sure that your company names don't also contain the underscore delimiter before doing this.
don’t do that ,try this instead ,
to split the String based on "_" and take the second word by mentioning the index .
Thanks in advance for taking the time to try to help. I am using the Jade templating engine in node.js, and want to generate an HTML table with rows grouped into sections (tbody tags).
Let's say I have the following object in memory:
[
{ type: 'Fruit', name: 'Apple' }
, { type: 'Fruit', name: 'Orange' }
, { type: 'Vegetable', name: 'Carrot' }
, { type: 'Vegetable', name: 'Spinach'}
]
(for simplicity, let's assume the array comes pre-ordered by the "type" column).
And I want to generate a table with rows for each object inside a tbody section for each "type" (Fruit vs. Vegetable). So the HTML I'm trying to generate is:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit">
<tr>
<td>Fruit</td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit</td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable">
<tr>
<td>Vegetable</td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable</td>
<td>Spinach</td>
</tr>
</tbody>
</table>
I think I would want my jade to look something like:
table
thead
th Type
th Name
- var lastType;
each o in objs
- if(o.type != lastType)
tbody(id='#{o.type}')
- lastType = o.type;
tr
td #{o.type}
td #{o.name}
But this generates:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit" />
<tbody>
<tr>
<td>Fruit</td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit</td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable" />
<tbody>
<tr>
<td>Vegetable</td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable</td>
<td>Spinach</td>
</tr>
</tbody>
</table>
Any ideas?
This is in Jade kinda hard to guess, because we don't really see how your indentations are really set in your local code, but here(your posted snippet) it seems like your indentations are not right.
You should try this: +Update (added another loop to get all same types under each body)
table
thead
th Type
th Name
- var lastType;
each o in objs
- if (o.type != lastType)
- lastType = o.type;
tbody(id='#{o.type}')
each t in objs
- if(t.type === lastType)
tr
td #{t.type}
td #{t.name}
Just shift the both tr tags into your tbody, like my code example above.
+Update This will produce this HTML code now:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit">
<tr>
<td>Fruit </td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit </td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable">
<tr>
<td>Vegetable </td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable </td>
<td>Spinach</td>
</tr>
</tbody>
</table>
table
thead
th Type
th Name
tbody#Fruit
tbody
tr
td Fruit
td Apple
tr
td Fruit
td Orange
tbody#Vegetable
tbody
tr
td Vegetable
td Carrot
tr
td Vegetable
td Spinach
You should go with pug basic formate to list the following process