Introduction
In my last
article, we discussed how to work with MultiColumnComboBox and now, we very well know that Kendo Multicolumn combo box control is used to display the data in multi-column Views within the combo box. In this article, we are going to see how to do the grouping and work with events in KendoMultiColumnComboBox. Before moving to the grouping and events customization, let's add a few more records in the database to get a clear view.
Execute the below query to get the same records.
- USE [CSharpCorner]
- GO
- SET IDENTITY_INSERT [dbo].[Employee] ON
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (1, N'Gnanavel Sekar', N'India', N'Sr. Software Engineer', N'gnanavelsekar')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (2, N'Gokul', N'UK', N'Sr. Software Engineer', N'ManIcon')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (3, N'Ramar', N'India', N'Technical Lead', N'ManIcon')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (10002, N'Subash', N'India', N'Software Developer', N'ManIcon')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (10003, N'Ananad', N'USA', N'Software Developer', N'ManIcon')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (10004, N'Robert', N'China', N'Sr.Software Engineer', N'ManIcon')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (10005, N'Pandi', N'China', N'Sr.Software Engineer', N'ManIcon')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (10006, N'Kaviarasan', N'Japan', N'Sr.Software Engineer', N'ManIcon')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Country], [Role], [ImageName]) VALUES (10007, N'Karthik', N'USA', N'Software Engineer', N'ManIcon')
- GO
- SET IDENTITY_INSERT [dbo].[Employee] OFF
- GO
Run your application.
In the above image, you can see that we have got the list of employee data but it looks somewhat weird, right?
MultiColumnComboBox Grouping
Now, let's work on grouping by the employee's country. For that, we have to use the below syntax.
Syntax
- group: { field: "columnname" }
Now, we shall mention our column is country. We just need to apply this group to our data source as below.
- dataSource = new kendo.data.DataSource({
- transport: {
- read: {
- url: "/KendoUI/LoadEmployee",
- dataType: "json",
- }
- },
- group: {
- field: "Country"
- }
- });
For a clear view:
Run your application. In the below image, you can see that the employees have been grouped by their respective country.
Working with MultiColumnComboBox events
Here, we will get the selected row values using an onSelect event. For that, let's write a custom function as below.
- function onSelect(e) {
- if (e.dataItem) {
- var dataItem = e.dataItem;
- alert("Name:" + dataItem.Name + " ,Country:" + dataItem.Country + ", Role: " + dataItem.Role + ", Id: " + dataItem.ID);
- }
- };
Now, let us call this onSelect custom function in our multiCoumnComboBox.
- $("#employees").kendoMultiColumnComboBox({
- dataTextField: "Name",
- dataValueField: "ID",
- height: 400,
- columns: [{
- field: "ContactName",
- title: "Emp Name",
- template: "<div class='customer-photo'" + "style='background-image: url(../Content/Images/#:data.ImageName#.jpg);'></div>" + "<span class='customer-name'>#: Name #</span>",
- width: 170
- },
-
- {
- field: "Role",
- title: "Role",
- width: 160
- }, {
- field: "Country",
- title: "Country",
- width: 90
- }
- ],
- footerTemplate: 'Total #: instance.dataSource.total() # items found',
- filter: "contains",
- filterFields: ["Name", "Country", "Role"],
- dataSource: dataSource,
- select: onSelect,
- });
Run your application. In the below image, we get the selected row details using select event. Similarly, we can perform multiple operations using Change, Close, Open, Filtering, and databound events.
Complete View
- @ {
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- } < link rel = "stylesheet"
- href = "https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" / > < link rel = "stylesheet"
- href = "https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" / > < link rel = "stylesheet"
- href = "https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" / > < script src = "https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js" > < /script> < script src = "https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js" > < /script> < br / > < div id = "example"
- class = "col-lg-3" > < div class = "demo-section k-content" > < input id = "employees"
- style = "width: 100%;" / > < /div> < /div> < style type = "text/css" > .customer - photo {
- display: inline - block;
- box - shadow: inset 0 0 1 px #999, inset 0 0 10px rgba(0,0,0,.2);
- margin: 0 10px 0;
- width: 20px;
- height: 20px;
- border-radius: 50%;
- background-size: 100%;
- background-repeat: no-repeat;
- vertical-align: middle;
- }
- </style>
- <script>
- $(document).ready(function () {
- dataSource = new kendo.data.DataSource({
- transport: {
- read: {
- url: "/KendoUI/LoadEmployee",
- dataType: "json",
- }
- },
- group: {
- field: "Country"
- }
- });
- $("#employees").kendoMultiColumnComboBox({
- dataTextField: "Name",
- dataValueField: "ID",
- height: 400,
- columns: [{
- field: "ContactName",
- title: "Emp Name",
- template: "<div class='customer-photo'" + "style='background-image: url(../Content/Images/#:data.ImageName#.jpg);'></div>" + "<span class='customer-name'>#: Name #</span>",
- width: 170
- },
-
- {
- field: "Role",
- title: "Role",
- width: 160
- }, {
- field: "Country",
- title: "Country",
- width: 90
- }
- ],
- footerTemplate: 'Total #: instance.dataSource.total() # items found',
- filter: "contains",
- filterFields: ["Name", "Country", "Role"],
- dataSource: dataSource,
- select: onSelect,
- });
-
- function onSelect(e) {
- if (e.dataItem) {
- var dataItem = e.dataItem;
- alert("Name:" + dataItem.Name + " ,Country:" + dataItem.Country + ", Role: " + dataItem.Role + ", Id: " + dataItem.ID);
- }
- };
- });
- < /script>
Refer to the attached project for reference. I have attached the demonstrated project without a package.
Summary
In this article, we have seen how to group the MultiColumnComboBox values and work with events in ASP.NET MVC5 using Kendo UI and EF.
I hope it helps you out. Your valuable feedback and comments about this article are always welcome.