How to render jade layout with loop of users - node.js

I am trying to render something like it:
<div class="row">
<div class"test">
User 1
</div>
<div class"test">
User 2
</div>
<div class"test">
User 3
</div>
</div>
<div class="row">
<div class"test">
User 4
</div>
<div class"test">
User 5
</div>
<div class"test">
User 6
</div>
</div>
I need 3 .test for each .row:
- var count = 0
div.row
for user in Users
- count++
-if ((count % 3) == 0)
div.test
| #{user.name}
div.row
-else
div.test
| #{user.name}
But, it does not working because when the loop try to render "div.row" its automatically close the tag.
The wrong result is:
<div class="row"></div>
<div class="test">User 1</div>
<div class="test">User 2</div>
<div class="test">User 3</div>
<div class="row"></div>
<div class="test">User 4</div>
<div class="test">User 5</div>
<div class="test">User 6</div>
<div class="row"></div>

One of possible ways is to reorganize your Users array before you send it to jade.
Users = [[user1,user2,user3],[user4,user5,user6],...
And then in jade
for group in Users
div.row
for user in group
div.test
| #{user.name}

var test = [{'1':'a','t':0},{'2':'b','t':0},{'3':'c','t':0},{'4':'d','t':0},{'5':'e','t':0},{'6':'f','t':0},{'8':'h','t':0},{'9':'i','t':0}];
//console.log(test)
var i=1;
var j=1;
_.each(test,function(t){
t.g = j;
if((i % 3) == 0){
j++;
}
i++;
return(t);
});
var test2 = _.groupBy(test,function(t){return t.g});
console.log(test2);

Related

Apply style to all nested li div's excluding the first li div

No matter what I've tried to either target or exclude the first li div, nothing works. I tried :not(and different variations of the selectors here) but that didn't work. On one occasion, the result rendered as though looking into a mirror that's facing a mirror. No bueno. No classes or ids can be added--representative example below.
ul {
list-style-type: none;
}
.message-content-wrap {
background-color: red;
}
<ul id="thread-list" class="group-message-thread">
<li>
<div class="message-wrap group-messages-highlight">
<div class="avatar-wrap"></div>
<div class="message-content">
<div class="message-content-wrap">
<p>This is content in the first message</p>
</div>
</div>
</div>
</li>
<li>
<div class="message-wrap">
<div class="avatar-wrap"></div>
<div class="message-content">
<div class="message-content-wrap">
<p>This is content in the second message</p>
</div>
</div>
</div>
</li>
<li>
<div class="message-wrap">
<div class="avatar-wrap"></div>
<div class="message-content">
<div class="message-content-wrap">
<p>This is content in the third message</p>
<p>some text</p>
</div>
</div>
</div>
</li>
<li>
<div class="message-wrap">
<div class="avatar-wrap"></div>
<div class="message-content">
<div class="message-content-wrap">
<p>This is content in the fourth message</p>
<p>some more text</p>
</div>
</div>
</div>
</li>
</ul>
Any ideas would be most welcome. My attempt in jsfiddle
a few ways to style child elements
ul > li:not(first-of-type) {
// styles go here
}
ul > li {
// styles go here
}
ul:first-child {
}
li + li {
}

Angular hide div on click function

i am new to angular and typescript and i need to hide div section *ngIf="stu.notes != null" on save button click.
i can display the respective divs when clicked on the respective buttons, but i am not able to hide the other one.
Can anyone hep me out here.
Thanks in advance.
HTML File:
<nb-accordion-item-body>
<div class="row">
<div *ngIf="notes_area != null">
<h5> <span class="col-md-8" style="font-weight : bold; font-size : 16px;"
[innerHTML]="notes_area"></span>
</h5>
<br/>
</div>
<div class="col-md-3" *ngFor = "let stu of studentData">
<div *ngIf="stu.notes != null" >
<div class="row">
<div class="col-md-8">
<br/>
<h5 style="font-weight : bold;" > {{stu?.notes.slice(3,-4)}}
</h5>
<br/>
</div>
</div>
</div>
</div>
</div>
<br/>
<div class="row" *ngIf="addNote == true">
<div class="col-md-6">
<tinymce [(ngModel)] = "notes" ></tinymce>
</div>
<div class="col-md-6">
<button nbButton (click)="saveNotes()">Save</button>
</div>
</div>
</nb-accordion-item-body>
.ts File:
saveNotes() {
this.api.saveNotes(this.my_app_data[0].app_id,this.notes).subscribe(data => {
this.notes_area = data['data'];
console.log(this.app_id);
console.log('log',this.notes);
});
}

Links blog post in Orchard CMS?

I have a list of blog posts in Orchard CMS. This is my code:
#{
IEnumerable<object>
blogPosts = Model.ContentItems;
Model.ContentItems.Classes.Add("content-items");
Model.ContentItems.Classes.Add("blog-posts");
}
<div class="row">
#foreach (var item in blogPosts)
{
<div class="col-md-4 col-sm-6">
<div>
<div class="blog-seg post-item seg">
<div>#Display(item)</div>
</div>
</div>
</div>
}
</div>
I want each DIV to have a link to the post. How can I do this?
Use the following to display links for content items:
#using Orchard.ContentManagement;
#{
IEnumerable<dynamic>
blogPosts = Model.ContentItems.ContentItems;
Model.ContentItems.Classes.Add("content-items");
Model.ContentItems.Classes.Add("blog-posts");
}
<div class="row">
#foreach (var item in blogPosts) {
<div class="col-md-4 col-sm-6">
<div>
<div class="blog-seg post-item seg">
<div>
#Html.ItemDisplayLink(item.ContentItem as IContent)
</div>
</div>
</div>
</div>
}
</div>
#using Orchard.ContentManagement;
#{
IEnumerable<dynamic>
blogPosts = Model.ContentItems;
Model.ContentItems.Classes.Add("content-items");
Model.ContentItems.Classes.Add("blog-posts");
}
<div class="row">
#foreach (var item in blogPosts)
{
IContent contentItem = item.ContentItem;
var metadata = contentItem.ContentItem.ContentManager.GetItemMetadata(contentItem);
string alias = metadata.Identity.Get("alias");
<div class="col-md-4 col-sm-6">
<div>
<div class="blog-seg post-item seg">
#Display(item)
</div>
</div>
</div>
}

