I am using underscore template to fetch and display values from model.
This below code working fine.
<% for (var i = 1; i <= data.total; i++) { %>
<td>List <%= i %> </td>
<% } %>
for the same i need to get the dynamic values which is stored in model like {List1,List2,List3 etc..} and need to display it in template.
for that i tried
1. <% for (var i = 1; i <= data.total; i++) { %>
<td><%- data.List<%= i %> %></td>
<% } %>
2. <% for (var i = 1; i <= data.total; i++) { %>
<td><%- data.List${i} %></td>
<% } %>
where data is model object and List1,List2..are its values.
Both the above codes are not working.
I had just checked freemaker template which is supporting this kind of operation.
Freemaker Template for loop
Can we get same type or any other approach to achieve this?
The interpolated parts of an Underscore template are just JavaScript expressions so you'd do it exactly the same way you'd do it normal JavaScript code (i.e. using [] and some string manipulation to build the keys):
<td><%- data['List' + i] %></td>
Related
I'm using mongoose and express, among other things. I can display the top level values, like "number", but i can't get "contact.email". I've tried a lot of options in my server.js file, but I'm hoping someone can help me extract this info from the json on the ejs side. Thanks!
I'm using this code in my ejs template:
`<% for(var i= 0 ; i < myObj.length; i++) {%>
<li>
<span><%=myObj[i].number %>
</li>
<% } %>
</ul>`
the schema
"_id" : ObjectId("5e67c84a65a2893029991863"),
"number" : 5,
"contact" : [
{
"_id" : ObjectId("5e69a33a0208203268813e01"),
"email" : "dhenley#gmail.com",
}
]
}`
You can try this below since you need another loop for accessing the contact array in your json:
<% for(var i= 0 ; i < myObj.length; i++) {%>
<li>
<span><%=myObj[i].number %>
</li>
<ul>
for(var j=0 ; j < myObj[i].contact.length; j++)
<li>
<span><%=myObj[i].contact[j].email %>
</li>
<% } %>
</ul>
<% } %>
</ul>`
By Assuming that your object format is as below:-
var myObj= {
"_id" : ObjectId("5e67c84a65a2893029991863"),
"number" : 5,
"contact" : [{
"_id": ObjectId("5e69a33a0208203268813e01"),
"email": "dhenley#gmail.com"
}]
};
You have to loop your data like as below:-
<span><%=myObj.number %></span>
<ul>
<% for(var i= 0 ; i < myObj.contact.length; i++) {%>
<li><%=myObj.contact[i].email %></li>
<% } %>
</ul>`
Note:- As myObj is an object. and myObj.contact is an array of myObj. so in this case you have to loop your myObj.contact.
When I try to change page using pagination bar in an entity generated by Jhipster, get the following javascript error: $state is not defined
I am evaluating the JHipster version 4.0.5 framework with angularJS version 1 to clientFramework. All entities were generated through the command jhipster:entity entity-name using pagination option.
The problem is on the entitycontroller.js generated 'state' variable is missing on the controller injection. e.g.
EntityController.$inject = ['Entity', 'ParseLinks', 'AlertService', 'paginationConstants','$state']
Can you solve this issue for the next release?
You have to modify _entity-management.controller.js
This file is located on the directory:
../node_modules/generator-jhipster/generators/entity/templates/client/angularjs/src/main/webapp/app/entities/_entity-management.controller.js
You need to add $state variable on the $inject and function code. Here you have, the changed code for jhipster 4.0.5 release:
(function() {
'use strict';
angular
.module('<%=angularAppName%>')
.controller('<%= entityAngularName %>Controller', <%= entityAngularName %>Controller);
<%= entityAngularName %>Controller.$inject = [<% if (fieldsContainBlob) { %>'DataUtils', <% } %>'<%= entityClass %>'<% if (searchEngine == 'elasticsearch') { %>, '<%= entityClass %>Search'<% } %><% if (pagination != 'no') { %>, 'ParseLinks', 'AlertService', 'paginationConstants', '$state'<% } %> <%_ if (pagination == 'pager' || pagination == 'pagination'){ %>, 'pagingParams'<% } %>];
function <%= entityAngularName %>Controller(<% if (fieldsContainBlob) { %>DataUtils, <% } %><%= entityClass %><% if (searchEngine == 'elasticsearch') { %>, <%= entityClass %>Search<% } %><% if (pagination != 'no') { %>, ParseLinks, AlertService, paginationConstants, $state<% } %> <%_ if (pagination == 'pager' || pagination == 'pagination'){ %>, pagingParams<% } %>) {
var vm = this;
<%_ if (pagination == 'pagination' || pagination == 'pager') { _%>
<%- include('pagination-template'); -%>
<%_ } else if (pagination == 'infinite-scroll') { _%>
<%- include('infinite-scroll-template'); -%>
<%_ } else { _%>
<%- include('no-pagination-template'); -%>
<%_ } _%>
}
})();
I hope it is useful.
I have an idea to create a navigation bar with Arrays and looping it with While.
first, I make a variable on my jsp file like this:
<%
int i, j;
i = 0;
j = 0;
String[][] NavMenu = new String[6][5];
NavMenu[0][0] = "Company Profile";
NavMenu[1][0] = "Product";
NavMenu[2][0] = "Marketing Plan";
NavMenu[3][0] = "News";
NavMenu[3][1] = "Event";
NavMenu[3][2] = "Promo";
NavMenu[3][3] = "Artikel";
NavMenu[3][4] = "Testimony";
NavMenu[4][0] = "Merchandise";
NavMenu[5][0] = "Job Vacancies";
String[][] NavLink = new String[6][5];
NavLink[0][0] = "company-profile-infiny-niaga-abadi.html";
NavLink[1][0] = "product.html";
NavLink[2][0] = "marketing-plan.html";
NavLink[3][0] = "#";
NavLink[3][1] = "news/event.html";
NavLink[3][2] = "news/promo.html";
NavLink[3][3] = "news/article.html";
NavLink[3][4] = "news/testimony.html";
NavLink[4][0] = "merchandise.html";
NavLink[5][0] = "job-vacancies.html";
%>
The NavMenu is a text for Navbar menu, and NavLink is a link for Navbar menu.
and then, I make my navbar like this:
<%
while (NavMenu[i][0]) { //ERROR ON HERE: cannot convert from String to boolean
j = 1;
//IF MENU HAVE A DROPDOWN
if(NavMenu[i][1]){ //ERROR ON HERE: cannot convert from String to boolean
if(NavMenu[i][0] == ActivePage) { %>
<li class="active dropdown">
<% } else { %>
<li class="dropdown">
<% } %>
<%=NavMenu[i][0]%> <b class="caret"></b>
<ul class="dropdown-menu">
<%
while(NavMenu[i][j]) { //ERROR ON HERE: cannot convert from String to boolean %>
<li>
<%= NavMenu[i][j]%>
</li>
<% j++;
} %>
</ul>
</li>
<%
//IF MENU DO NOT HAVE A DROPDOWN
} else {
if(NavMenu[i][0] == ActivePage){ %>
<li class="active"><%= NavMenu[i][0] %></li>
<% } else {%>
<li><%= NavMenu[i][0] %></li>
<%}
}
i++;
j=0;
}
%>
How to solve this Problem? I have no idea what should I do to make this working.
Java is not like javascript, you can not use string as condition, In java the condition must be in boolean. you can use a for loop to iterate over the String array and do your logic
<%
String[][] NavMenu = new String[6][5];
NavMenu[0][0] = "Company Profile";
NavMenu[1][0] = "Product";
NavMenu[2][0] = "Marketing Plan";
NavMenu[3][0] = "News";
NavMenu[3][1] = "Event";
NavMenu[3][2] = "Promo";
NavMenu[3][3] = "Artikel";
NavMenu[3][4] = "Testimony";
NavMenu[4][0] = "Merchandise";
NavMenu[5][0] = "Job Vacancies";
String[][] NavLink = new String[6][5];
NavLink[0][0] = "company-profile-infiny-niaga-abadi.html";
NavLink[1][0] = "product.html";
NavLink[2][0] = "marketing-plan.html";
NavLink[3][0] = "#";
NavLink[3][1] = "news/event.html";
NavLink[3][2] = "news/promo.html";
NavLink[3][3] = "news/article.html";
NavLink[3][4] = "news/testimony.html";
NavLink[4][0] = "merchandise.html";
NavLink[5][0] = "job-vacancies.html";
%>
<%
for (int i=0; i<NavMenu.length; i++) { //ERROR ON HERE: cannot convert from String to boolean
String navMenuValue = NavMenu[i][0];
//IF MENU HAVE A DROPDOWN
if(NavMenu[i].length > 1){ //ERROR ON HERE: cannot convert from String to boolean
if(NavMenu[i][0].equals("ActivePage")) { %>
<li class="active dropdown">
<% } else { %>
<li class="dropdown">
<% } %>
<%=NavMenu[i][0]%> <b class="caret"></b>
<ul class="dropdown-menu">
<%
for(int j=0;j<NavMenu[i].length;j++) { //ERROR ON HERE: cannot convert from String to boolean %>
<li>
<%= NavMenu[i][j]%>
</li>
<%
} %>
</ul>
</li>
<%
//IF MENU DO NOT HAVE A DROPDOWN
} else {
if(NavMenu[i][0].equals("ActivePage")){ %>
<li class="active"><%= NavMenu[i][0] %></li>
<% } else {%>
<li><%= NavMenu[i][0] %></li>
<%}
}
}
%>
Guys, it's my first question, :). I'm trying to do as the title says. Here is my code:
in Ruby :
<% _points = Array.new %>
<% #users.each do |user| %>
<% _points.push([user.latitude, user.longitude]) %>
<% end %>
I wanna use _points in a javascript fonction so i do it:
<script>
var points = new Array();
points = <%=raw _points%>;
for (x in points) {
setInterval(function () {
var b = new R.BezierAnim([points[x], citylatlon], {}, function () {
.......................
}, 4000);
}
</script>
finally, the funtction just can worked for le last array in ruby, help! Thx Guys.
i found the way!
var points = <%=_points.to_json%> ;
$(document).ready(function () {
points.forEach(function (x, index, arr) {
animation_voyage(x);
}
)
}
);
I made a dummy data for my application using this method in my controller:
$scope.owners = [];
for (var i = 1; i <= 14; i++) {
$scope.owners.push({
id: i,
systemCode: '00' + (1284538+i),
systemName: 'Desktop',
description: 'Lorem ipsum dolor',
operational: '2973238',
business: '5621253',
manager: 'Dr. Smith',
status: 'Active'
});
}
and adding this function to perform delete request:
$scope.rowDelete = function(row){
$scope.owners.splice((this.owner.id - 1), 1);
}
and a page filter to use for pagination:
MyApp.filter('page', function () {
return function (list, page, size) {
var start = 0, end = 0;
//console.log('page filter...', page, size);
page = page || 1;
if (angular.isArray(list)) {
end = list.length;
if (page && Number(page) && size && Number(size)) {
page = Number(page);
size = Number(size);
start = (page - 1) * size;
end = start + size;
}
return Array.prototype.slice.call(list, start, end);
}
return null;
}
});
In the HTML template, I'm using ng-repeat and the custom filter:
<tr data-ng-repeat="owner in owners | page:$stateParams.page:2">
<td class="table-cell-clean">
<i class="mg-icon-delete"></i>
</td>
<td>{{ owner.systemCode }}</td>
<td>{{ owner.systemName }}</td>
<td>{{ owner.description }}</td>
<td>{{ owner.operational }}</td>
<td>{{ owner.business }}</td>
<td>{{ owner.manager }}</td>
<td class="align-center">{{ owner.status }}</td>
Whenever I click the delete button, rowDelete() is called as expected, the row is deleted, but as soon as it's leaving the DOM, a new item from the next page is inserted into the current table.
What might go wrong?
That would be as expected. Your owners array is being modified which requires the ng-repeat expression to be re-evaluated which includes the page filter expression as apart of it. So owners is modified by deleting a row, ng-repeat detects the change and re-evaluates the repeat expression which before it can iterate over the array it runs owners through the page filter. So n items per page, delete leaves n-1, and page filters the owners array to return n items.
All normal behavior.