Hello World with KendoUI & AngularJS – Code Example

KendoUI AngularJS

This article presents code samples and related concepts for what would it take for someone to get started with KendoUI and AngularJS. The code below is demonstrated on this page, http://hello-angularjs.appspot.com/kendoui-helloworld. Following will be discussed in this article:

  • Key CSS/JS libraries from KendoUI and AngularJS
  • AngularJS Code Representing Inclusion of Kendo.Directives Module
  • Take away code to get started quickly

 

Key CSS/JS KendoUI & AngularJS Libraries

Following are important CSS/JS libraries that need to be included:

  • kendo.common.min.css: This is KendoUI common CSS file that gets used across different themes.
  • kendo.default.min.css (Web Themes): This represents default web theme for KendoUI. One could use some of the following available themes. All that is required to be done with CSS is include file in this format: kendo.<themename>.min.css
    • Flat
    • Metro
    • MetroBlack
    • Uniform
    • Default
    • Bootstrap
    • MoonLight
    • Silver
    • HighContrast
    • Textures
    • Black
    • BlueOpal
  • jquery.min.js: This is JQuery JS library which is key for KendoUI widgets to work.
  • kendo.all.min.js: This is Kendo JS library required for different KendoUI widgets
  • angular.min.js: This is core AngularJS JS library.
  • kendo.angular.min.js: This is AngularJS library by KendoUI defining kendo directives

 

Code Snippets Representing Above Libraries

Note that all of the libraries are retrieved from CDNs. Thus, you could just copy and paste below, place them within <head> tag and get started.

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link href="//cdn.kendostatic.com/2014.2.716/styles/kendo.common.min.css" rel="stylesheet" />
<link href="//cdn.kendostatic.com/2014.2.716/styles/kendo.flat.min.css" rel="stylesheet" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdn.kendostatic.com/2014.2.716/js/kendo.all.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<script src="//cdn.kendostatic.com/2014.2.716/js/kendo.angular.min.js"></script>

 

AngularJS Code Representing Inclusion of Kendo.Directives Module

Pay attention to some of the following in the code below:

  • Dependency on “kendo.directives” module when loading helloKendo App.
  • Model “countries” defined on $scope object which is used to populate the Kendo DropDownList widget in the view. Within the “countries” variable defination, note that definition of dataSource, optionLabel, change variable in JSON format
var helloKendo = angular.module( "helloKendo", ["kendo.directives"] );
helloKendo.controller( "HelloCtrl", ['$scope', function( $scope ){
  $scope.firstname = "Calvin Hobbes";
  $scope.countries = {
      dataSource: new kendo.data.DataSource({
        data: [ "Afganistan", "India", "Ireland", "Nepal", "Pakistan", "Sri Lanka", "UK", "USA" ]
      }),
      optionLabel: "Select A Country",
      change: function(e) {
        console.log(e.sender.text());
      }
    };
}]);

 

Hello World Code Example with KendoUI & AngularJS

Pay attention to Angular directive “k-options” in select element assigned to “countries” model. It asks Angular to create
elements for each data element defined in the Controller. Note that the data source for “countries” could be easily populated with a REST API call to server.

Also, note that the usage of Bootstrap CSS library. Somehow, I was unable to find good CSS classes with Kendo using which I could create a container and define form elements in a decent manner. There are CSS elements like k-separator, k-content, k-group etc, but it did not make the page look like what got easily achieved with Bootstrap. However, as I am rookie to KendoUI, please suggest if there are ways to make a great looking page with KendoUI.

The code below is demonstrated at http://hello-angularjs.appspot.com/kendoui-helloworld.

<!DOCTYPE html>

<html ng-app="helloKendo">
<head>
  <title>HelloKendo</title>
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
  <link href="//cdn.kendostatic.com/2014.2.716/styles/kendo.common.min.css" rel="stylesheet" />
  <link href="//cdn.kendostatic.com/2014.2.716/styles/kendo.flat.min.css" rel="stylesheet" />
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="//cdn.kendostatic.com/2014.2.716/js/kendo.all.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
  <script src="//cdn.kendostatic.com/2014.2.716/js/kendo.angular.min.js"></script>
  <script>
    var helloKendo = angular.module( "helloKendo", ["kendo.directives"] );
    helloKendo.controller( "HelloCtrl", ['$scope', function( $scope ){
      $scope.firstname = "Calvin Hobbes";
      $scope.countries = {
        dataSource: new kendo.data.DataSource({
          data: [ "Afganistan", "India", "Ireland", "Nepal", "Pakistan", "Sri Lanka", "UK", "USA" ]
        }),
        optionLabel: "Select A Country",
        change: function(e) {
          console.log(e.sender.text());
        }
      };
    }]);
  </script>
</head>
<body ng-controller="HelloCtrl">
<div class="container">
  <div class="page-header" style="margin: 0">
				<!-- Heading goes here -->
				<h1>Hello, {{firstname }}</h1>
		</div>
  <div class="k-content" style="padding:20px">
    <form class="form-horizontal">
      <div class="form-group">
          <label for="firstname"  class="col-md-2 control-label">Type your name:</label>
          <div class="col-md-4">
            <input id="firstname" name="firstname" class="k-textbox col-md-6" ng-model="firstname" required/>
          </div>
      </div>
      <div class="form-group">
        <label for="country" class="col-md-2 control-label">Your Country:</label>
        <div class="col-md-4">
          <select id="country" kendo-drop-down-list k-options="countries"></select>
        </div>
      </div>
    </form>
  </div>
</div>
</body>
</html>

[adsenseyu1]

Ajitesh Kumar
Follow me

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com
Posted in Javascript, UI. Tagged with , .

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *