Change KendoGrid cell background based on condition using AngularJS and jQuery in ASP.Net MVC

mahesh213
 
on Oct 09, 2021 06:03 AM
Sample_153816.zip
605 Views

Hi,

How to display background color based upon Flag Value

Implement Inline Edit Update KendoGrid row using AngularJS in ASP.Net MVC

in above url code i am going to do one functionality

I have one field Named as Flag

public char Flag {get;set;}

based upon flag display field background color on initial load of grid

Note:don't display any text on background

If flag='G' then display bg color green

if Flag='R' then display bg color red

if Flag='B' then display bg color blue

can you please help me

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 09, 2021 06:32 AM
on Oct 12, 2021 11:08 PM

Hi mahesh213,

Refer below example.

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult GetCustomers()
    {
        List<Customer> customers = new List<Customer>();
        customers.Add(new Customer { Id = 1, Name = "John Hammond", Flag = "G" });
        customers.Add(new Customer { Id = 2, Name = "Mudassar Khan", Flag = "R" });
        customers.Add(new Customer { Id = 3, Name = "Suzanne Mathews", Flag = "B" });
        customers.Add(new Customer { Id = 4, Name = "Robert Schidner", Flag = "R" });
        return Json(customers, JsonRequestBehavior.AllowGet);
    }

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Flag { get; set; }
    }
}

View

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.default-v2.min.css" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/angular.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/kendo.all.min.js"></script>
    <script type="text/javascript">
        var app = angular.module("MyApp", ["kendo.directives"]);
        app.controller("MyController", function ($scope, $http, $window, $interval) {
            $scope.mainGridOptions = {
                dataSource: {
                    transport: {
                        read: { url: "/Home/GetCustomers", cache: false }
                    },
                    pageSize: 10,
                    serverPaging: false,
                    serverSorting: false
                },
                columns: [
                    { field: "Id", title: "Id", width: '40px' },
                    { field: "Name", title: "Name", width: '200px' },
                    { field: "Flag", title: "Flag", width: '60px' },
                    {
                        title: 'Action',
                        command: [{ name: "edit", text: "Edit", iconClass: "k-icon k-i-hyperlink-open" }]
                    }
                ],
                dataBound: function (e) {
                    var rows = e.sender.tbody.children();
                    for (var j = 0; j < rows.length; j++) {
                        var row = $(rows[j]);
                        var dataItem = e.sender.dataItem(row);
                        var flag = dataItem.get("Flag");
                        row[0].cells[2].style.color = "White";
                        if (flag == "G") {
                            row[0].cells[2].style.backgroundColor = "Green";
                        } else if (flag == "R") {
                            row[0].cells[2].style.backgroundColor = "Red";
                        } else if (flag == "B") {
                            row[0].cells[2].style.backgroundColor = "Blue";
                        }
                    }
                }
            };
        })
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    <kendo-grid k-options="mainGridOptions" id="tblCustomers"></kendo-grid>
</body>
</html>

Screenshot