Wednesday, December 21, 2011

kendoUI - adding menu using web service

kendoUI is still in the beginning stages. Eventhough they have a commercial version there is quite a bit of functionalities needed before it can be fully used for a large application.


I am not sure what is the best practice for using each control and I would like to know this myself from @Telerik.  But here is an example of how to populate the kendoMenu dynamically using a web-service.


General approach is that each control/widget on the page should get the data from web-service. That way there is no server request. Now, our web-service can internally get the data from anywhere (database, xml file or any other appropriate way.) There will be a question about menu based on user-rights. But let's save that for later.


#1 - for now, let's assume that we have a web-service that has a function like following to return the menu details. 

    [WebService] //(Namespace = "http://WebService/")
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public List GetMenu()
        {
            List items = new List();
            items.Add(new MenuItem() { ParentTitle = "", Title = "Home", Link = "default.aspx" });
            items.Add(new MenuItem() { ParentTitle = "", Title = "Edit", Link = "edit.aspx" });
            items.Add(new MenuItem() { ParentTitle = "", Title = "View", Link = "view.aspx" });
            items.Add(new MenuItem() { ParentTitle = "Home", Title = "Login", Link = "view.aspx" });
            items.Add(new MenuItem() { ParentTitle = "Home", Title = "Profile", Link = "view.aspx" });
            items.Add(new MenuItem() { ParentTitle = "Home", Title = "Meetings", Link = "view.aspx" });
            items.Add(new MenuItem() { ParentTitle = "Home", Title = "Logout", Link = "view.aspx" });
            items.Add(new MenuItem() { ParentTitle = "Edit", Title = "Cut", Link = "view.aspx" });
            items.Add(new MenuItem() { ParentTitle = "Edit", Title = "Copy", Link = "view.aspx" });
            items.Add(new MenuItem() { ParentTitle = "Edit", Title = "Paste", Link = "view.aspx" });
            return items;
        }



#2 - we can declare our MenuItem class having all possible properties that we want to make use of in the front-end. We may want to have some unique id, or type of menu etc. but below is a simpler form of such menu item class.
    public class MenuItem
    {
        public string ParentTitle { get; set; }
        public string Title { get; set; }
        public string Link { get; set; }
        public string IconUrl { get; set; }
    }

#3 - the html can be as simple as 

No comments:

Post a Comment