Showing posts with label Kendo UI. Show all posts
Showing posts with label Kendo UI. Show all posts

Wednesday, July 10, 2013

MVC +WEBAPI +KendoUI Sample project

Download the application form following location


https://drive.google.com/folderview?id=0B1N7j4r5VvcrakZ5dEFOVTlUUms&usp=sharing

Here i have demonstrated how we can use kendo UI with MVC and WEB API. There are various ways to do this, this is one of the way.

Thursday, July 19, 2012

Kendo UI: Iterations on Hierarchical data Source

If you are looking into populating the hierarchical data source using Keno framework, First of all you need to use [ kendo.data.HierarchicalDataSource] and the structure should be something like
 var dataSource = kendo.data.HierarchicalDataSource({  
   type: "json",  
   transport: {  
     read: {  
       url: "/test",  
       datatype: json,  
       type: "POST",  
       contentType "application/json;charset=utf-8",  
     }  
   },  
   schema: {  
     model: {  
       hasChildren: "hasChildren"  
     }  
   }  
 });  

Here hasChildren value comes from your controller with the data,depending upon if the parent has any child node attach. so when you return data from controller make sure you have hasChildren field defined and that should be a bool value.

 <div id="dTree">  
 </div>  
 <script type="text/javascript">  
   var Tree;  
   var Root;  
   $(document).ready(function () {  
     Root = new kendo.data.HierarchicalDataSource({  
       transport: {  
         read: {  
           url: document.URL + "/GetTreeData/",  
           dataType: "json"  
         }  
       },  
       schema: {  
         model: {  
           id: "Id",  
           hasChildren: "HasChildren"  
         }  
       }  
     });  
     Tree = $("#dTree").kendoTreeView({  
       dataSource: Root,  
       dataValueField: "Id",  
       dataTextField: "Name",  
       select: function (e) {  
         var selecteddata = null;  
         if ($.browser.msie)  
           selecteddata = Root.getByUid(e.node.attributes[1].nodeValue);  
         else  
           selecteddata = Root.getByUid(e.node.attributes[0].nodeValue);  
         UnitEventArgunments.Selecteddata = selecteddata;  
         UnitEventArgunments.SelectedTreeNode = e;  
         $(document).trigger(OnNodeEvents.OnNodeUnitClicked, UnitEventArgunments);  
       }  
     });  
                //This is the event that will be triggered when node has been added  
     $(document).bind(OnNodeEvents.OnNodetAdded, function (e, args) {  
       refreshTreeView(args);  
     });  
   });  
   function refreshTreeView(e) {  
     
     var uId = new Array();  
     if ($.browser.msie)  
       selectedNodeData = Root.getByUid(e.node.attributes[1].nodeValue);  
     else  
       selectedNodeData = Root.getByUid(e.node.attributes[0].nodeValue);  
     var childData = selectedNodeData.children._data;  
     for (var i = 0; i < childData.length; i++) {  
       uId[i] = childData[i].uid;  
     }  
     for (var j = 0; j < uId.length; j++) {  
       var item = Tree.data("kendoTreeView").dataSource.getByUid(uId[j]);  
       Tree.data("kendoTreeView").dataSource.remove(item);  
     }  
     appendNode(selectedNodeData);  
   }  
   function appendNode(selectedNodeData) {  
     $.post("/GetTreeData", { Id: selectedNodeData.Id },  
         function (data) {  
           Tree.data("kendoTreeView").append(data, $("[data-uid=" + selectedNodeData.uid + "]"));  
         });  
   }  
 </script>  

Friday, June 29, 2012

Kendo UI :Web development framework- Part 2

The MVVM model is most popular now a days . Here we will see with the sample example the use of MVVM in MVC.

To start with  MVVM  we need to first create a View Model, A view model is an observable object. This object has properties and methods. Each property will be bound to something in the HTML. In MVVM, this binding is two way, meaning that if the binding changes on the UI, the model changes, and vice versa.

Let us take a sample example where we will take display a grid and a drop down box, firstly i will create a HTML controls for grid and drop-down. And grid will be using a Template column.

 <script id="rowTemplate" type="text/x-kendo-tmpl">  
   <tr>  
     <td>${ UserName }</td>  
     <td>${ GroupName }</td>  
     <td>  
      <input type="image" id="btnremove" src="/Content/Images/DeleteIcon.gif" data-bind='events { click : removeUser }, enabled: btnSaveIsEnabled' style="width:20px;height:20px;"/></td>  
   </tr>  
 </script>  
 <div id="UserTab">  
   <div id="userGrid" data-role="grid" data-bind="source: User.UsersOf"  
     data-row-template="rowTemplate" data-columns='["User", "Group", "Remove" ]' data-pageable='{"pageSize":10}'>  
   </div>  
   <table style="width: 100; margin-top: 20px; margin-bottom: 20px">  
     <tr>  
       <td style="width: 45%">  
         <select id="dduser" data-role="dropdownlist" data-value-field="Id" data-text-field="Name"  
           data-bind="source: User.AllowableUsers, value: selectedUser, events: { change : userSelectChange }" />  
       </td>  
       <td style="width: 45%">  
         <select id="ddgroup" data-role="dropdownlist" data-value-field="Id" data-text-field="Name"  
           data-bind="source: User.AllowableGroups, value: selectedGroup, events: { change : groupSelectChange }" />  
       </td>  
       <td style="width: 10%">  
         <button id="btnSave" class="k-button" style="width: 75px" data-bind='events { click : addUser }, text: addUserText, enabled: btnSaveIsEnabled'>  
         </button>  
       </td>  
     </tr>  
   </table>  
   <a href='@Url.Action("Create", "Group", new { })' style="color: Blue; text-decoration: true;">  
     Add New Group</a><br />  
   <a href='@Url.Action("Index", "Type", new { })' style="color: Blue; text-decoration: true;">  
     Add New Type</a>  
 </div>  

