2013-08-30 26 views
0

我试图建立一个剑道树菜单。我无法弄清楚数据应该提供给小部件的格式。到目前为止,我已经试过这样:什么应该是供给剑道树的数据格式?

型号:

public class TreeModel 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string URL { get; set; } 
    public int? ParentsID { get; set; } 
    public bool HasChild { get; set; } 
} 

控制器:

public ActionResult LoadMenu() 
    { 
     List<TreeModel> list = new List<TreeModel> { 
     new TreeModel() { ID=1, Name="Setup", URL="m.facebook.com", HasChild=true}, 
     new TreeModel() { ID=10, Name="Leave", URL="google.com", ParentsID=1, HasChild=false}, 
     new TreeModel() { ID=2, Name="EmployeeInfo", URL="m.facebook.com", HasChild=true}, 
     new TreeModel() { ID=11, Name="Basic Employee", URL="m.facebook.com", HasChild=false, ParentsID=2}, 

     }; 

     var nodes = (from n in list 
        where n.HasChild == true 
        select n).ToList(); 

     return Json(nodes, JsonRequestBehavior.AllowGet); 

    } 

脚本对我的看法:

<script type="text/javascript"> 

    homogeneous = new kendo.data.HierarchicalDataSource({ 
     transport: { 
      read: { 
       url: "/Home/LoadMenu", 
       //dataType: "json", 
       type: "GET" 

      } 
     }, 
     schema: { 
      model: { 
       id: "ID", 
       hasChildren: "HasChild" 
      } 
     } 
    }); 




    $(document).ready(function() { 

     $("#treeMenu").kendoTreeView({ 
      dataSource: homogeneous, 
      dataTextField: "Name", 
      dataUrlField: "URL", 
      hasChildren:"ParentsID" 
     }); 

    }); 


     </script> 

电流输出&问题上都标有屏幕截图。 enter image description here

请大家帮忙。谢谢。

回答

1

请试试下面的代码片段。

public ActionResult LoadMenu(int? id) 
{ 
    List<TreeModel> list = new List<TreeModel> { 
    new TreeModel() { ID=1, Name="Setup", URL="m.facebook.com", HasChild=true}, 
    new TreeModel() { ID=10, Name="Leave", URL="google.com", ParentsID=1, HasChild=false}, 
    new TreeModel() { ID=2, Name="EmployeeInfo", URL="m.facebook.com", HasChild=true}, 
    new TreeModel() { ID=11, Name="Basic Employee", URL="m.facebook.com", HasChild=false, ParentsID=2}, 

    }; 

     var nodes = (from n in list 
        where (id.HasValue ? n.ParentsID == id.Value : n.ParentsID == null) 
        select n).ToList(); 

     return Json(nodes, JsonRequestBehavior.AllowGet); 

} 
+0

它实际上工作,我看到了kendo网站上的代码,但没有给予应有的注意。谢了哥们。 –