Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kendo UI Cookbook

You're reading from   Kendo UI Cookbook Over 50 recipes to help you rapidly build rich and dynamic user interfaces for web and mobile platforms.

Arrow left icon
Product type Paperback
Published in Jun 2014
Publisher
ISBN-13 9781783980000
Length 250 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sagar Ganatra Sagar Ganatra
Author Profile Icon Sagar Ganatra
Sagar Ganatra
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Kendo UI Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. The Kendo UI Application Framework FREE CHAPTER 2. The Kendo UI Grid 3. Kendo UI TreeView 4. Kendo UI Editor 5. Kendo UI PanelBar 6. Kendo UI File Uploader 7. Kendo UI Window 8. Kendo UI Mobile Framework 9. Kendo UI Mobile Widgets 10. Kendo UI DataViz 11. Kendo UI DataViz – Advance Charting Index

Using Source and Template binding with ViewModels to generate HTML content


In the previous recipe, data binding between View components and ViewModel attributes was explored. Binding can be performed not only on the value of the form elements, but also on the attributes, and also to hide or show the elements on the page. In this recipe, we will look at the source and the HTML binding in combination.

Consider a dropdown (to select an element) that you want to populate it with data (option tags). The data for these could be provided from a ViewModel object; use a template to generate these option tags.

How to do it…

Let's first construct a ViewModel object:

var viewModel = kendo.observable({
    optionsData: [
                   {optionValue:1, optionName: "Test1"},
                   {optionValue:2, optionName: "Test2"},
                   {optionValue:3, optionName: "Test3"}
                 ]
});

The ViewModel object here contains an optionsData attribute, which is a collection (array) of objects. Each object in optionsData contains two attributes, namely optionValue and optionName. The optionValue attribute will be used for the value attribute of the option tag, and optionName will be used for the HTML content to be shown in the dropdown. The template that contains the option tag binding is shown in the following code snippet:

<script id="select-template" type="text/x-kendo-template">
  <option data-bind=
    "attr: {value: optionValue}, html: optionName">				
  </option>
</script>

Here, the template with the id attribute set to select-template contains one option tag. The option tag contains the data-bind attribute whose value is attr: {value: optionValue}, html: optionName. We already saw the attribute binding in action in the previous recipe. The other binding specified here is html. This binding is used to generate the HTML content for the tag. These option tags should be bound to the ViewModel object and be encapsulated inside a select tag.

The select tag can refer to the template (mentioned previously) and bind to the ViewModel object using the source attribute:

<select data-template="select-template" 
  data-bind="source: optionsData">
</select>

Here, the select tag specifies the two attributes, namely data-template and data-bind. The value of the data-template attribute is the id attribute of the template (the script tag) that we defined earlier, that is, select-template. This instructs kendo to bind the template to the select tag, and the generated content would become the HTML content for this select tag.

The data-bind attribute specifies a binding source that refers to the optionData model attribute in ViewModel. Now, the only step pending is to bind View elements to ViewModel, which is attained by executing the following line of code:

kendo.bind($('select'),viewModel);

How it works…

The select tag specifies the source binding that refers to the optionsData model attribute. This will make the collection of objects available to all the tags inside it.

The data-template attribute that refers to the template in the page is provided with the collection of objects (optionsData) and the template is iterated for the number of entries in the array. In this example, the optionsData collection contains three objects and hence, the template is generated three times and inserted as the HTML content for the select tag. The generated content would look like the following code snippet:

<select data-template="select-template" 
  data-bind="source: optionsData">
  
  <option data-bind=
    "attr: {value: optionValue}, html: optionName"  
    value="1">
    Test1
  </option>
  
  <option data-bind=
    "attr: {value: optionValue}, html: optionName"  
    value="2">
    Test2
  </option>
  
  <option data-bind=
    "attr: {value: optionValue}, html: optionName"  
    value="3">
    Test3
  </option>
  
</select>

The data- attributes would remain as they are, and the generated content now has the value attribute and the HTML content. Note that the preceding code could have been accomplished by using a for statement in a hash-based template and then appending the generated output to the select tag. However, using a data-template attribute and source binding allows you to do this seamlessly and write more intuitive code.

This pattern can be applied in various situations. Another example includes generating list tags (li elements) and inserting them into an unordered or ordered list.

You have been reading a chapter from
Kendo UI Cookbook
Published in: Jun 2014
Publisher:
ISBN-13: 9781783980000
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime