Templates In Kendo Panel Bar

Introduction

Templates in Kendo panel bar is one of the newest features which has came through the recent ( 2017.1.118) release of kendo UI packages. This article explains how to implement the template in kendo UI panel bar, using ASP.NET WEB API. To explain it, I have created a RESTful GET Service using ASP.NET Web API, which is used to load the DataSource of Kendo panel bar. 

Prerequisites

Basic knowledge of ASP.NET Web API, jQuery, and Kendo UI.

Before going through this article, I strongly recommend you refer to my previous article where I have explained how to implement the kendo panel bar with remote databinding.

Remote Data Source for Kendo UI Panel Bar

I’m going to use the following remote datasource which is created in my previous article with the help of ASP.NET WEB API and Entity Framework.

API - /api/Employee/EmployeeList.

Type - GET.

Testing the APIs using POSTMAN.
 
   
                                             Figure 1 

API - /api/Employee/EmployeeList?EmployeeID=[EmployeeID]

Type - GET.

   
                                                            Figure 2 
 
   
                                                            Figure 3 

Templates in kendo Panel Bar

Templates are mainly used for customizing the information which is rendering each node in the panel bar. This can easily be achieved using the template property in the Kendo panel bar.

PanelBar.html

  1.  <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Kendo Panel Bar</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css">  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css">  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.default.min.css">  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css">  
  10.   
  11.     <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/angular.min.js"></script>  
  13.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/jszip.min.js"></script>  
  14.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  15. </head>  
  16. <body>  
  17.     <h3>Kendo Panel Bar</h3>  
  18.     <div id="example">  
  19.         <div class="demo-section k-content">  
  20.             <div id="panelbar"></div>  
  21.         </div>  
  22.          
  23.         <script>  
  24.   
  25.             var homogeneous = new kendo.data.HierarchicalDataSource({  
  26.                 transport: {  
  27.                     read: {  
  28.                         url: "/api/Employee/EmployeeList",  
  29.                         dataType: "json"  
  30.                     }  
  31.                 },  
  32.                 schema: {  
  33.                     model: {  
  34.                         id: "EmployeeID",  
  35.                         hasChildren: "HasEmployees"  
  36.                     }  
  37.                 }  
  38.             });  
  39.   
  40.             $("#panelbar").kendoPanelBar({  
  41.                 template:"#=item.FirstName# #=item.LastName# - #=item.Designation#" 
  42.                 dataSource: homogeneous,  
  43.                 dataTextField: "FirstName"  
  44.             });  
  45.         </script>  
  46.     </div>  
  47. </body>  
  48. </html>  

From the above code, you can observe that the template property is used while defining the kendo panel bar. The template is decorated/customized in the way which is used to display the firstname and lastname of the employee with his designation.

Result 
 
                                                                                 Figure 4

You can perform the same operation by creating the template in separate script.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Kendo Panel Bar</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css">  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css">  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.default.min.css">  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css">  
  10.   
  11.     <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/angular.min.js"></script>  
  13.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/jszip.min.js"></script>  
  14.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  15. </head>  
  16. <body>  
  17.     <h3>Kendo Panel Bar</h3>  
  18.     <div id="example">  
  19.         <div class="demo-section k-content">  
  20.             <div id="panelbar"></div>  
  21.         </div>  
  22.         <script id="panelbar-template" type="text/kendo-ui-template">  
  23.             #:item.FirstName# #: item.LastName# - #: item.Designation#  
  24. </script>  
  25.         <script>  
  26.   
  27.             var homogeneous = new kendo.data.HierarchicalDataSource({  
  28.                 transport: {  
  29.                     read: {  
  30.                         url: "/api/Employee/EmployeeList",  
  31.                         dataType: "json"  
  32.                     }  
  33.                 },  
  34.                 schema: {  
  35.                     model: {  
  36.                         id: "EmployeeID",  
  37.                         hasChildren: "HasEmployees"  
  38.                     }  
  39.                 }  
  40.             });  
  41.   
  42.             $("#panelbar").kendoPanelBar({  
  43.                 template: kendo.template($("#panelbar-template").html()),  
  44.                 dataSource: homogeneous,  
  45.                 dataTextField: "FirstName"  
  46.             });  
  47.         </script>  
  48.     </div>  
  49. </body>  
  50. </html>  
