Kendo UI Core Grid

For those of you who do not know, Telerik has released a core version of their Kendo UI product. The Kendo UI Core is open source, that means you can use it without spending the license costs of their flagship product. This is great news for small projects that don't need the heavy lifting that comes with the full Kendo UI Product.

I have recently been working with the Kendo UI Core grid component and thought I would share some of the things that I did to make my grids flexible and UX friendly.

First, let me show you the code required to get a Kendo UI Core grid working. All I have done on the MVC Layout page is include the required Kendo UI Code JavaScript and CSS files, jQuery and also Bootstrap. I use Bootstrap because it gives nice control formatting and Kendo UI Core actually has a Bootstrap theme so my styles are kept consistent throughout the entire site.

  1. @model IEnumerable<ViewModelUser>  
  2.   
  3. @{  
  4.    ViewBag.Title = "View All Users  
  5.    Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <table id="list-grid" class="table table-bordered hidden">  
  9.    <thead>  
  10.       <tr>  
  11.          <th>  
  12.             @Html.DisplayNameFor(model => model.Id)  
  13.          </th>  
  14.          <th>  
  15.             @Html.DisplayNameFor(model => model.UserName)  
  16.          </th>  
  17.          <th>  
  18.             @Html.DisplayNameFor(model => model.FirstName)  
  19.          </th>  
  20.          <th>  
  21.             @Html.DisplayNameFor(model => model.LastName)  
  22.          </th>  
  23.          <th>  
  24.             @Html.DisplayNameFor(model => model.Email)  
  25.          </th>  
  26.          <th>  
  27.             @Html.DisplayNameFor(model => model.IsVisible)  
  28.          </th>  
  29.          <th></th>  
  30.       </tr>  
  31.    </thead>  
  32.   
  33.    <tbody>  
  34.       @foreach (var item in Model)  
  35.       {  
  36.          <tr>  
  37.             <td>  
  38.                @Html.DisplayFor(modelItem => item.Id)  
  39.             </td>  
  40.             <td>  
  41.                @Html.DisplayFor(modelItem => item.UserName)  
  42.             </td>  
  43.             <td>  
  44.                @Html.DisplayFor(modelItem => item.FirstName)  
  45.             </td>  
  46.             <td>  
  47.                @Html.DisplayFor(modelItem => item.LastName)  
  48.             </td>  
  49.             <td>  
  50.                @Html.DisplayFor(modelItem => item.Email)  
  51.             </td>  
  52.             <td>  
  53.                @Html.DisplayFor(modelItem => item.IsVisible)  
  54.             </td>  
  55.             <td></td>  
  56.          </tr>  
  57.       }  
  58.    </tbody>  
  59. </table>
  60.   
  61. <script>  
  62.    $(function () {  
  63.       $("#list-grid").find("th").eq(0).hide(); // hides the Id column  
  64.       $("#list-grid").kendoGrid({  
  65.          sortable: true,  
  66.          resizable: true,  
  67.          reorderable: true,  
  68.          columnMenu: true,  
  69.          filterable: true,  
  70.          columns: [  
  71.             { field: "Id", title: "User Id", width: 50, hidden: true },  
  72.             { field: "UserName", title: "User Name", width: 120 },  
  73.             { field: "FirstName", title: "First Name" },  
  74.             { field: "LastName", title: "Surname" },  
  75.             { field: "Email", title: "Email Address" },  
  76.             { field: "IsVisible", title: "Active?", width: 100 },  
  77.             {  
  78.             command: [  
  79.             { text: "Edit", click: editItem },  
  80.             { text: "Delete", click: deleteItem }  
  81.             ],  
  82.             title: " ",  
  83.             width: 200  
  84.             }  
  85.          ]  
  86.          }).removeClass("hidden"); // displays the grid after JS and CSS have been applied  
  87.       });  
  88.   
  89. </script>

Hide Columns But Don't Hide Them 

Sometimes, you want to have columns included in your grid. But you don't necessarily want them to be initially visible. I do this using JavaScript and applying the .hide() method against the TH cell that I want to hide.
  1. $("#list-grid").find("th").eq(0).hide();

This code looks for the <code>#list-grid</code> table, finds the 0th TH element and hides it. You can do that with any column that is displayed in the table. Just remember, you do need to set the <code>hidden</code> property within the column instantiation of the <code>kendoGrid()</code> method so that the cell content is hidden too.

Stop Unformatted Table Data Showing Before CSS Is Applied 

You will notice that I have applied the <code>hidden</code> class to the table. By default, the table will not be displayed. However, you will see at the end of the <code>kendGrid()</code> method that I have chained the <code>removeClass("hidden")</code> method. This will remove the hidden class from the table once all the JavaScript and CSS have been applied. The reason for this is because slow internet connections may take time to apply CSS to the table. If this happens, there is a flash of the unformatted table before the Kendo CSS is applied. Letting JavaScript "unhide" the finished formatted table will ensure the user gets the intended formatted experience without the flash of raw content.

Until next time...
 


Similar Articles