Here we have created a UserViewModel that is binded to the div id :UserTab, This example also shows how to add and delete user using MVVM pattern.

 <script language="javascript" type="text/javascript">  
   $(document).ready(function () {  
     var UserViewModel = kendo.observable({  
       UserSource: new kendo.data.DataSource({  
         transport: {  
           read: {  
             url: document.URL + "/GetUser/",  
             dataType: "json",  
             type: "post",  
             complete: function (e) {  
               if (UserViewModel.UserSource == null) return;  
               UserViewModel.set("User", UserViewModel.UserSource.at(0));  
             },  
             data: { Id: 0, TypeId: 0 }  
           },  
           update: {  
             url: document.URL + "/AddUserToUser/",  
             dataType: "json",  
             type: "post",  
             complete: function (e) { }  
           },  
           parameterMap: function (options, operation) {  
             if (operation !== "read" && options.models)  
               return { models: kendo.stringify(options.models) };  
             return options;  
           }  
         },  
         schema: {  
           model: { id: "Id" }  
         }  
       }),  
       User: null,  
       selectedGroup: null,  
       selectedUser: null,  
       Id: 0,  
       TypeId: 0,  
       btnSaveIsEnabled: false,  
       addUserText: 'Add',  
       groupSelectChange: function (e) { },  
       userSelectChange: function (e) { },  
       load: function (e) {  
         this.set("Id", e.Id);  
         this.set("TypeId", e.TypeId);  
         this.UserSource.read({ Id: e.Id, TypeId: e.TypeId });  
         $("#dduser").val("").data("kendoDropDownList").text("--Choose User--");  
         $("#ddgroup").val("").data("kendoDropDownList").text("--Choose Group--");  
       },  
       addUser: function (e) {  
         if (this.get("selectedUser") == null || this.get("selectedGroup") == null) {  
           alert("Please select User and Group");  
           return;  
         }  
         if (IsUserExist(this.get("selectedUser").Id, this.get("selectedGroup").Id)) {  
           alert("User already exist");  
           return;  
         }  
         var Id = this.get("Id");  
         var TypeId = this.get("TypeId");  
         $.post("/AddUserToUser", { Id: this.get("Id"), groupId: this.get("selectedGroup").Id, userId: this.get("selectedUser").Id },  
         function (data) {  
           if (data) {  
             UserViewModel.UserSource.read({ Id: Id, TypeId: TypeId });  
             aler("User/Group Added");  
           }  
           else  
             growlSad("User not Added");  
         });  
       },  
       removeUser: function (e) {  
         if (confirm("Are you sure you want to delete this user from group?")) {  
           var Id = this.get("Id");  
           var TypeId = this.get("TypeId");  
           $.post("/DeleteUser", { UserId: e.data.UserId },  
         function (data) {  
           UserViewModel.UserSource.read({ Id: Id, TypeId: TypeId });  
         });  
         }  
       }  
     });  
     kendo.bind($("#UserTab"), UserViewModel);  
     $(document).bind(Events.OnClicked, function (event, args) {  
       UserViewModel.load({ Id: args.Selected.Id, TypeId: args.Selected.TypeId });  
       UserViewModel.set("btnSaveIsEnabled", args.Selected.IsActive);  
     });  
     $(document).bind(window.Events.OnUpdated, function (e, args) {  
       ResetFormUser();  
     });  
     function IsUserExist(userId, groupId) {  
       var gridDataSource = UserViewModel.UserSource._data[0].UsersOf;  
       for (var i = 0; i < gridDataSource.length; i++) {  
         if (userId == gridDataSource[i].UserId && groupId == gridDataSource[i].GroupId)  
           return true;  
       }  
       return false;  
     }  
     function ResetFormUser() {  
       UserViewModel.load({ Id: 0, TypeId: 0 });  
       UserViewModel.set("btnSaveIsEnabled", false);  
     }  
   });  
 </script>  

Happy Coding :) 

Thursday, June 28, 2012

Kendo UI :Web development framework- Part 1


Couple of days back i came to learn about a web UI frame work known as Kendo UI. A framework that is for HTML5 apps, It is jQuery-based which also includes MVVM support ,rich Data Source and several UI widgets.


Currently i am using the MVVM framework for the UI development  and the widgets like Grid Control,Splitter,Tree view, and Validation framework provided by Kendo.

The best part of the kendo UI is that your development time for the UI is very low, lets take a example of the Kendo Grid. It has a built in feature for Paging,Searching, shorting and filtering,editing on each column.

With the rich data source you can directly bind a  view model on the grid and what ever changes you are doing on the grid can be updated in the data base in a single go. I am using the Entity Framework for the batch update . We as a developer need not worry about tracking the changes. we just need to add couple of methods provided by this framework and we are done.

Soon i will be updating this article with some example and coding pattern that are used here.