Skip to content Skip to sidebar Skip to footer

Conditional Formatting Of Table With Angular And Javascript

How would I go about conditioning below table for when the lastPingedTimeAsString reaches a 5 minute mark and changes the color of the text from green to red, and vice versa.

Solution 1:

Let's add a function to our controller scope:

$scope.diffMins = function(pingDate) {
  var now = newDate();
  var diffMs = (now-pingDate);
  returnMath.round(((diffMs % 86400000) % 3600000) / 60000);
}

<form name="myform" data-ng-submit="submit()" data-ng-controller="Ctrl" id="submitForm">
    <pdata-ng-repeat="beat in beats"ng-class="{longtime:diffMins(beat.lastPinged)>=5}">
        Station ID: {{ beat.stationID }}
        Uptime: {{ beat.lastPingedAsString | date : 'mediumTime'}}
    </p><inputtype="submit"id="submit"value="Submit" />
</form>

css: .longtime {color: red}

This assumes you have a beat.lastPinged as an javascript date. You could also put the business logic inside diffMins and just return true or false.

ng-class works by assinging a class with the name from the left if the right side evaluates as true.

There is also an ng-style that does something similar with css style attributes.

Post a Comment for "Conditional Formatting Of Table With Angular And Javascript"