0

I have this dynamic table:

<table id="thetable">


            <tr> 

                <th class="th2">&nbsp;&nbsp;Id&nbsp;&nbsp;</th>
                <th class="th2">&nbsp;&nbsp;Attivo&nbsp;&nbsp;</th>
                <th class="th2">&nbsp;&nbsp;Capacità&nbsp;&nbsp;</th> 
                <th class="th2">&nbsp;&nbsp;Zona&nbsp;&nbsp;</th>
                <th class="th2">&nbsp;&nbsp;Stato&nbsp;&nbsp;</th>

         </tr> 

            <tr style="cursor:pointer"  ng-repeat="staffdispenser in staffdispensers">  

        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.iddispenser }}</td>  
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.stateOn }}</td> 
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.capacity.type }}</td>
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.geoArea.city }}</td>  
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ (count[$index]*100)/(staffdispenser.dispenser.capacity.depthForSection*staffdispenser.dispenser.capacity.nsections) }}%</td>  

        </tr>  

        </table>

I want to change td background for a specific row, that rows where "count[$index]100)/(staffdispenser.dispenser.capacity.depthForSectionstaffdispenser.dispenser.capacity.nsections)" is less than 0.5

This is my view:

enter image description here

I want something like that:

enter image description here

2
  • use ng-class with that condition in it? Commented Jan 13, 2017 at 19:11
  • can you show me an example? Commented Jan 13, 2017 at 19:12

3 Answers 3

2

Have you ever heard of ngClass?

You can add it to your elements like this

<td ng-class="{'yellow-background': (count[$index]100)/(staffdispenser.dispenser.capacity.depthForSectionstaffdispenser.dispenser.capacity.nsections) < 0.5 }"></td>

Although your conditional is not syntactically valid. What is the 100 supposed to be?

count[$index]100)/(staffdispenser.dispenser.capacity.depthForSectionstaffdispenser.dispenser.capacity.nsections)
Sign up to request clarification or add additional context in comments.

2 Comments

[$index]*100 ..but nothing happenens
You need to make sure that all the variables in your conditional are in scope and that you actually have a yellow-background class in your CSS
1

Alfonso,

Consider looking here for an example of ngClass:

https://docs.angularjs.org/api/ng/directive/ngClass

You can apply this to the element to dynamically apply the class.

Example:

            <tr style="cursor:pointer"  ng-repeat="staffdispenser in staffdispensers" ng-class="{'special-row-color': staffdispenser.isMyCondition == true}">  

        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.iddispenser }}</td>  
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.stateOn }}</td> 
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.capacity.type }}</td>
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ staffdispenser.dispenser.geoArea.city }}</td>  
        <td ng-click="changePath(staffdispenser.dispenser.iddispenser)"> {{ (count[$index]*100)/(staffdispenser.dispenser.capacity.depthForSection*staffdispenser.dispenser.capacity.nsections) }}%</td>  

        </tr>  

I would recommend making a function inside the controller for that conditional you want to apply.

1 Comment

I'm not managing to make it work, can you do me a practical example and banal?
1

First, I think you have way too much logic in your Angular {{ }} expressions, and you should offload that to a controller.

You said:

I want to change td background for a specific row, that rows where "count[$index]100)/(staffdispenser.dispenser.capacity.depthForSectionstaffdispenser.dispenser.capacity.nsections)" is less than 0.5

So your <td> should look like this:

<td ng-class="getClass($index)">{{ myLogicHere }}</td>

And in your controller:

$scope.getClass = function(index) {
  var value = count[index]100)/(staffdispenser.dispenser.capacity.depthForSectionstaffdispenser.dispenser.capacity.nsections;
  return value < .5 ? 'highlight-class' : '';
};

Of course, adjust to whatever values/properties/logic your controller has.

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.