showing MongoDB document value to EJS in a loop

I'm working on a node project. And node is very new to me so sorry if I ask a lot of stupid questions.
I'm trying to show all of my topics on my index.ejs. The root file already shows them in the console log but I can't seem to get them to show in the view.
The view already loops and knows that there are 2 topics, but the content is empty.
This is my code from the routes/index.js
router.get('/', function(req, res, next) {
Topic.find({}).exec(function(err, topic)
{
if(err){
console.log('There ware no topics');
return next(err)
}
else
{
console.log('Whoop whoop there are some topics');
res.render('index', { topic: topic } );
console.log("Logging data: " + topic);
console.log("Loggin data title out db: " + topic.topicTitle);
console.log("Loggin data desc out db: " + topic.topicDescription);
console.log("Loggin data date out db: " + topic.topicDateCreated);
}
});
});
module.exports = router;
And here is how I try to show it in my views/index.ejs
<ul>
<% for(var i = 0; i < topic.length; i++) {%>
<li>
<div class="post animated fadeInLeft">
<div class="wrap-ut pull-left">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="default avatar" />
<p class="moderator"> <%= topic.fbUsername %> </p>
</div>
</div>
<div class="posttext pull-left">
<h2 class="topictitle"><a href="/topicdetail/{topic_id}" <%= topic.topicTitle %> </a></h2>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</li>
<% } %>
</ul>
The topic variable is an array, so you need to pass the index in order to get the proper Topic document. For instance:
<%= topic[i].fbUsername %>
Your view should look like:
<ul>
<% for(var i = 0; i < topic.length; i++) {%>
<li>
<div class="post animated fadeInLeft">
<div class="wrap-ut pull-left">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="default avatar" />
<p class="moderator"> <%= topic[i].fbUsername %> </p>
</div>
</div>
<div class="posttext pull-left">
<h2 class="topictitle"> <%= topic[i].topicTitle %> </h2>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</li>
<% } %>
</ul>
UPDATE
You also forgot to close the tag here:
--------------------------------------------------------↴
<h2 class="topictitle"><a href="/topicdetail/{topic_id}" <%= topic[i].topicTitle %> </a></h2>
Closing the tag, I was able to see the titles:
<h2 class="topictitle"> <%= topic[i].topicTitle %> </h2>

