Dynamically change KendoGrid column value based on condition using AngularJS in ASP.Net MVC

mahesh213
 
on Sep 29, 2021 05:21 AM
Sample_115633.zip
1007 Views

Hi,

In below forum i am going to do some modifications

Display TextBox background color inside KendoGrid based condition using AngularJS in ASP.Net MVC

my requirement is that in status column i need to display value based upon Name filed

in above forum Status field is editable but my requirement is that  it should be non editable i need to display same like name format

can you please help me

 template: "# if(Name == 'Mudassar Khan') {"
           + "# <input type='text' style='background-color:red;color:white;' value='Present' /> #"
           + "} else { "
           + "# <input type='text' style='background-color:blue;color:white;' value='Absent' /> #"
           + "} #"
}

in above code can you please remove input type and try to display value based upon name

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 29, 2021 05:27 AM
on Oct 02, 2021 01:14 AM

Hi mahesh213,

Refer below code.

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", Status = "" });
        customers.Add(new Customer { Id = 2, Name = "Mudassar Khan", Status = "" });
        customers.Add(new Customer { Id = 3, Name = "Suzanne Mathews", Status = "" });
        customers.Add(new Customer { Id = 4, Name = "Robert Schidner", Status = "" });
        return Json(customers, JsonRequestBehavior.AllowGet);
    }
 
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Status { 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) {
            ApplyKendoGrid();
            function ApplyKendoGrid() {
                $scope.mainGridOptions = {
                    dataSource: {
                        transport: {
                            read: { url: "/Home/GetCustomers", cache: false }
                        },
                        pageSize: 10,
                        serverPaging: false,
                        serverSorting: false
                    },
                    columns: [
                        { field: "Id", title: "Id", width: '30px' },
                        { field: "Name", title: "Name", width: '60px' },
                        {
                            field: "Status",
                            title: "Status",
                            width: 100,
                            template: "# if(Name == 'Mudassar Khan') {"
                                + "# <span style='background-color:red;color:white;'>Present</span> #"
                                + "} else { "
                                + "# <span style='background-color:blue;color:white;'>Absent</span> #"
                                + "} #"
                        }
                    ]
                };
            }
        })
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    <kendo-grid k-options="mainGridOptions" id="tblCustomers"></kendo-grid>
</body>
</html>

Screenshot