Solved

AngularJS :   ng-repeat  how -to conditionally format display items

Posted on 2015-01-06
7
313 Views
Last Modified: 2015-01-06
Hi..
I'm using  JSON binding to the following -   a fact contains  a name    so   fact.name = 'ABC',  fact.name ='XYZ'

  <ion-list>
     <ion-item ng-repeat="fact in facts" item="fact" >

     if fact contains  ABC I want to show the item like this.

       use ABC class and show it in BOLD

    if fact contains   XYZ  I want to show the itme like this.

    use XYZ class and show in RED


        and so on



How do I conditionally show / format etc an item based on the value?
0
Comment
Question by:JElster
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40533726
You can just create CSS classes that match those names and apply them with ng-class.
Can actually be even with the normal class like:
<ion-item ng-repeat="fact in facts" item="fact" class="{{fact.name}}">

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 40533746
How do I check if fact.name = 'XYZ' to use a particular class, say class = "RED".

How do I structure the item

like this?

 <ion-item class="RED">

thanks
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40533761
Can't you create a class called XYZ like:
.XYZ{
color: #f00;
}

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40533763
You can even prefix it to avoid conflicts:
<ion-item ng-repeat="fact in facts" item="fact" class="fact_{{fact.name}}">

Open in new window

And create a class like:
.fact_XYZ{
color: #f00;
}

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 40533778
How do I check if the fact.name is 'XYZ' ?   I can't name the class XYZ... it will not match the value in fact.name.

the class for XYZ maybe  Called 'Special_Class_For_XYZ'

thanks
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 40533809
That's my point.
To make it easier to maintain it should have the same name, otherwise you have to create a function in the controller that does the mapping:
$scope.getFactClass = function(fact){
    var name = fact.name;
    
    switch{
        case 'XYZ':
            return 'Special_Class_For_XYZ';
        case 'ABC':
            return 'SomeOtherClassForABC';
        default:
            return '';
        
    }
}

Open in new window

and use it like:
<ion-item ng-repeat="fact in facts" item="fact" class="{{getFactClass(fact)}}">

Open in new window

0
 
LVL 1

Author Closing Comment

by:JElster
ID: 40533835
thanks!!!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question