Angular apply style to the first visible item in ngRepeat - css

I have a list generated by ngRepeat, like so
<ul class="list-group">
<li class="list-group-item" ng-repeat="data in tree | filter:key">
{{data.name}}
</li>
</ul>
and I want the first item in the list to have rounded corners. I have a style such as
.first-item {
border-top-left-radius: 15px;
border-top-right-radius: 15px;
}
How can I cause this style to be applied to the first visible item? Note that the key is connected to a search box, so the first item is dynamic.

use $first, that represents the first iteration within a ng-repeat
<ul class="list-group">
<li ng-class="{'first-item':$first}"
class="list-group-item" ng-repeat="data in tree | filter:key">
{{data.name}}
</li>
</ul>
edit: since first-item has a dash, it must be in quotes

You can use the css rule first-child:
.list-group li:first-child {
border-top-left-radius: 15px;
border-top-right-radius: 15px;
}
http://www.w3schools.com/cssref/sel_firstchild.asp

Pure Angular Way would be:
<ul class="list-group">
<li class="list-group-item" ng-repeat="data in tree | filter:key track by $index" ng-class="{MyClassName: $index == 0}">
{{data.name}}
</li>
</ul>
or see answer of Sasi Kiran ;)

You can use the $index variable value to do such conditional formatting within ng-repeat. Here's an example:
<ul class="list-group">
<li class="list-group-item"
ng-repeat="data in tree | filter:key"
ng-class="{'first-item': $index == 0}">
{{data.name}}
</li>
</ul>

Use $first and ng-class. $first is a property which is exposed on the local scope, for each item in the repeated list. It's value is true if the item is the first one in the repeated list. The HTML should look something as below:
<ul class="list-group">
<li class="list-group-item" ng-repeat="data in tree | filter:key" ng-class="{'.first-item': $first}">
<div>{{data.name}}</div>
</li>

Related

Targetting next sibling on hover with tailwindcss

I am trying to hide an element until its previous sibling is hovered over, in css (or scss rather), it looks like this:
.menu-container {
// style with flex etc...
& .menu-item-link {
// style the link...
&+.sub-menu-container {
display: none;
}
&:hover+.sub-menu-container {
display: block;
}
}
}
<ul class="menu-container">
<li class="menu-item-container">
<a class="menu-item-link">Ingredients</a>
<ul class="sub-menu-container">
<li class="sub-menu-item-container">
<a class="sub-menu-link">Fruits</a>
</li>
<li class="sub-menu-item-container">
<a class="sub-menu-link">Vegetables</a>
</li>
<li class="sub-menu-item-container">
<a class="sub-menu-link">Dairy</a>
</li>
<li class="sub-menu-item-container">
<a class="sub-menu-link">Children</a>
</li>
</ul>
</li>
</ul>
How do I achieve this using tailwind?
You're not actually trying to target a sibling in your code, you're trying to target a child element. This is a very simple process when you just want to show a sub-menu dropdown.
Just add group to the hover trigger (.menu-item-link in your case) and group-hover:[some-display-class] to the child. This way the child will change it's display property when the parent element (or itself) is hovered.
You should change your title, also I'd recommend that you don't use Tailwind with class names like that. Please see extracting components for the recommended way to use Tailwind CSS. Of course, you are free to use it how you want but you're better off with plain old CSS if you want to use SCSS and classes like that.
Example with your structure:
<ul>
<li class="group">
<a>Ingredients</a>
<ul class="hidden group-hover:block">
<li>
<a>Fruits</a>
</li>
<li>
<a>Vegetables</a>
</li>
<li>
<a>Dairy</a>
</li>
<li>
<a>Children</a>
</li>
</ul>
</li>
</ul>
Example on Tailwind Play https://play.tailwindcss.com/dFc2zlmqDA

Divide space into 3 parallel blocks

