首页 文章

如何在Kendo Grid单元格中显示Kendo Grid?

提问于
浏览
2

我在我的MVC应用程序中使用Kendo Grid来显示数据 . 它工作得很好 . 但我想在网格单元格中显示另一个网格 . 我做了我的研究并尝试了不同的东西,但我没有找到任何解决方案 . 请建议 . 这是我的代码 .

@(Html.Kendo().Grid<TimeSheetManagement.Models.ClientView>()
.Name( "Clients" )
.Columns( columns =>
{
    columns.Bound( e => e.Name );
    columns.Bound( e => e.Address );
    columns.Bound( e => e.City );
    columns.Bound( e => e.State );
    columns.Bound( e => e.ZipCode );
    columns.Template( e => e.Contacts ).ClientTemplate( "#= buildContactsGrid(data) #" );
    columns.Bound( e => e.CreatedDate );
    columns.Bound( e => e.CreatedBy );
    columns.Bound( e => e.UpdatedDate );
    columns.Bound( e => e.UpdatedBy );
    columns.Bound( "" ).ClientTemplate( @Html.ActionLink( "Edit" , "Create" , new { clientId = "#=Id#" } , new { title = "Edit Client" } ).ToHtmlString() );
} )
.Pageable().Sortable().Filterable()
.AutoBind( true )
.DataSource( source => source.Ajax()
                    .PageSize( 20 )
                    .Read( read => read.Action( "GetClients" , "Client" ) )
    )
)

这是我的JavaScript函数 .

<script>

    function buildContactsGrid(client)
    {
        var htmlContacts = '';
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: '@Url.Action( "GetJsonContactsByClientId" )',
            data: JSON.stringify({
                'sClientId': client.Id
            }),
            dataType: "json",
            async: false,
            success: function (response) {
                htmlContacts += "<table style='border:1px solid black;'><tr><th>First Name</th><th>Last Name</th><th>Role</th></tr><tr>";
                $(response).each(function (index, item) {
                    htmlContacts +="<td>"+ item.FirstName +"</td><td>"+ item.LastName+"</td><td>"+item.Role +"</td></tr>";
                });
                htmlContacts += "</table>";
            }
        });
        return htmlContacts;
    }
</script>

我能够在JavaScript函数中构建一个表并在网格单元格中显示,但我想显示Kendo Grid .

enter image description here

1 回答

  • 3

    通过做一些研究在谷歌上花了几天后,我发现了这个帖子link,他们解释了为什么客户端模板没有受到限制 .

    这是网格单元格中的网格:

    columns.Template( e => "" ).Title("Contacts").ClientTemplate( 
            Html.Kendo().Grid<TimeSheetManagement.Models.ContactView>()
               .Name( "Clients_#=Id#" )
               .Columns( c =>
               {
                   c.Bound( e1 => e1.FullName );
                   c.Bound( e1 => e1.Role );
                   c.Bound( e1 => e1.Email );
                   c.Bound( e1 => e1.PhoneNumber );
    
                } )
                .AutoBind( true )
                .DataSource( source1 => source1.Ajax()
                .PageSize( 5 )
                .Read( read1 => read1.Action( "GetContactsByClientId" , "Client" , new { sClientId = "#=Id#" } ) )
                )
                .ToClientTemplate()
                .ToHtmlString()
            );
    

    我在网格上有这个事件 .

    .Events( e => e.DataBound( "onGridDataBound" ) )
    

    最后我在脚本中添加了这段代码 .

    function onGridDataBound(e)
        {
            $('#Clients script').appendTo(document.body);
        }
    

    这是我预期的输出 . 如果您有任何疑问,请告诉我 .

    enter image description here

相关问题