assign style with *ngIf evaluation in angular2 - css

This is what I have:
home.html
<li *ngFor="let item of myList">
<div *ngIf="item.messageText === {{myVar}}" class="bordered">{{item.messageText}}</div>
<div *ngIf="item.messageText !== {{myVar}}" class="greyedOut">{{item.messageText}}</div>
</li>
in home.ts I have defined the variable myVar and I've assigned to it a value.
I want to assign bordered class to the element of myList which has a value equal to myVar and assign another class if that element's value is different.

*ngIf would work if you change {{myVar}} to myVar(without interpolation) in expression.
<li *ngFor="let item of myList">
<div *ngIf="item.messageText === myVar" class="bordered">{{item.messageText}}</div>
<div *ngIf="item.messageText !== myVar" class="greyedOut">{{item.messageText}}</div>
</li>
Though I would prefer you to use ngClass here, more cleaner and better solution.
<li *ngFor="let item of myList">
<div [ngClass]="item.messageText == item.messageText ? 'bordered': 'greyedOut'">
{{item.messageText}}
</div>
</li>
OR
<li *ngFor="let item of myList">
<div [ngClass]="{'bordered': item.messageText == item.messageText, 'greyedOut': item.messageText !== item.messageText }">
{{item.messageText}}
</div>
</li>

To complete Panjak Parkar's answer, and to answer to the question in the title (style)
<li *ngFor="let item of myList">
<div [ngStyle]="{'background-color':item.country === 'UK' ? 'green' : 'red'}">
</div>
</li>

Related

How do I properly execute "flex-row" in this instance?