I expect li inside ul to be divided into 3 parallel lists of elements next to each other. However, the list appears in a straight list.
I expected col-xs-3 to do the job but it does not seem to work. Suggestions to fix this?
return <div id="col_sub_1">
<ul className="col-xs-3 sub-menu-width ">
{
childitem.map(function(subcat,subcatindex){
{/*--LEAF WHEN NO CHILD ELEMENTS */}
return <li>
{
subcat.id !=54 ? <a className="event_menu_item_desktop"><span> {subcat.name}</span></a> : null
</li>
})
}
</ul>
</div>
You need to add col-xs-3 to <li> tag but not to <ul> element. Also You are not properly closed expression.
Keep in mind always that you need to add unique key to the top element inside loop in react. Try this
return <div id="col_sub_1">
<ul className="sub-menu-width ">
{
childitem.map((subcat,subcatindex) => (
<li key={subcat.id} className="col-xs-3">
{subcat.id !=54 ? <a className="event_menu_item_desktop"><span> {subcat.name}</span></a> : null}
</li>
))}
</ul>
</div>

How to highlight same page number for two different URLs in angular 2 using routerLink

I have pagination like below. I would like to highlight page number 2 for both the below links.
[routerLink]="['/link/sublink2']
[routerLink]="['/link/sublink22']
How to achieve this using angular 2.
<ul class="pagination pagination-plain">
<li [routerLinkActive]="['active']">
<a [routerLink]="['/link/sublink1']">1</a>
</li>
<li [routerLinkActive]="['active']">
<a [routerLink]="['/link/sublink2']">2</a>
</li>
<li [routerLinkActive]="['active']">
<a [routerLink]="['/link/sublink3']">3</a>
</li>
</ul>
In your html,
<li [class]="addActiveClass()" >
<a [routerLink]="['/link/sublink2', '/link/sublink22']">2</a>
</li>
and in your component.ts file,
private addActiveClass() {
let fullpath:any[]=this.router.url.split("/")
if(fullpath[2] == 'sublink2' || fullpath[2] == 'sublink22'){ /* fullpath[2] has the second parameter after the '/'. If you want first parameter after the slash, use fullpath[1] */
return 'active';
}else{
return '';
}
}
This adds an active class to the li when you route to the page. You can highlight the li.active using css.

CSS- Can't add margin between links?

I'm using a CMS . I have a menu. I've been able to edit the menu class of course, but not the li class. ALL I want to do is add some space between each link using margin. However the CSS is not working??
CSS I'm trying to use with class:
#block-system-main-menu li.menu__item is-leaf first leaf a{
margin-bottom:15px;
}
ISN'T this right?
HTML
<div id="block-system-main-menu" class="block block-system contextual-links-region block-menu first last odd" role="navigation">
<div class="contextual-links-wrapper contextual-links-processed">
<ul class="menu">
<li class="menu__item is-leaf first leaf">
<li class="menu__item is-leaf leaf">
<li class="menu__item is-leaf leaf">
<li class="menu__item is-leaf leaf">
<li class="menu__item is-leaf leaf">
<li class="menu__item is-leaf leaf">
<li class="menu__item is-leaf last leaf">
<a class="menu__link active" title="" href="/">Contact Us</a>
</li>
</ul>
</div>
The margins on a are ignored unless it's displayed as block/inline-block.
If you have multiple classes, then you need to join them with . because otherwise css mistakes them for element names (as pointed out by Benjamin)
To make a larger area clickable, I'd suggest using padding instead of margins, but that depends on what you want.
#block-system-main-menu li.menu__item.is-leaf.leaf a
{
padding-bottom: 15px;
display: inline-block;
}
http://jsfiddle.net/7SWB4/
li.menu__item is-leaf first leaf a
This assumes, that you have element a inside element leaf inside element first and etc. Class names should not contains spaces. Spaces separates different classes|elements.
So in your case you can just leave:
#block-system-main-menu li.menu__item
And margin will apply to all list elements

CSS Selector for incremented ID

I have a bunch of incremented elements
<ul>
<li id="idTab1">
<li id="idTab2">
<li id="multiTab1">
<li id="multiTab2">
<li id="multiTab3">
<li id="idTab3">
</ul>
Is there some CSS selector method for me to select all 'multiTab' elements? something like 'li #multiTab*' ? :P
You can use ^=, example:
li[id^="mulitiTab"]

Resources