0

For my intention I've defined a table who has a column called "Difference". The column "Difference" contains numbers like so:

Difference | ...
    0.02   | (yellow)
    0.01   | (yellow)
    0      | (green)
   -0.06   | (green)
   -0.13   | (green)
    0.06   | (red)
    0.09   | (red)
...

As follows is the code of the view:

<tr ng-repeat="report in repos.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))">
    <td>{{ report.datum | date:'dd.MM.yyyy' }}</td>
    <td ng-repeat="row in rowOptions" ng-class="row.rowClass">
        {{ report[row.rowTitle] }}
    </td>
</tr>

Then the css definition for the colors:

.colGreat { background-color: #11a001; } // ok => <= 0
.colNormal { background-color: #ffd800; } // normal => between 0,01 and 0,03 
.colBad { background-color: #E60014; } // bad =>  >= 0,04

In my Ctrl the columns are defined in an array:

$scope.rowOptions = [
      { rowTitle: 'name' },
      { rowTitle: 'produce', rowClass: 'num-right' },
      { rowTitle: 'consume', rowClass: 'num-right' },
      { rowTitle: 'difference', rowClass: 'num-right ' + $scope.getColumnColor }
];

$scope.columnColor = ['colBad', 'colNormal', 'colGreat'];

Currently for testing I'm defining a static solution. How you can see the entire column is getting the color green, but I want to display the column with the corresponding colors each after what kind of number the row has.

How can I define this approach?


EDIT:

I've tried on this way but if the first item has for example 0.01 then all rows are yellow, even though I'm using forEach().

 angular.forEach($scope.repos, function (value) {
       var numDiff = parseFloat(value.Difference, 10);
       diffNumber.push(numDiff);
 });

 angular.forEach(diffNumber, function (val) {
       if (val <= 0.00) {
          return $scope.getColumnColor = 'colGreat';
       }

       if (val >= 0.01 && val <= 0.03) {
          return $scope.getColumnColor = 'colNormal';
       }

       if (val >= 0.04) {
          return $scope.getColumnColor = 'colBad';
       }
 });
3
  • see if you can create a plnkr so others will be able to help more easlily Commented Sep 10, 2015 at 8:53
  • You mean something like this? Commented Sep 10, 2015 at 9:04
  • I'll post it as an answer then. Commented Sep 10, 2015 at 9:11

4 Answers 4

2

Just defined the desired Json in the rowClass json That was:

{
 'num-right': 1, 
 'colGreat': report[row.rowTitle] <= 0,
 'colNormal': report[row.rowTitle] > 0 && report[row.rowTitle] <= 0.03,
 'colBad': report[row.rowTitle] > 0.04 
}

. And use expression syntax for the ngClass attribute so it evaluates with the ngClass

$scope.rowOptions = [
      { rowTitle: 'name' },
      { rowTitle: 'produce', rowClass: 'num-right' },
      { rowTitle: 'consume', rowClass: 'num-right' },
      { rowTitle: 'diff', 
         rowClass: "{'num-right': 1, 'colGreat': report[row.rowTitle] <= 0,'colNormal': report[row.rowTitle] > 0 && report[row.rowTitle] <= 0.03,'colBad': report[row.rowTitle] > 0.04 }"
      }
  ];

and change this line:

<td ng-repeat="row in rowOptions" ng-class="row.rowClass">

TO:

<td ng-repeat="row in rowOptions" ng-class="{{row.rowClass}}">

Here is an updated plunkr

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

11 Comments

I tried to go into the Map-Syntax direction as you suggested but it leaves me with a prototypal inheritence problem: the rowOptions has no reference to the report object you're trying to access because it is in the same scope as the repos array. You're expecting the options to behave as a child in the report iteration (new scope per ng-repeat). I'm curious as how you will solve this! +1 if you do! :-)
@skubski I've edited my post above.. I had tried with the forEach method to check every value and give them the color then. But it seemed that the forEach method check only the first item in the array and then colored the entire column with the same color.
That's because in the forEach you're assigning a different value to just only one object, the last css-class will be used as result. @yuro.
Keep that value for every field. P.S. You can use the row.rowClass like this too. What is the problem with this solution?
Thank you for the challenge @skubski. Here is the plunkr. plnkr.co/edit/QLCJInuUAflKi2tjhCLG?p=preview
|
1

@Yuro,

I dont know how you will be reusing this but combining yours and @skubski JS and html code.

Change your JSON to

$scope.rowOptions = [
      { rowTitle: 'name' },
      { rowTitle: 'produce', rowClass: 'num-right' },
      { rowTitle: 'consume', rowClass: 'num-right' },
      { rowTitle: 'difference', rowClass: 'num-right ', evaluate: true }
];

and your JS function to:

$scope.getClass = function(existingClass, val, evaluate) {
    if(!evaluate) 
      return existingClass;

    if(val <= 0) {
      existingClass = existingClass + ' colGreat'; //Or use the concat function
    } else if(val <= 0.03) {
      existingClass = existingClass + ' colNormal'; //Or use the concat function
    } else {
      existingClass = existingClass + ' colBad'; //Or use the concat function
    }
    return existingClass
}

and your html file to:

<tr ng-repeat="report in repos.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))">
    <td>{{ report.datum | date:'dd.MM.yyyy' }}</td>
    <td ng-repeat="row in rowOptions" 
        ng-class="getClass(row.rowClass, report.value, row.evaluate)">
        {{ report[row.rowTitle] }}
    </td>
</tr>

1 Comment

Thanksss It works fine! :) ... Could you help me in this topic -> stackoverflow.com/questions/32548886/…
0

A simplified dynamic version that uses a function to retrieve the class, instead of saving it in the object. Notice the getClass(value) function instead of carrying the class reference in your object.

  <table>
    <tr ng-repeat="report in reports">
      <td>{{ report.datum | date:'dd.MM.yyyy' }}</td>
      <td ng-class="getClass(report.value)">{{ report.value }}</td>
      <!--<td ng-class="getClass(report.value)">&nbsp;</td>-->
    </tr>
  </table>

And the function that determines which css class belongs to what value:

 $scope.getClass = function(val) {
    if(val <= 0) {
      return 'colGreat';
    } else if(val <= 0.03) {
      return 'colNormal';
    } else {
      return 'colBad';
    }
}

Complete example can be found here.

6 Comments

But what about other columns? maybe in the getClass function you can include classes for other columns as well?
You can easily extend the function to return more classes, which should be preferred over adding redundant repetitive data in your json , which in reality already carries the correct information in its value.
@skubski How would you define this in my ngRepeat? Because yours is a bit simpler then my example. <td ng-repeat="row in rowOptions" ng-class="row.rowClass"> {{ report[row.rowTitle] }} </td>
Updated my plunker to reflect your example and datastructure.
@skubski here is a prepared plunkr. plnkr.co/edit/AQZgR0dDuyffKla2Nood?p=preview
|
0

You can try with ng-if:

<tr ng-repeat="report in repos.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))">
    <td>{{ report.datum | date:'dd.MM.yyyy' }}</td>
    <td ng-repeat="row in rowOptions" ng-class="row.rowClass">
        <div ng-if="difference <= 0" class="colGreat">{{report[row.rowTitle]}}</div>
        <div ng-if="difference > 0 && difference <= 0.03" class="colNormal">{{report[row.rowTitle]}}</div>
        <div ng-if="difference > 0.03" class="colBad">{{report[row.rowTitle]}}</div>
    </td>
</tr>

Normally , the only div that will appear is the div that filled the condition with the class you want. I hope it 'll help you. Keep me informed.

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.