Introduction
This article will show you how to get the kendo UI support to work with its widget using AngularJS.
This article flow as follows,
- Creating an ASP.NET Web API application.
- Creating a Controller.
- Testing the REST API
- Implementing the Kendo Detail Grid using AngularJS with the REST API.
Create an empty Web API application using an installed web template in Visual Studio as in the following figures:
Creating a Model Classes:
In the Solution Explorer, right click the model folder, Add-> Class and name it as Department
Department.cs
- public class Department
- {
- [Required]
- public int DepartmentID { get; set; }
- [Required]
- public string DepartmentName { get; set; }
-
-
- }
Add another class and name it Employee
Employee.cs
- public class Employee
- {
-
- public int EmployeeID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public int DepartmentID { get; set; }
- public Department Department { get; set; }
- }
Creating a Context Class:
Add one more class in the Model and name it DetailGridContext which is our Entity framework code first context.
DetailGridContext.cs
- public class DetailGridContext : DbContext
- {
-
- public DetailGridContext() : base("name=DetailGridContext")
- {
- }
-
- public System.Data.Entity.DbSet<DetailGrid.Models.Department> Departments { get; set; }
-
- public System.Data.Entity.DbSet<DetailGrid.Models.Employee> Employees { get; set; }
- }
Seed the Database:
Now, open the Package manager console and run the following commands,
- Enable-migrations
This will add the folder called migration with code file named configuration.cs
-
Add-Migration Initial
Check your Web config file and ensure the SQL connection string,
Open configuration.cs file under migration folder and add the following code in seed method,
Configuration.cs
- protected override void Seed(DetailGrid.Models.DetailGridContext context)
- {
-
- context.Departments.AddOrUpdate(new Department { DepartmentID = 4, DepartmentName = "Development" },
- new Department { DepartmentID = 5, DepartmentName = "Testing" }, new Department { DepartmentID = 6, DepartmentName = "Infrastructure" });
-
-
-
- context.Employees.AddOrUpdate(new Employee { EmployeeID = 7, FirstName = "Bob", LastName = "Ross", DepartmentID = 4 },
- new Employee { EmployeeID = 8, FirstName = "James", LastName = "Ross", DepartmentID = 4 },
- new Employee { EmployeeID = 9, FirstName = "Steve", LastName = "Smith", DepartmentID = 4 },
- new Employee { EmployeeID = 10, FirstName = "Dave", LastName = "Tucker", DepartmentID = 5 },
- new Employee { EmployeeID = 11, FirstName = "Pradeep", LastName = "Raj", DepartmentID = 6 },
- new Employee { EmployeeID = 12, FirstName = "Shivraj", LastName = "Kumar", DepartmentID = 7 },
- new Employee { EmployeeID = 13, FirstName = "Raja", LastName = "Sekar", DepartmentID = 7 });
- }
3. Update-Database
This will run the seed method,
Check you database which is created,
Department Table
Employee Table
Please refer to my previous
article (
ASP.NET Web API With Entity Framework 6 Code First Technique - Part 1) to get more detail about Entity Framework code first technique.
Creating WEB API Controller:
Note: Before adding the controller build your application once.
In Solution Explorer, right-click the Controller folder. Select Add -> Controller and name it DepartmentController.cs
DepartmentController.cs
- [RoutePrefix("api/Department")]
- public class DepartmentsController : ApiController
- {
- private DetailGridContext db = new DetailGridContext();
-
-
- [Route("List")]
- public IQueryable<Department> GetDepartments()
- {
- return db.Departments;
- }
-
- [HttpGet]
- [Route("EmployeeDetails/{deptId}")]
- public HttpResponseMessage EmployeeDetails(int deptId)
- {
- try
- {
-
- var _emp = from emp in db.Employees where emp.DepartmentID == deptId select emp;
- return Request.CreateResponse(HttpStatusCode.OK, _emp, Configuration.Formatters.JsonFormatter);
-
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);
- }
- }
-
- }
GetDepartment action method is used to get the list of department and the EmployeeDetails action method is used to get the list of Employees based on department id.
API Test
GET: /api/Department/List – Get the Department records.
GET: /api/Department/EmployeeDetails/{departmentId} – Get the Employee records based on deppartment
The API is working fine, now it's ready to consume.
Implementing the Kendo Detail Grid using AngularJS and the REST API
Creating a HTML page
Create a new HTML page in the project.
Design:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Untitled</title>
-
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.common.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.rtl.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.default.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.412/styles/kendo.mobile.all.min.css">
-
- <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.412/js/angular.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.412/js/jszip.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.412/js/kendo.all.min.js"></script>
- </head>
- <body>
- <div id="example" ng-app="KendoDemos">
- <div ng-controller="MyCtrl">
- <kendo-grid options="mainGridOptions">
- <div k-detail-template>
- <kendo-tabstrip>
- <ul>
- <li class="k-state-active">Employee information</li>
-
- </ul>
- <div>
- <div kendo-grid k-options="detailGridOptions(dataItem)"></div>
- </div>
-
- </kendo-tabstrip>
- </div>
- </kendo-grid>
-
-
- </div>
- </div>
-
- <script>
- angular.module("KendoDemos", [ "kendo.directives" ])
- .controller("MyCtrl", function($scope){
- $scope.mainGridOptions = {
- dataSource: {
- type: "json",
- transport: {
- read: "/api/Department/List"
- },
- pageSize: 5,
- serverPaging: true,
- serverSorting: true
- },
- sortable: true,
- pageable: true,
- dataBound: function() {
- this.expandRow(this.tbody.find("tr.k-master-row").first());
- },
- columns: [{
- field: "DepartmentID",
- title: "DepartmentID",
- width: "120px"
- },{
- field: "DepartmentName",
- title: "Department Name",
- width: "120px"
- }]
- };
-
- $scope.detailGridOptions = function(dataItem) {
- return {
- dataSource: {
- type: "json",
- dataType:"GET",
- transport: {
- read: "/api/Department/EmployeeDetails/"+ dataItem.DepartmentID
- },
- serverPaging: true,
- serverSorting: true,
- serverFiltering: true,
- pageSize: 5,
-
- },
- scrollable: false,
- sortable: true,
- pageable: true,
- columns: [
- { field: "EmployeeID", title:"ID", width: "56px" },
- { field: "FirstName", title:"FirstName", width: "110px" },
- { field: "LastName", title:"LastName" },
-
- ]
- };
- };
- })
- </script>
- </body>
- </html>
From the above code it is obvious that we have injected the kendo tabstrip with the tab Employee Information. The Employee Information is populated based on the selection of department in the Grid.
Result:
Conclusion:
We have seen how to work with Kendo detail grid using AngularJS, which is really useful to build an application with ease. I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcomed.
Read more articles on jQuery: