2017-05-05 133 views
0

我想在telerik/kendo网格中显示分层数据。数据来自数据库视图(导入为模型),其中包含电气中断列表。主键是interruptID在视图中有一个名为ParentOutage的字段,如果填充了另一个中断的InterruptionID,则会与2条记录相关联。我跟着Telerik的例子/演示到信,但我不能让网格显示任何儿童记录。填充子记录的事件永远不会触发。我已经试着对传递给getChildOutages函数的值进行硬编码,以查看是否存在问题,但仍未解雇。网格中的每一行都会出现箭头以展开记录,但是当您展开它时,只会出现一个小空白区域。也只有少数记录具有子记录,因此箭头不应出现在每条记录旁边。我已为我下面的代码:Telerik MVC Grid Hierarchy - 没有儿童记录

<div class="form-group"> 
    @Code 


     Html.Kendo.Grid(Of vw_ElectInterruptions)() _ 
                           .Name("gridInterruptions") _ 
              .Columns(Sub(c) 
                  c.Bound(Function(p) p.InterruptionID).Width(75).Sortable(True).Title("ID") 
                  c.Bound(Function(p) p.ParentOutage).Width(85).Sortable(True).Title("Parent ID") 
                  c.Bound(Function(p) p.Description).Width(175).Sortable(True).Title("Description") 
                  c.Bound(Function(p) p.CircuitOrArea).Width(150).Sortable(True).Title("Area/Circuit") 
                  c.Bound(Function(p) p.TimeOff).Width(100).Sortable(True).Title("TimeOff").Format("{0:dd-MMM-yy hh:mm}") 
                  c.Bound(Function(p) p.TimeOn).Width(100).Sortable(True).Title("TimeOn").Format("{0:dd-MMM-yy hh:mm}") 

                End Sub) _ 
                .Events(Function(e) e.DataBound("dataBound")) _ 
                .ClientDetailTemplateId("template") _ 
                .AutoBind(True) _ 
                .Sortable(Sub(d) d.SortMode(GridSortMode.SingleColumn).AllowUnsort(False)) _ 
                .Filterable() _ 
                 .HtmlAttributes(New With {.Style = "height:500px;"}) _ 
                       .Pageable(Sub(d) d.PageSizes(True).ButtonCount(5).Refresh(True)) _ 
                             .DataSource(Sub(d) 
                                  d.Ajax() _ 
                                  .Sort(Sub(sort) 
                                      sort.Add("TimeOff").Descending() 
                                      sort.Add("InterruptionID").Ascending() 
                                    End Sub) _ 
                                  .PageSize(25) _ 
                                  .ServerOperation(False) _ 
                                  .Read(Function(read) read.Action("ElecInterruptionRefreshGrid", "Application")) _ 
                                  .Events(Sub(e) 
                                      e.Error("grid_error") 
                                    End Sub) 
                                End Sub).Render() 


    End Code 

</div> 

<script id="template" type="text/x-kendo-template"> 
 
    @Code 
 
     Html.Kendo.Grid(Of vw_ElectInterruptions)() _ 
 
                             .Name("childInterruptions_#=InterruptionID#") _ 
 
                .Columns(Sub(c) 
 
                    c.Bound(Function(p) p.InterruptionID).Width(75).Sortable(True).Title("ID") 
 
                    c.Bound(Function(p) p.Description).Width(175).Sortable(True).Title("Description") 
 
                    c.Bound(Function(p) p.CircuitOrArea).Width(150).Sortable(True).Title("Area/Circuit") 
 
                    c.Bound(Function(p) p.TimeOff).Width(100).Sortable(True).Title("TimeOff").Format("{0:dd-MMM-yy hh:mm}") 
 
                    c.Bound(Function(p) p.TimeOn).Width(100).Sortable(True).Title("TimeOn").Format("{0:dd-MMM-yy hh:mm}") 
 

 

 
                  End Sub) _ 
 
                    .DataSource(Sub(d) 
 
                         d.Ajax() _ 
 
                         .Read(Function(read) read.Action("GetChildOutages", "Application", New With {.id = "#=InterruptionID#"})) 
 
                         
 
                       
 
                       End Sub).ToClientTemplate() 
 
    End Code 
 
    
 
</script> 
 

 
    <script> 
 
     function dataBound() { 
 
      this.expandRow(this.tbody.find("tr.k-master-row").first()); 
 
     } 
 
    </script>

And the controller code: 

    Public Function ElecInterruptionRefreshGrid(request As DataSourceRequest) As ActionResult 

       Dim model = db.vw_ElectInterruptions.ToList 

       Dim jsonResult = Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet) 
       jsonResult.MaxJsonLength = Int32.MaxValue 
       Return jsonResult 

     End Function 

    Public Function GetChildOutages(id As Integer, request As DataSourceRequest) As ActionResult 


       Dim model = db.vw_ElectInterruptions.Where(Function(w) w.ParentOutage = id).ToList 

       Dim jsonResult = Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet) 
       jsonResult.MaxJsonLength = Int32.MaxValue 
       Return jsonResult 

     End Function 
+0

首先,我强烈建议改进此帖子的缩进。其次,在试图展开一行时,你是否在控制台窗口中看到任何错误? – Sandman

+0

抱歉,缩进 - Visual Studio似乎无法正确处理剃须刀代码/ html,并总是弄乱我的缩进 - 我修复它们,然后回到原来的样子。我能够解决问题并发布答案。 – PlasmaX

回答

0

好吧我花在这个小时,然后找到分钟内张贴回答这个问题后。显然它不喜欢我将子网格模板放入@Code ... End Code - 块中的事实。我改变了模板脚本看起来像这一点,它开始工作:

@(Html.Kendo.Grid(Of vw_ElectInterruptions)() _ 
 
                             .Name("childInterruptions_#=InterruptionID#") _ 
 
                .Columns(Sub(c) 
 
                    c.Bound(Function(p) p.InterruptionID).Width(75).Sortable(True).Title("ID") 
 
                    c.Bound(Function(p) p.Description).Width(175).Sortable(True).Title("Description") 
 
                    c.Bound(Function(p) p.CircuitOrArea).Width(150).Sortable(True).Title("Area/Circuit") 
 
                    c.Bound(Function(p) p.TimeOff).Width(100).Sortable(True).Title("TimeOff").Format("{0:dd-MMM-yy hh:mm}") 
 
                    c.Bound(Function(p) p.TimeOn).Width(100).Sortable(True).Title("TimeOn").Format("{0:dd-MMM-yy hh:mm}") 
 

 

 
                  End Sub).AutoBind(True) _ 
 
                    .DataSource(Sub(d) 
 
                         d.Ajax() _ 
 
                         .Read(Function(read) read.Action("GetChildOutages", "Application", New With {.id = "#=InterruptionID#"})) 
 
                         
 
                       
 
                       End Sub).ToClientTemplate())

不知道为什么会有所作为。