2013-04-26 102 views
2

如何使用MVC ServerSide包裝創建HierarchialDataSource並將其用作TreeView的數據源?Kendo TreeView與使用ServerSide包裝的分層數據源

我已經創建了一個從控制器返回爲Json的層次結構,但TreeView只顯示頂級節點。是否需要在TreeView上設置一個屬性來指示DataSource是否是heirarchial?

我看到了幾個客戶端使用JS和kendo.data庫的例子,但我找不到服務器端等效的程序集。

感謝您的協助。

回答

1

從我的理解,你必須結合在HierarchicalDataSource匹配模型的屬性: http://docs.kendoui.com/api/framework/hierarchicaldatasource

我使用樹形視圖來顯示一個菜單結構,所以這裏是我的模型:

public class Menu 
{ 
    public int Id { get; set; } 
    public int? ParentId { get; set; } 
    public string Description { get; set; } 

    public int id 
    { 
     get { return this.Id; } 
    }     

    public bool hasChildren { get; set; } 
} 

我明確必須實現idhasChildren屬性與這個確切的大腸桿菌(我填充hasCHildren屬性基於查詢之前將模型推送到視圖)。

不知道這是否是正確的做法(仍然徘徊於找到官方信息),但至少它的工作原理。

0

使用此在您的CSHTML文件:

@(Html.Kendo().TreeView() 
    .Name("trvReport") 
    .DataTextField("Name") 
    .DataSource(dataSource => dataSource 
     .Read(read => read.Action("ReportType", "Read")) 
     .Model(model => 
     { 
     model.Id("Id"); 
     model.Field("Name", typeof(string)); 
     model.Children("Reports"); 
     model.HasChildren("HasReports"); 
     }) 
    ) 
) 

在模型中實現HasChildren這樣的:

public class ReportType 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public ICollection<Report> Reports { get; set; } 

    [NotMapped] 
    public bool HasReports { get { return Reports.Count() > 0; } } 
}