2014-09-25 2 views
0

내 검도 트리 뷰가 많은 자식 노드 및바인딩 JSON 데이터

여기 내 코드 중복 코드를 하위 아이의 노드를 점점 하위 노드 .Problem 있습니다.

nodes = rep.GetTreeViewData(CompanyId); 

     var productTreeData = nodes.Select(p => new 
     { 
      Text = p.CategoryName, 
      Url = p.ActionLink, 
      hasChildren = p.SubCategories.Any(), 
      // Expanded = true, 
      EntityType = p.EntityType, 
      Selected = currurl.ToLower() == p.ActionLink.ToLower() ? true : false, 
      ImageUrl = p.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif", 

      Children = p.SubCategories.Select(c => new 
      { 
       Text = c.SubCategoryName, 
       Url = c.ActionLink, 
       // Expanded = true, 
       hasChildren = c.SubCategories.Any(), 
       EntityType = c.EntityType, 
       Selected = currurl.ToLower() == c.ActionLink.ToLower() ? true : false, 
       ImageUrl = c.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif", 

       Children = c.SubCategories.Select(d => new 
       { 
        Text = d.SubCategoryName, 
        Url = d.ActionLink, 
        // Expanded = true, 
        hasChildren = d.SubCategories.Any(), 
        EntityType = d.EntityType, 
        Selected = currurl.ToLower() == d.ActionLink.ToLower() ? true : false, 
        ImageUrl = d.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif", 


        Children = d.SubCategories.Select(f => new 
        { 
         Text = f.SubCategoryName, 
         Url = f.ActionLink, 
         //Expanded = true, 
         hasChildren = f.SubCategories.Any(), 
         EntityType = f.EntityType, 
         Selected = currurl.ToLower() == f.ActionLink.ToLower() ? true : false, 
         ImageUrl = f.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif", 

         Children = f.SubCategories.Select(g => new 
         { 
          Text = g.SubCategoryName, 
          Url = g.ActionLink, 
          // Expanded = true, 
          hasChildren = g.SubCategories.Any(), 
          EntityType = g.EntityType, 
          Selected = currurl.ToLower() == g.ActionLink.ToLower() ? true : false, 
          ImageUrl = g.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif", 

         }) 

        }) 

       }) 

      }) 

     }); 

이 과정에 의해 내보기

@(Html.Kendo().TreeView() 
             .Name("treeview-right") 
             .ExpandAll(true) 
             //.LoadOnDemand(false) 
             //.Events(events=>events.DataBound("DataBound")) 
             .DataSource(d => d 
             .Model(m => m 
             .HasChildren("hasChildren") 
             .Children("Children")) 
             .Read(r => r.Action("_ProductTree", "Home"))) 
             .DataTextField("Text") 
             .DataUrlField("Url") 
             .DataImageUrlField("ImageUrl") 
            ) 

입니다 얻고 모든 자식의 서브 자녀의 개별 부모의 네 가지 수준의 계층 구조까지 내가 한 번 더 아이를 작성해야 다른 수준을 필요 ..if 암호.

이 과정을 단순화하기 위해 2 일에서 고생하고 있습니다.

감사합니다.

답변

0

이것은 재귀로 해결할 수있는 것처럼 보입니다. 이것은 꽤 가까워 야합니다.

var productTreeData = new ListofYourTreeViewModelHere(); 

foreach (var node in rep.GetTreeViewData(CompanyId)) 
{ 
    productTreeData.Add(FillTree(node)); 
} 
... 
} 


private YourTreeViewModelHere FillTree(p) 
{ 
    var tv = new YourTreeViewModelHere(); 

    tv.Text = p.CategoryName, 
    tv.Url = p.ActionLink, 
    tv.hasChildren = p.SubCategories.Any(), 
    tv.EntityType = p.EntityType, 
    tv. Selected = currurl.ToLower() == p.ActionLink.ToLower() ? true : false, 
    tv.ImageUrl = p.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif"; 

    foreach(var sub in p.SubCategories) 
    { 
     tv.Children.Add(FillTree(x)); 
    } 
    }