1

I want to differentiate the values using different colours. Here's a rough idea of what i want to do. I was able to do it for a button but not in a table. When the values are retrieved from the database I want to place a background colour according to the diary.level values. sample of my intended end result

Health.js

    angular
  .module('app')
  .controller('TrackingController', ['$scope', 'Diary', '$state', function($scope, Diary, $state) {
    $scope.diaries = [];

    $scope.submitForm = function() {
      Diary
        .upsert({
          date: $scope.diary.date,
          taken: $scope.diary.taken,
          level: $scope.diary.level
        })
        .$promise
        .then(function() {
          $state.go('data');
        });
    };
  }])

    .controller('DataController', ['$scope', 'Diary', function ($scope, Diary) {
        $scope.diaries = Diary.find();

            if($scope.diary.level == "1-3.8") {
            $scope.buttonClass = "background-color:lightgreen";
            }
            else if ($scope.diary.level == "4-7") {
                $scope.buttonClass = "background-color:tomato";
            }
            else if ($scope.diary.level == "7.2-10.0") {
                $scope.buttonClass = "background-color:purple";
            }
            else {
                $scope.buttonClass = "background-color:yellow";
            }
    }]) 

data.html

<section>
<body>

<table style="width:90%"  align="center">
  <tr>
    <th>Date</th>
    <th>Type</th>
    <th>Level</th>
  </tr>
  <tr ng-repeat="d in diaries">
    <td>{{d.date}}</td>
    <td>{{d.taken}}</td>
    <td style={{buttonClass}}>{{d.level}}</td>
  </tr>
</table>
</body>
</section>
sxxxxxxx
  • 565
  • 2
  • 6
  • 17

1 Answers1

0

write a helper function which can be called for each item in a diaries list.

<section>
<body>

<table style="width:90%"  align="center">
  <tr>
    <th>Date</th>
    <th>Type</th>
    <th>Level</th>
  </tr>
  <tr ng-repeat="d in diaries">
    <td>{{d.date}}</td>
    <td>{{d.taken}}</td>
    <td ng-style="getbuttonClass(d);">{{d.level}}</td>
  </tr>
</table>
</body>
</section>


.controller('DataController', ['$scope', 'Diary', function ($scope, Diary) {
        $scope.diaries = Diary.find();
    if(d.level == "1-3.8") {
          return {'background-color':'lightgreen'};
     }else if (d.level == "4-7") {
          return {'background-color':'tomato'};
     }else if (d.level == "7.2-10.0") {
         return {'background-color':'purple'};
     }else {
         return {'background-color':'yellow'};
     }

}