1

I want to provide a different background color for the angular ng-repeat in the table design format. If play_id is same I want to provide same background color in tr tag else another color tr tag.

JSON (https://jsfiddle.net/0sasz78n/)

{
    "play_details": [
        {
            "play_id": "15",
            "quest_id": "63",
            "question": "What will be the match result ?"
        },
        {
            "play_id": "8",
            "quest_id": "61",
            "question": "Which of this batswan will score higher ?"
        },
        {
            "play_id": "8",
            "quest_id": "59",
            "question": "What will be the match result ?"
        }
    ]
}

HTML code:

<table><tr ng-repeat="single_Play in all_Play_details">
    <td>{{$index + 1}}</td>
    <td>{{single_Play.play_id}}</td>
    <td>{{single_Play.quest_id}}</td>
    <td>{{single_Play.question}}</td>
</tr></table>

JS code:

$scope.all_Play_details = response.data.play_details;
5
  • Your jsfiddle link has nothing. Commented Jan 2, 2017 at 10:50
  • {"play_details":[{"play_id":"8","quest_id":"61","question":"Which of this batswan will score higher ?"},{"play_id":"8","quest_id":"59","question":"What will be the match result ?"}]} Commented Jan 2, 2017 at 10:51
  • this is json format Commented Jan 2, 2017 at 10:52
  • 1
    You could use ng-style/ng-class, but before that include backgroundColor property in each element of your array Commented Jan 2, 2017 at 10:52
  • the play_id will change randomly, then how can provide ng-class Commented Jan 2, 2017 at 10:54

3 Answers 3

1

edit: ok, Rouk was faster ;)

I would suggest to use ng-class and add something like the following into your ng-repeat tag.

ng-class="{'highlightedCell' : play_details.play_id == currentID, 'diffentColoredCell' : play_details.play_id != currentID"}

and of course create the appropriate css-class.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use ng-class for that. https://docs.angularjs.org/api/ng/directive/ngClass

Just create some classes with different background color, then add a condition in ng-class.

table><tr ng-repeat="single_Play in all_Play_details" ng-class="{'YOURCSSCLASS': single_Play.play_id == 8}>
...
 </tr></table>

Something like that should work.

To really fit your need (having the same background-color for every ID), you could also pass a function to ng-class. In this function, add some logic and return the good CSS class. (logic could be to add have a scope array of object, like [{8: 'GREEN'}, {12: 'YELLOW'}] and everytime you go in this function, you add an object if the ID doesn't exist, and you associate a random color. If the ID already exist, you simply return the CSSproperty (like GREEN, fir play_ID 8)

8 Comments

can u please tell how to add that condition
main thing play_id change randomly , then how can provide statically play_id == 8
yeah, it was as an example. If play_id change randomly and is not in a range, you can add a background-color property to your JSON as Pankaj Parkar suggested.
However, setting a background-color depending on a random ID is kinda weird, if I can give my opinion.
is it possible to write like this ng-class="{'YOURCSSCLASS': single_Play.play_id =='next_play_id'} how to get this 'next_play_id'
|
0

Just do like this :

 <table>
   <tr ng-repeat="single_Play in all_Play_details">
     <td>{{$index + 1}}</td>
     <td ng-class="{'background-color: yellow':single_Play.quest_id === 2 || single_Play.quest_id === 3 ,'background-color: red':single_Play.quest_id === 4}">{{single_Play.play_id}}</td>
     <td>{{single_Play.quest_id}}</td>
     <td>{{single_Play.question}}</td>
   </tr>
 </table>

Like this you can writes ng-class cases . hope it will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.