最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access row data from within a ClientTemplate within a ClientDetailTemplate - Stack Overflow

programmeradmin5浏览0评论

I'm using a Kendo UI Web Grid in an ASP.Net MVC 4 application.

At the bottom of the code below you'll see a JavaScript function called GetEditChildUrl that accepts a parameter called data. Unfortunately, what gets passed in as the data parameter is the parent row data but I was expecting the child row data. This works fine for GetEditParentUrl. So how do I get the child row data?

@(Html.Kendo().Grid<Application.Models.Parent>()
    .Name("grid_parent")
    .Columns(columns =>
    {
        columns.Bound(x => x.Name);
        columns.Bound(x => x).ClientTemplate(
            "<a href='#= GetEditParentUrl(data) #' class='k-button' style='min-width:0px;'><span class='k-icon k-i-pencil'></span></a>"
          ).Width(90).Filterable(false);
    })
    .Scrollable(action => action.Virtual(true))
    .Filterable()
    .Sortable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Parents_Read", "Parent"))
    )
    .Scrollable(s => s.Height("auto"))
    .ClientDetailTemplateId("client-template")
)

@section Scripts {
    <script id="client-template" type="text/x-kendo-template">
        <h3>Contacts</h3>

        @(Html.Kendo().Grid<Application.Models.Child>()
            .Name("grid_child_#=Id#") // make sure the Name is unique
            .Columns(columns =>
            {
                columns.Bound(x => x.Name);
                columns.Bound(x => x).ClientTemplate(
                    "<a href='#= GetEditChildUrl(data) #' class='k-button' style='min-width:0px;'><span class='k-icon k-i-pencil'></span></a>"
                ).Width(90).Filterable(false);
            })
            .DataSource(dataSource =>
                dataSource.Ajax().Read(read => read.Action("Children_Read", "Parent", new { parentId = "#=Id#" }))
            )
            .Scrollable(s => s.Height("auto"))
            .Sortable()
            .ToClientTemplate()
        )
    </script>

    <script>
        function GetEditParentUrl(data) {
            return "@Url.Action("Edit", "Parent")/" + data.Id;
        }
        function GetEditChildUrl(data) {
            return "@Url.Action("Edit", "Child")/" + data.Id;
        }
    </script>
}

I'm using a Kendo UI Web Grid in an ASP.Net MVC 4 application.

At the bottom of the code below you'll see a JavaScript function called GetEditChildUrl that accepts a parameter called data. Unfortunately, what gets passed in as the data parameter is the parent row data but I was expecting the child row data. This works fine for GetEditParentUrl. So how do I get the child row data?

@(Html.Kendo().Grid<Application.Models.Parent>()
    .Name("grid_parent")
    .Columns(columns =>
    {
        columns.Bound(x => x.Name);
        columns.Bound(x => x).ClientTemplate(
            "<a href='#= GetEditParentUrl(data) #' class='k-button' style='min-width:0px;'><span class='k-icon k-i-pencil'></span></a>"
          ).Width(90).Filterable(false);
    })
    .Scrollable(action => action.Virtual(true))
    .Filterable()
    .Sortable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Parents_Read", "Parent"))
    )
    .Scrollable(s => s.Height("auto"))
    .ClientDetailTemplateId("client-template")
)

@section Scripts {
    <script id="client-template" type="text/x-kendo-template">
        <h3>Contacts</h3>

        @(Html.Kendo().Grid<Application.Models.Child>()
            .Name("grid_child_#=Id#") // make sure the Name is unique
            .Columns(columns =>
            {
                columns.Bound(x => x.Name);
                columns.Bound(x => x).ClientTemplate(
                    "<a href='#= GetEditChildUrl(data) #' class='k-button' style='min-width:0px;'><span class='k-icon k-i-pencil'></span></a>"
                ).Width(90).Filterable(false);
            })
            .DataSource(dataSource =>
                dataSource.Ajax().Read(read => read.Action("Children_Read", "Parent", new { parentId = "#=Id#" }))
            )
            .Scrollable(s => s.Height("auto"))
            .Sortable()
            .ToClientTemplate()
        )
    </script>

    <script>
        function GetEditParentUrl(data) {
            return "@Url.Action("Edit", "Parent")/" + data.Id;
        }
        function GetEditChildUrl(data) {
            return "@Url.Action("Edit", "Child")/" + data.Id;
        }
    </script>
}
Share Improve this question asked Dec 30, 2013 at 6:39 DeanDean 4,6047 gold badges36 silver badges46 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Try escaping the hash (#) symbols in your column client template.

columns.Bound(x => x)
    .ClientTemplate("<a href='\\#= GetEditChildUrl(data) \\#' ...>")

From the KendoUI docs:

Important: The "#" characters used for a template expression should be escaped when using a column ClientTemplate in a detail template so that the expression is evaluated in the correct context.

发布评论

评论列表(0)

  1. 暂无评论