guagay_wk
guagay_wk

Reputation: 28030

Make color of text change depending on certain condition with angularjs ng-table

I am using angularjs ng-table module to display values in a table.

The relevant html code looks like this;

<div ng-controller="ViewCtrl" class="container">
    <table ng-table="tableParams" class="table table-bordered">
        <thead>

        <tr>            
            <th>id</th>            
            <th>price_alert</th>
            <th>lastPrice</th>
        </tr>

        <thead>
        <tbody>
        <tr ng-repeat="item in $data">
            <td data-title="'id'">
                {{item.id}}
            </td>
            <td data-title="'price_alert'">
                ${{item.price_alert}}
            </td>
            <td data-title="'lastPrice'">
                ${{item.lastPrice}}
            </td>
        </tr>
        </tbody>
        </tbody>
    </table>
</div>

The controller code looks like this;

controller('ViewCtrl', ['$scope', '$http', 'moment', 'ngTableParams',
        function ($scope, $http, $timeout, $window, $configuration, $moment, ngTableParams) {
            var tableData = [];
            //Table configuration
            $scope.tableParams = new ngTableParams({
                page: 1,
                count: 100
            },{
                total:tableData.length,
                //Returns the data for rendering
                getData : function($defer,params){
                    var url = 'http://127.0.0.1/list';
                    $http.get(url).then(function(response) {
                        tableData = response.data;
                        $defer.resolve(tableData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                        params.total(tableData.length);
                    });
                }
            });
        }])

I want the color of lastPrice to become red when lastPrice < price_alert condition is fulfilled. Otherwise, lastPrice is default color which is black.

Upvotes: 0

Views: 2675

Answers (1)

sahbeewah
sahbeewah

Reputation: 2690

Use ng-class and CSS:

HTML

<td data-title="'lastPrice'" ng-class="{ red: item.lastPrice < item.price_alert }">
    ${{item.lastPrice}}
</td>

CSS

.red { color: red; }

Upvotes: 3

Related Questions