2014-03-19 29 views
0

你好,我正在嘗試加載類別和產品的樹形視圖。我正在使用MVC 4 Razor。在Treeview中加載類別和項目

我終於能夠在單獨的treeviews中加載數據,看看我是否至少可以得到它。

但這裏是我現在的位置。

@(Html.Kendo().TreeView() 
    .Name("treeview") 
    .HtmlAttributes(new {@class="tree-section" }) 
    .DataTextField("Name") 
    .DataSource(dataSource => dataSource 
     .Read(read => read 
      .Action("treeItems", "Home") 
     ) 
    ) 
) 
<script> 
    $(document).ready(function() { 
     var treeview = ("#treeview").data("kendoTreeView"); 
    }); 

</script> 

這裏是我的控制器

public JsonResult treeItems(int? id) 
{ 
    var dataContext = new myContext(); 

    //var cat = from e in dataContext.Categories 
    // where (id.HasValue ? e.ParentId == id : e.ParentId == null) 
    // select new 
    // { 
    //  id = e.CategoryId, 
    //  Name = e.Name, 
    //  hasChildren = e.Categories1.Any() 
    // }; 

    var prods = from c in dataContext.Items 
       join a in dataContext.SubItems on c.ItemId equals a.ItemId 
       where (id.HasValue ? a.ParentItemId == id : a.ParentItemId == null) 
       select new 
       { 
        id = c.ItemId, 
        Name = c.Name, 
        hasChildren = c.SubItems.Any() 
       }; 
    return Json(prods, JsonRequestBehavior.AllowGet); 
} 

這些都是如果我seperately加載它們變成2種不同的樹木進行工作的兩個查詢,但我想要的分類 - >子類別 - >項目 - - >子項

這裏是模型

enter image description here

的示例代碼段

如何正確加載?

+0

要加載子項以及解決? –

+0

@Rudresh是子項目以及 – user1307149

+0

在子項目中也會存儲類別ID嗎? –

回答

1

綁定一個劍道樹視圖使用內置視圖模型樹視圖劍道稱爲

Kendo.Mvc.UI.TreeViewItemModel 

,這裏是

public ActionResult GetAllTreeData() 
{ 
    var treeItems = new List<Kendo.Mvc.UI.TreeViewItemModel>(); 
    var categories = from category in this.dataContext.Categories 
        select category; 
    var products = from Item in this.dataContext.Items 
        select Item; 
    var allCategoryList = categories.toList(); 
    var allItems = products.ToList(); 

    if (allItems == null) 
    { 
     allItems = new List<Item>(); 
    } 

    if (allCategoryList != null && allCategoryList.count() > 0) 
    { 
     foreach (var category in allCategoryList) 
     { 
      treeItems.Add(new Kendo.Mvc.UI.TreeViewItemModel() 
      { 
       Id = category.Id.ToString(), 
       Text = category.Name 
      }); 
     } 

     foreach (var node in treeItems) 
     { 
      var items = products.Where(x => x.CategoryId == Convert.ToInt32(node.Id)).ToList(); 
      if (items.Any()) 
      { 
       foreach (var item in items) 
       { 
        var parentItem = new Kendo.Mvc.UI.TreeViewItemModel() 
        { 
         Id = item.Id.ToString(), 
         Text = item.Name 
        }; 

        this.BuildTreeRecursive(allItems, parentItem); 
        treeItems.Add(parentItem); 
       } 
      } 
     } 
    } 
} 

public void BuildTreeRecursive(IEnumerable<Item> allItems, Kendo.Mvc.UI.TreeViewItemModel parentNode) 
{ 
    parentNode.HasChildren = true; 

    var nodes = allItems 
     .Where(x => x.ParentItemId == Convert.ToInt32(parentNode.Id)) 
     .Select(p => new TreeViewItemModel 
     { 
      Text = p.Name, 
      Id = p.Id.ToString(CultureInfo.InvariantCulture) 
     }); 
    foreach (var node in nodes) 
    { 
     parentNode.Items.Add(node); 
     this.BuildTreeRecursive(allItems, node); 
    } 
}