Introduction
This article tells you how to use the drop-down list as a custom editor in Kendo Grid with remote data.
Content
- Remote Data Source for Kendo Grid
- Building a Kendo Grid with remote datasource
- Custom Editor in Kendo Grid
Remote Data Source for Kendo Grid
I’m using my existing ASP.NET Core API application which I used in my last article. You can go through my previous article to know how to create an ASP.NET Core API.
Model Class- TechnologyList.cs
- public class TechnologyList
- {
- public TechnologyList(int value, string text)
- {
-
- this.value = value;
- this.text = text;
- }
- public TechnologyList(int value, string text,Category category)
- {
-
- this.value = value;
- this.text = text;
- this.Category = category;
- }
- public int value { get; set; }
- public string text { get; set; }
-
- public Category Category { get; set; }
- }
-
- public class Category
- {
- public int CategoryID { get; set; }
- public string CategoryName { get; set; }
- }
Controller- TechologiesController.cs
- [HttpGet]
- [Route("GetTechList")]
-
- public List<TechnologyList> GetTech()
- {
- try
- {
- List<TechnologyList> _TechList = new List<TechnologyList>();
-
- _TechList.Add(new TechnologyList(1, "C#", new Category { CategoryID = 1, CategoryName = "Programming" }));
- _TechList.Add(new TechnologyList(2, "F#", new Category { CategoryID = 2, CategoryName = "Programming" }));
- _TechList.Add(new TechnologyList(3, "HTML", new Category { CategoryID = 3, CategoryName = "Web Technology" }));
- _TechList.Add(new TechnologyList(4, "JavaScript", new Category { CategoryID = 4, CategoryName = "Client Side Scripting" }));
- _TechList.Add(new TechnologyList(5, "Azure", new Category { CategoryID = 5, CategoryName = "Cloud" }));
- return _TechList;
- }
- catch (Exception ex)
- {
- List<TechnologyList> _Tech = null;
- return _Tech;
- }
- }
-
- [HttpGet]
- [Route("GetCategory")]
-
- public List<Category> GetCategories()
- {
- try
- {
- List<Category> categories = new List<Category>();
- categories.Add(new Category { CategoryID = 1, CategoryName = "Programming" });
- categories.Add(new Category { CategoryID = 2, CategoryName = "Web Technology" });
- categories.Add(new Category { CategoryID = 3, CategoryName = "Client Side Scripting" });
- categories.Add(new Category { CategoryID = 4, CategoryName = "Cloud" });
- return categories;
- }
- catch (Exception ex)
- {
- List<Category> categories = null;
- return categories;
- }
- }
GetTech() Action is used to return a datasource for the grid. GetCategories() Action is used to return a datasource for the dropdownlist.
I am going to use the below API response to construct my remote data source for Kendo Grid:
- API - /api/Technologies/TechList
- Type - GET.
Test the APIs using POSTMAN.
The below API response is used to build datasource for drop-down list in Grid.
- API - /api/Technologies/GetCategory
- Type - GET
Test the APIs using POSTMAN.
Build a Kendo Grid with remote datasource
Create a new HTML page. In my case, I named it as KendoGridDropDown.html.
KendoGridDropDown.html
- <!DOCTYPE html>
- <html>
- <head>
-
- <style>
- html {
- font-size: 14px;
- font-family: Arial, Helvetica, sans-serif;
- }
- </style>
- <title></title>
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.common-material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.mobile.min.css" />
-
- <script src="https://kendo.cdn.telerik.com/2017.3.913/js/jquery.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2017.3.913/js/kendo.all.min.js"></script>
-
-
- </head>
- <body>
-
- <h4>Kendo Grid</h4>
- <div id="example">
- <div id="grid"></div>
-
- <script>
-
- $(document).ready(function() {
-
-
- var grid = $("#grid").kendoGrid({
- dataSource: {
- type: "json",
- transport: {
- read: "/api/Technologies/GetTechList"
-
- },
- schema: {
- model: {
- fields: {
- value: { type: "number" },
-
- text: { type: "string" },
-
- }
- }
- },
- pageSize: 20,
-
- },
- editable:true,
- pageable: {
- refresh: true,
- pageSizes: [20, 40, 60, 80,100],
-
- },
- height: 500,
- columns: [
-
- { field: "value", title: "S No", width: "130px" },
-
- { field: "text", title: "Technology", width: "130px" },
-
- ]
- }).data("kendoGrid");
-
-
- });
-
-
- </script>
-
-
- </div>
-
-
- </body>
- </html>
From the above code, you can notice that the Kendo Grid is constructed based on our remote datasource which is built from the API.
Result in Browser
Custom Editor in Kendo Grid
Now, let’s add a custom editor which is a dropdown list in the Kendo Grid using editor attribute in column definition of Kendo Grid.
KendoGridDropDown.html
- <!DOCTYPE html>
- <html>
- <head>
-
- <style>
- html {
- font-size: 14px;
- font-family: Arial, Helvetica, sans-serif;
- }
- </style>
- <title></title>
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.common-material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.mobile.min.css" />
-
- <script src="https://kendo.cdn.telerik.com/2017.3.913/js/jquery.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2017.3.913/js/kendo.all.min.js"></script>
-
-
- </head>
- <body>
-
- <h4>Kendo Grid with Custom Editor</h4>
- <div id="example">
- <div id="grid"></div>
-
- <script>
-
- $(document).ready(function() {
-
-
- var grid = $("#grid").kendoGrid({
- dataSource: {
- type: "json",
- transport: {
- read: "/api/Technologies/GetTechList"
-
- },
- schema: {
- model: {
- fields: {
- value: { type: "number" },
- category: { defaultValue: { categoryID: 1, categoryName: "Programming"} },
- text: { type: "string" },
-
- }
- }
- },
- pageSize: 20,
-
- },
- editable:true,
- pageable: {
- refresh: true,
- pageSizes: [20, 40, 60, 80,100],
-
- },
- height: 500,
- columns: [
-
- { field: "value", title: "S No", width: "130px" },
- { field: "category", title: "Category", width: "180px", editor: categoryDropDownEditor, template: "#=category.categoryName#" },
- { field: "text", title: "Technology", width: "130px" },
-
- ]
- }).data("kendoGrid");
-
-
- });
-
- function categoryDropDownEditor(container, options) {
- $('<input required name="' + options.field + '"/>')
- .appendTo(container)
- .kendoDropDownList({
-
- dataTextField: "categoryName",
- dataValueField: "categoryID",
- dataSource: {
- type: "json",
- transport: {
- read: "/api/Technologies/GetCategory"
- }
- }
-
- });
- }
- </script>
-
-
- </div>
-
-
- </body>
- </html>
categoryDropDownEditor editor function is used to load the datasource for Kendo dropdownlist.
Reference
https://demos.telerik.com/kendo-ui/grid/editing-custom
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.