Using Telerik Grid for ASP.NET MVC without any server-side code

by ASP.NET MVC Team | Comments 15

Since all Telerik UI components for ASP.NET MVC are actually jQuery plugins one can use them without any server-side code (or ASP.NET MVC). In this blog post I will show how to use the grid and bind it to Twitter. Here are the required steps:

 

  1. Register the two CSS files to style the grid:
    <head>
     <link rel="stylesheet" type="text/css" href="telerik.common.min.css" />
     <link rel="stylesheet" type="text/css" href="telerik.vista.min.css" />
    </head>
  2. Then add the JavaScript files:
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript" src="telerik.common.min.js"></script>
      
    <script type="text/javascript" src="telerik.grid.min.js"></script>
  3. Add the minimum HTML required by the grid:
    <div id="Grid" class="t-widget t-grid">
    <div class="t-grid-header">
    <div class="t-grid-header-wrap">
    <table cellspacing="0">
    <colgroup>
    <col style="width:100px;" />
    <col style="width:80px;" />
    <col />
    </colgroup>
    <tr>
    <th class="t-header" scope="col">Author</th>
    <th class="t-header" scope="col">Avatar</th>
    <th class="t-header t-last-header" scope="col">Post</th>
    </tr>
    </table>
    </div>
    </div>
    <div class="t-grid-content" style="height:400px;">
    <table cellspacing="0">
    <colgroup>
    <col style="width:100px;" />
    <col style="width:80px;" />
    <col />
    </colgroup>
    <tbody>
    <tr class="t-no-data">
    <td colspan="3"></td>
    </tr>
    </tbody>
    </table>
    </div>
    <div class="t-grid-footer">
    <table cellspacing="0">
    <colgroup>
    <col style="width:100px;" />
    <col style="width:80px;" />
    <col />
    </colgroup>
    <tr>
    <td class="t-footer" colspan="3">
    <div class="t-status">
    <a class="t-icon t-refresh" href="#"></a>
    </div>
    </td>
    </tr>
    </table>
    </div>
    </div>

  4. Initialize the jQuery plugin of the grid:
    $(document).ready(function() {
    $("#Grid").tGrid({
    columns:
    [
    {"name":null,"type":""},
    {"name":null,"type":""},
    {"name":"text","type":"String"}
    ],
    pageSize:0,
    onDataBinding:onDataBinding,
    onRowDataBound:onRowDataBound
    });
    });

  5. And finally bind it to the Twitter service:
    function onRowDataBound(e) {
    var row = e.row;
    var dataItem = e.dataItem;

    // update `Author` cell with template
    row.cells[0].innerHTML = [
    '<a class="t-link" href="http://www.twitter.com/', dataItem.from_user, '">',
    dataItem.from_user,
    '</a>'
    ].join('');

    // update `Avatar` cell with template
    row.cells[1].innerHTML = [
    '<img width="48" height="48"',
    ' src="', dataItem.profile_image_url,
    '" alt="', dataItem.from_user, '" />'
    ].join('');
    }

    function onDataBinding(e) {
    var grid = $(this).data('tGrid');

    $('.t-status .t-icon', grid.element).addClass('t-loading');

    // call the twitter search api
    $.ajax({
    url: 'http://search.twitter.com/search.json',
    contentType: 'application/json; charset=utf-8',
    type: 'GET',
    dataType: 'jsonp',
    error: function(xhr, status) {
    alert(status);
    },
    data: {
    q: $('#searchText').val()
    },
    success: function(result) {
    grid.dataBind(result.results);
    $('.t-status .t-icon', grid.element).removeClass('t-loading');
    }
    });
    }

    I have attached a zip file with a runnable demo. Check it out!

 

,
Team Leader,
Kendo UI Team

15 Comments

Brook
Love it!  Thanks for the example greatly appreciated.
chris
Very cool, I didn't realize the MVC code would be that independent.  As someone who loves jQuery and Telerik, but has not moved to MVC, this is nice!
 
Is it possible to use these new MVC server-side Telerik objects without MVC? For example, could we utilize the objects in the standard page_load and web/page methods?

I'm not a fan of webforms or MS's ajax, so I've been switching to jQuery UI and similar items.  I write wrapper server objects to emit json and html.  It works, but its a lot of work that I'd prefer to pay Telerik to do.
Atanas Korchev
Unfortunately it is not possible to use the server-side code without MVC. The components rely on quite a few MVC specific objects - ViewContext for example.
novajoe
Nice! I am stoked about more Telerik controls being available for MVC!
Sean

looks really cool! can this work with paging and sorting?

Atanas Korchev
@Sean
Yes it can but the twitter search API does not support that AFAIK. You can check however the facebook example which supports paging: http://demos.telerik.com/aspnet-mvc/grid/externalservicefacebook
Sean
thanks a lot!
Sean
thanks!
Sean

Atanas, how can i display the dates on a grid?

I currently get: /Date(1258416000000+0000)/
is there a reference of the jQuery plugging?

Sean
sebastian
Hi, I´ve been checking telerik controls and found it amazing. I read on your website the possibility to integrate those control in any environment based on the fact that they are built over jquery. Is this possible?, to put it more clear... it is possible to acquire all these controls from http://demos.telerik.com/aspnet-ajax/ and from http://demos.telerik.com/aspnet-mvc and implement them directly into a php environment?.
thank you so much for your time answering, it would be very appreciated. brgds.
sebastian.
Sean
Hi, Can you do the same with the Telerik Panel Bar control?
Sean
Sean

Hi, another question, is there built in functionality in the grid to show all records (when using paging)?

Sean

jeremy
Hi Atanas, its possible to use all mvc controls in other programming langauges or just the ones that dont need controller classes?. as an example on the panelbar:

        public ActionResult FirstLook(string expandMode)
       
{
           
ViewData["expandMode"] = expandMode ?? "Multiple";
           
return View();


its possible to change on php and use it out of the box the UI?.
brgds.
ken
Hi Atanas:
    It's nice!
    but I can't check out the demo ,can you give me an email with the demo.thank you

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by ASP.NET MVC Team - or - read latest articles in Developer Tools