Show Hide KendoGrid rows based on CheckBox selection using AngularJS in ASP.Net MVC

Last Reply 2 months ago By dharmendr

Posted 2 months ago

Hi,

Currently my requirement is that based upon check box selection need to display records on grid else hide records on grid

If i have checked check box then i need to display grid view records else disappear grid view records 

Note: Don't want to disappear headings of grid view

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <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, $window, $http) {
            $scope.customersGridOptions = {
                dataSource: {
                    transport: { read: "Home/GetCustomers" },
                    pageSize: 5,
                    serverPaging: false,
                    serverSorting: false
                },
                sortable: true,
                pageable: true,
              
                columns: [{ field: "CustomerId", title: "ID" },
                        { field: "Name", title: "Name" },
                        { field: "Country", title: "Country"}]
            };
            
        })
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<input type="checkbox" ngmodel="ShowHideChildGrid()" /><br />

    <kendo-grid options="customersGridOptions">
        
    </kendo-grid>
</body>
</html>

Could you please help me

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago

Hi mahesh213,

Check this example. Now please take its reference and correct your 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 { CustomerId = 1, Name = "John Hammond", Country = "United States" });
        customers.Add(new Customer { CustomerId = 2, Name = "Mudassar Khan", Country = "India" });
        customers.Add(new Customer { CustomerId = 3, Name = "Suzanne Mathews", Country = "France" });
        customers.Add(new Customer { CustomerId = 4, Name = "Robert Schidner", Country = "Russia" });
        return Json(customers, JsonRequestBehavior.AllowGet);
    }

    public JsonResult ClearCustomers()
    {
        List<Customer> customer = new List<Customer>();
        return Json(customer, JsonRequestBehavior.AllowGet);
    }

    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
        public string Country { 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) {
            $scope.IsChecked = true;
            $scope.customersGridOptions = {
                dataSource: {
                    transport: { read: "/Home/GetCustomers" },
                    pageSize: 5,
                    serverPaging: false,
                    serverSorting: false
                },
                sortable: true,
                pageable: true,
                columns: [
                    { field: "CustomerId", title: "ID" },
                    { field: "Name", title: "Name" },
                    { field: "Country", title: "Country" }
                ]
            }

            $scope.ShowHideGrid = function () {
                if (!$scope.IsChecked) {
                    $("#tblGrid").data("kendoGrid").dataSource.data([]);
                } else {
                    $('#tblGrid').data('kendoGrid').dataSource.read();
                    $('#tblGrid').data('kendoGrid').refresh();
                }
            };
        })
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    <input type="checkbox" ng-click="ShowHideGrid()" ng-model="IsChecked" /><br />
    <kendo-grid options="customersGridOptions" id="tblGrid">
    </kendo-grid>
</body>
</html>

Screenshot