Render JSON Data To HTML Using Itemtemplate.JS

Introduction

This article is an introduction on ItemTemplateJS.

Enter detail and see detail

Well if you are a .NET developer, you must have got an idea of what this JavaScript is all about. The ItemTemplate.js helps one bind JSON objects to HTML with ease, it is as simple as binding a GridView or a Repeater, all you need is to supply JSON object to the datasource.

While using this JavaScript you might find to quite similar to AngularJS but its not Angular, I mean its not as huge as Angular.

ItemTemplate.js only renders data on to html.

Directions to use ItemTemplateJS

  1. HTML

    In the following examples the HTML has an attribute itemtemplate, basically used to identify the part of the html that has to be repeated and replaces all the elements within {{ }} in the itemtemplate.  
    1. @*For Table*@  
    2.    <table id="grid">  
    3.        <tr>  
    4.            <td>ID</td>  
    5.            <td>Name</td>  
    6.            <td>Age</td>  
    7.            <td>Gender</td>  
    8.            <td>Location</td>  
    9.        </tr>  
    10.        <tr itemtemplate="true">  
    11.            <td>  
    12.                <a href="javascript:EditPerson({{Id}});">  
    13.                    {{Id}}  
    14.                </a>  
    15.            </td>  
    16.            <td>  
    17.                {{Name}}  
    18.            </td>  
    19.            <td>  
    20.                {{Age}}  
    21.            </td>  
    22.            <td>  
    23.                {{Gender}}  
    24.            </td>  
    25.            <td>  
    26.                {{Location}}  
    27.            </td>  
    28.        </tr>  
    29.    </table>  
    30.   
    31.    @*For Select*@  
    32.    <select id="ddlSelect">  
    33.        <option value="0">Select</option>  
    34.        <option itemtemplate="true" value="{{Id}}">  
    35.            {{Name}}/{{Age}}}/{{Gender}}/{{Location}}  
    36.        </option>  
    37.    </select>  
    38.   
    39.    @*For List*@  
    40.    <ol id="oList">  
    41.        <li itemtemplate="true" data-value="{{Id}}">  
    42.            {{Name}} {{Age}}} {{Gender}} {{Location}}  
    43.        </li>  
    44.    </ol>  
    45.   
    46.    @*For AnyCustomHTML*@  
    47.    <div id="divRpt">  
    48.        <div itemtemplate="true">  
    49.            {{index}}  
    50.            <br />  
    51.            {{Name}} {{Age}}} {{Gender}} {{Location}}  
    52.        </div>  
    53.    </div>  
  2. JQuery

    Now to bind the control all you need to do is the following:
    1. $.post('/Home/SelectPeople''{}', function(result) {  
    2.     //Applying the result to the HTML   
    3.     //the result object is applied to option dataSource   
    4.   
    5.     $('#grid').itemTemplate({  
    6.         dataSource: result  
    7.     });  
    8.     $('#ddlSelect').itemTemplate({  
    9.         dataSource: result  
    10.     });  
    11.     $('#oList').itemTemplate({  
    12.         dataSource: result  
    13.     });  
    14.     $('#divRpt').itemTemplate({  
    15.         dataSource: result  
    16.     });  
    17. });  

So here we have finished binding an HTML element with data using the ItemTemplateJS.

Note: This JS requires JQuery 1.6 or higher

Well the ItemTemplateJS isn't that perfect, like for example it doesn't work with nested itemTemplates and I have been working on it for quite sometime. So if anyone find any imperfections or any scope for enhancements please feel free to comment.

That's all for this article.