0

I want to change Background color of row based on cell data, like if it matches first four characters from table cell "td", then row should change its color to red.

here is my example, it is working but it takes whole cell data.but i want row color should change once it matches first four characters from cell.

 <style>
        .bgRed{
            background-color:red;
        }
    </style>

<body ng-app="myApp">
    <div ng-controller="myController">
        <div class="container" style="margin-top:40px;">
            <div class="row">
                {{error}}
                <div class="col-md-6">
                    <table class="table table-bordered table-condensed">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Class Name</th>
                                <th>Roll No</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody ng-repeat="std in students">
                            **<tr ng-class="{'bgRed': std.Name === 'Prash'}>**
                                <td>{{std.Name}}</td>
                                <td>{{std.ClassName}}</td>
                                <td>{{std.RollNo}}</td>
                                <td>{{std.Email}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>

and my table is

enter image description here

Table row should change to red if table cell data "Prash or Prashant" matches.instead of taking "Prashant Olekar"

how to achive this please help. Thank you

0

3 Answers 3

3

Using substring you can trim the characters of the string, here I'm creating one more variable(filed) "trimmed_name" which is a substring of your "name" which gives you first 4 characters of your string "name".

<tr ng-repeat="std in students" ng-init="trimName(std)">
   <td ng-class="{'bgRed': std.trimmed_name === 'Prash', 'bgBlue': std.trimmed_name === 'Pavi', 'bgBlack' : std.trimmed_name === 'Pava'}">{{std.Name}}</td>
   <td>{{std.ClassName}}</td>
   <td>{{std.RollNo}}</td>
   <td>{{std.Email}}</td>
</tr>

In Css add respective colours for respective classes

in your controller

  $scope.trimName = function (std) {
  std.trimmed_name = std.Name.substring(0, 4);
  return std;
};

Just in case if 'Prash' dose not work use {'bgRed': std.trimmed_name === &quot;Prash&quot;}

Hope it helps you.

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

1 Comment

Thank you so much bro.. I appreciate your answer it works.
1

you can use a custom filter to set class according to condition of your row data,

html

<tbody ng-repeat="std in students | filter:filterColor">
    <tr class="{{std.class}}">
        <td>{{std.Name}}</td>
        <td>{{std.ClassName}}</td>
        <td>{{std.RollNo}}</td>
        <td>{{std.Email}}</td>
    </tr>
</tbody>

js

    $scope.filterColor = function (item) {
        if (your condition) {
            item.class = 'your class';
        }
        else
            item.class = 'some other class';
        return true;
    };

1 Comment

Thank you so much for your answer, I will try this and let you know. if its work i will be happy.
0

I have got solution to my question with the help of Rajat, here is my code. but it only matches characters from 0th Position.

<head>
    <title></title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/App/app.js"></script>
    <style>
        .bgRed {
            background-color: #cfeaff;
        }
    </style>
</head>
<body ng-app="myApp">
    <div ng-controller="myController">
        <div class="container" style="margin-top:40px;">
            <div class="row">
                {{error}}
                <div class="col-md-6">
                    <table class="table table-bordered table-condensed">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Class Name</th>
                                <th>Roll No</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="std in students" ng-init="trimName(std)" ng-class="{bgRed: std.trimmed_name === 'Pras'}">
                                <td>{{std.Name}}</td>
                                <td>{{std.ClassName}}</td>
                                <td>{{std.RollNo}}</td>
                                <td>{{std.Email}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

and In Controller

/// <reference path="../angular.min.js" />

var myApp = angular.module('myApp', [])

            .controller("myController", function ($scope, $http, $log) {

                var successCallback = function (response) {
                    $scope.students = response.data;
                    $log.info(response);
                }
                var errorCallback = function (response) {
                    $scope.error = response.data;
                    $log.info(response);
                }

                $scope.StudentsData = $http({
                    method: 'GET',
                    url: 'PKWebService.asmx/getPkData'
                })
                 .then(successCallback, errorCallback);

                $scope.trimName = function (std) {
                    std.trimmed_name = std.Name.substring(0, 4);
                    return std;
                };
            });

and output is

enter image description here

Thank you

2 Comments

not just from the 0th position, you have a control over it, substring(0, 4), first arg is your starting position, i.e 0(zero) in this example. string.substring(start, end).
yes but within the table cell i need match random contents..say for ex: if content comes first in one row or same content comes last in second row or in the middle, so in that case i need to match that content in every row if it is present else leave that as it is.

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.