How can I dynamically create rows in Jade?

Right now I have this:
- count = 0
- each organization in organizations
if (count == 0 || count % 3 == 0)
.row
.col-md-4
a(href="#{organization.url}" target="_blank")
.fancyy-box
h3= organization.name
img(src="/images/organizations/#{organization.logo}")
p= organization.mission
- count = count + 1
What I am trying to do is start a new row every third organization, so that I get:
<div class="row">
<div class="organization">...</div>
<div class="organization">...</div>
<div class="organization">...</div>
</div>
<div class="row">
<div class="organization">...</div>
<div class="organization">...</div>
<div class="organization">...</div>
</div>
// and so on...
Right now I am getteing:
<div class="row" />
<div class="organization">...</div>
<div class="organization">...</div>
<div class="organization">...</div>
<div class="row" />
<div class="organization">...</div>
<div class="organization">...</div>
<div class="organization">...</div>
<div class="row" />
Is there an easy way to accomplish this?
Like Mukesh Soni said you need to think about your indentation. But this is just one part of your problem, the second thing I recognized is your conditional statement and your loop. I edited this a little bit like you can see below:
- for (var j = 0; j < organizations.length; j++)
if (j == 0 || j % 3 == 0)
.row
- for (var i = j; i < (3 + j); i++ )
- if (i >= organizations.length) break;
.col-md-4
a(href="#{organizations[i].url}" target="_blank")
.fancyy-box
h3= organizations[i].name
img(src="/images/organizations/#{organizations[i].logo}")
p= organizations[i].mission
So in the first for loop I simply post a row every three times of iteration, in the second for loop I add your data, simply by adding 3 lines / divs. If i gets bigger then your data - it breaks.
Here an example: first my data
{
organizations: [
{name: 'demo1', url: 'example.com', logo: 'pic1.jpg', mission: '1'},
{name: 'demo2', url: 'anotherexample.com', logo: 'pic2.jpg', mission: '2'},
{name: 'demo3', url: 'justanotherexample.com', logo: 'pic3.jpg', mission: '3'},
{name: 'demo4', url: 'wowjustanotherexample.com', logo: 'pic4.jpg', mission: '4'}]
}
Now the HTML output:
<div class="row">
<div class="col-md-4">
<a href="example.com" target="_blank">
<div class="fancyy-box">
<h3>demo1</h3><img src="/images/organizations/pic1.jpg"/>
<p>1</p>
</div>
</a>
</div>
<div class="col-md-4">
<a href="anotherexample.com" target="_blank">
<div class="fancyy-box">
<h3>demo2</h3><img src="/images/organizations/pic2.jpg"/>
<p>2</p>
</div>
</a>
</div>
<div class="col-md-4">
<a href="justanotherexample.com" target="_blank">
<div class="fancyy-box">
<h3>demo3</h3><img src="/images/organizations/pic3.jpg"/>
<p>3</p>
</div>
</a>
</div>
</div>
<div class="row">
<div class="col-md-4">
<a href="wowjustanotherexample.com" target="_blank">
<div class="fancyy-box">
<h3>demo4</h3><img src="/images/organizations/pic4.jpg"/>
<p>4</p>
</div>
</a>
</div>
</div>
You could do this
.row
each organization, i in organizations
if i > 0 && i % 3 === 0
// jade/pug can interpret html
</div><div class="row">
// past you col template here
.col-md-4
a(href="#{organizations[i].url}" target="_blank")
.fancyy-box
...

Resources