I'm attempting to have a portion of my React code appear in an in-line block using "flex-row" as a class name, but it's not working. I've looked at other questions and haven't been able to find the answer needed (I'm attempting it without using the "View" wrapper). My code is as follows (but my result is does not show the list in an in-line block; see attached image below):
<header data-testid="header" className="flex-row px-1">
<nav>
<ul className="flex-row">
<li className="mx-2">
<a href="#language" onClick={() => handleClick()}>
Language
</a>
</li>
<li>
<a href="#about" onClick={() => handleClick()}>
About Us
</a>
</li>
</ul>
</nav>
</header>
I feel like it's a simple fix, but any help is greatly appreciated. Thank you in advance!
I think you are using TailwindCSS. If so add a flex class before "flex-row" in element. Hope this will sort your issue/.
<header data-testid="header" className="flex-row px-1">
<nav>
<ul className="flex flex-row">
<li className="mx-2">
<a href="#language" onClick={() => handleClick()}>
Language
</a>
</li>
<li>
<a href="#about" onClick={() => handleClick()}>
About Us
</a>
</li>
</ul>
</nav>
</header>
It truly was a simple fix...lol. I had code for the navigation options "Language" and "About Us" to be in an in-line block (hence the "flex-row" denomination), yet I have <li> tags around the two navigation options. Once I took off the tag, it worked. Thanks, everyone! corrected image displayed here

Problem with Active Item in angular tablist

i have issue with active class in tablist/listgroup of angular. when i click any items it becomes active,but active class is not being removed from old tab. Please help me out
<div class="list-group" id="list-tab" role="tablist">
<span *ngFor="let itm of serviceitms;let i = index">
<a *ngIf="i == 0" class="list-group-item list-group-item-action active" id="list-home-list" data-toggle="list"
href="#list-home" role="tab" aria-controls="itm.name">{{itm.name}}</a>
<a *ngIf="i != 0" class="list-group-item list-group-item-action" id="list-home-list" data-toggle="list"
href="#list-home" role="tab" aria-controls="itm.name">{{itm.name}}</a>
</span>
</div>
Seems like you are using angular with bootstrap directly. Use ng-bootstrap or ngx-bootstrap angular should not be directly used with bootstrap. That is a very bad practice.
ng-bootstrap option:
https://ng-bootstrap.github.io/#/components/nav/overview
ngx-bootstrap option (combine groups with accordion):
https://valor-software.com/ngx-bootstrap/#/buttons
https://valor-software.com/ngx-bootstrap/#/accordion
i think you should change the id and href for every item.
also you should do this for tab content divs.
<div class="list-group" id="list-tab" role="tablist">
<span *ngFor="let itm of serviceitms;let i = index">
<a [class.active]="i == 0" class="list-group-item list-group-item-action" id="list-home-list{{ i }}" data-toggle="list"
href="#list-home{{ i }}" role="tab" aria-controls="itm.name">{{itm.name}}</a>
</span>
</div>

Dynamic multi-columns dropdown - Angular2 and Bootstrap

How to create a dynamic multi column dropdown list. I am using angular2 and looking for something like this http://alijafarian.com/bootstrap-multi-column-dropdown-menu/
But here we have the columns fixed at 3 and the list items hardcoded.In my case I have a dynamic list and would like to display 5 items in one column.So for Eg: if the list contains 20 items then we would have 4 columns with 5 items each in one column.And my code looks like below
....
<li role="presentation" class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown"
href="#" role="button"
aria-haspopup="true" aria-expanded="false">
Artists
<span class="caret"></span></a>
<ul class="dropdown-menu ">
<li *ngFor="let artist of artists">
<a><input type="checkbox"> {{artist.name}} </a>
</li>
</ul>
</li>
.....
I am using ngFor directive to loop through the list of artists and display it in single column now. But would like to display it in multi column depending on the size of the list.
You should be created a new array from the data by using a simple chunk function:
public chunk(arr, size) {
var newArr = [];
for (var i=0; i<arr.length; i+=size) {
newArr.push(arr.slice(i, i+size));
}
return newArr;
}
and do some changes in your view like this:
<ul class="dropdown-menu multi-column columns-2">
<div class="row">
<div class="col-sm-6" *ngFor="let persons of chunk(persons, 3)">
<ul class="multi-column-dropdown">
<li *ngFor="let person of persons">{{person.name}}</li>
</ul>
</div>
</div>
</ul>
Here is an example: plunker

Navigation styling - selecting child li only

I'm building a LHN in Sitefinity and ran into an issue styling it. When a page is set as the active page, it gets the sfsel class. Unfortunately, it also applies it to the parent page when a subpage is active. I need to get the styling so when a subpage is active, only that list item is highlighted, but when only the parent "About" page is active, it still gets highlighted.
http://jsfiddle.net/4NnaZ/1/
<div class="sfNavWrp sfNavTreeviewWrp leftnav">
<div class="k-widget k-treeview" tabindex="0" role="tree" aria-activedescendant="C001_ctl00_ctl00_navigationUl_tv_active">
<ul class="sfNavTreeview sfNavList k-group k-treeview-lines" id="C001_ctl00_ctl00_navigationUl" data-role="treeview">
<li class="k-item k-first k-last" data-uid="ceac0efa-1b50-46c7-a351-945f05a6eb87" role="treeitem" data-expanded="true" aria-expanded="true">
<div class="k-top k-bot"><span class="k-icon k-minus"></span><a class="sfSel k-in" href="../about">About</a>
</div>
<ul id="C001_ctl00_ctl00_ctl03_ctl00_childNodesContainer" class="k-group" style="display: block;">
<li class="k-item" data-uid="3b1f4e90-1945-4c93-a770-43787527d7bf" role="treeitem" id="C001_ctl00_ctl00_navigationUl_tv_active">
<div class="k-top"><a class="sfSel k-in k-state-focused" href="locations">Locations</a>
</div>
</li>
<li class="k-item" data-uid="48d48d44-55ee-4bf7-9fcd-20380c18b991" role="treeitem">
<div class="k-mid">Careers
</div>
</li>
<li class="k-item" data-uid="267e4a18-8489-45c2-bef3-1efcba63916f" role="treeitem">
<div class="k-mid">Producer Board
</div>
</li>
<li class="k-item k-last" data-uid="d75d7989-3815-49b3-856c-c4d24dcd5dc8" role="treeitem">
<div class="k-bot">Contact Information
</div>
</li>
</ul>
</li>
</ul>
</div>
isn't k-state-focused the class you should use to highlihgt only current link/page ?
I believe this is a jQuery script producing this classname.
You can modify your code to set a class for that purpose , http://www.sitefinity.com/developer-network/forums/general-discussions-/highlight-current-page

How to apply css style for HTML element using Mootools

How can we hide all list element from first form in following html structure using mootools
<form method="post" action="/signup" class="global_form" enctype="application/x-www-form-urlencoded">
<div>
<div>
<ul class="form-errors">
<li>First Name
<ul class="errors">
<li>Please provide a valid answer for this field.</li>
</ul>
</li>
<li>Last name
<ul class="errors">
<li>Please provide a valid answer for this field.</li>
</ul>
</li>
</ul>
</div>
</div>
</form>
<form method="post" id="user_form_login" action="/login" class="global_form" enctype="application/x-www-form-urlencoded">
<div>
<div>
<ul class="form-errors">
<li>Uername
<ul class="errors">
<li>Please provide a valid answer for this field.</li>
</ul>
</li>
<li>Password
<ul class="errors">
<li>Please provide a valid answer for this field.</li>
</ul>
</li>
</ul>
</div>
</div>
</form>
This is completely untested and I switched to jQuery a while ago so there might be a cleaner way to do this, but this should give you a basic idea.
If you are trying to select multiple elements use the mootools dollars method.
If you were to append an id to the first form you could do something like this...
$$('#form1 li').setStyle('display', 'none');
OR
If you do not want to add an id tag this might work.
var firstForm = $$("form").getFirst(),
listElements = firstForm.getChildren('li');
listelements.setStyle('display', 'none');

Resources