Result in browser
 

                                                               Figure 5

Template with condition

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Kendo Panel Bar</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css">  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css">  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.default.min.css">  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css">  
  10.   
  11.     <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/angular.min.js"></script>  
  13.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/jszip.min.js"></script>  
  14.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  15. </head>  
  16. <body>  
  17.     <h3>Kendo Panel Bar</h3>  
  18.     <div id="example">  
  19.         <div class="demo-section k-content">  
  20.             <div id="panelbar"></div>  
  21.         </div>  
  22.         <script id="panelbar-template" type="text/kendo-ui-template">  
  23.              
  24.             #if(item.HasEmployees){#   
  25.             #: item.FirstName# #: item.LastName# - #: item.Designation#  
  26.             #}  
  27.             else{#   
  28.             #: item.FirstName# #: item.LastName#  
  29.             #}#  
  30.         </script>  
  31.         <script>  
  32.   
  33.             var homogeneous = new kendo.data.HierarchicalDataSource({  
  34.                 transport: {  
  35.                     read: {  
  36.                         url: "/api/Employee/EmployeeList",  
  37.                         dataType: "json"  
  38.                     }  
  39.                 },  
  40.                 schema: {  
  41.                     model: {  
  42.                         id: "EmployeeID",  
  43.                         hasChildren: "HasEmployees"  
  44.                     }  
  45.                 }  
  46.             });  
  47.   
  48.             $("#panelbar").kendoPanelBar({  
  49.                 template: kendo.template($("#panelbar-template").html()),  
  50.                 dataSource: homogeneous,  
  51.                 dataTextField: "FirstName"  
  52.             });  
  53.         </script>  
  54.     </div>  
  55. </body>  
  56. </html>   

Based on the HasEmployee property value, the panel node value is customized. If the HasEmployee value is true it is going to display first name and last name with designation or else it is going to display the firstname and lastName.

Result In Browser 
 
                                                               Figure 6 

Template with HTML tags

PanelBar.html

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Kendo Panel Bar</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css">  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css">  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.default.min.css">  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css">  
  10.   
  11.     <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/angular.min.js"></script>  
  13.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/jszip.min.js"></script>  
  14.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  15. </head>  
  16. <body>  
  17.     <h3>Kendo Panel Bar</h3>  
  18.     <div id="example">  
  19.         <div class="demo-section k-content">  
  20.             <div id="panelbar"></div>  
  21.         </div>  
  22.         <script id="panelbar-template" type="text/kendo-ui-template">  
  23.              
  24.             #if(item.HasEmployees){#   
  25.             <b>#:item.FirstName# #: item.LastName# </b> -  <label style='font-style:italic'> #: item.Designation# </label>  
  26.             #}  
  27.             else{#   
  28.             <b> #: item.FirstName# #: item.LastName# </b>  
  29.             #}#  
  30.         </script>  
  31.         <script>  
  32.   
  33.             var homogeneous = new kendo.data.HierarchicalDataSource({  
  34.                 transport: {  
  35.                     read: {  
  36.                         url: "/api/Employee/EmployeeList",  
  37.                         dataType: "json"  
  38.                     }  
  39.                 },  
  40.                 schema: {  
  41.                     model: {  
  42.                         id: "EmployeeID",  
  43.                         hasChildren: "HasEmployees"  
  44.                     }  
  45.                 }  
  46.             });  
  47.   
  48.             $("#panelbar").kendoPanelBar({  
  49.                 template: kendo.template($("#panelbar-template").html()),  
  50.                 dataSource: homogeneous,  
  51.                 dataTextField: "FirstName"  
  52.             });  
  53.         </script>  
  54.     </div>  
  55. </body>  
  56. </html>  
Result in Browser
 

                                                           Figure 7 
 
I hope, you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all