Kendo网格与子网格ClientTemplate错误的模板

时间:2019-04-04 06:40:06

标签: c# kendo-ui kendo-ui-grid

如果单击父网格项目,则将扩展子网格。如果未批准子网格模型的ItemState,我想显示一个删除行的按钮。但我收到“未捕获的错误:无效的模板”

我尝试过:

columns.Bound(c => c.Id).ClientTemplate(
 "# if (ItemState != 1 ) { #" +
 "<button type='button' class='btn btn-delete' onclick=\"deleteChildBtn('\\#: 
 Id \\#')\"><span class='fa fa-trash'></span></button>" +
 "# } #"
);

未定义'ItemState'。因为使用了父级的模型,所以ItemState不存在!

columns.Bound(c => c.Id).ClientTemplate(
 "# if (\\#: data.ItemState \\# != 1 ) { #" +
 "<button type='button' class='btn btn-delete' onclick=\"deleteChildBtn('\\#: 
 Id \\#')\"><span class='fa fa-trash'></span></button>" +
 "# } #"
);
columns.Bound(c => c.Id).ClientTemplate(
 "# if (\\#: ItemState \\# != 1 ) { #" +
 "<button type='button' class='btn btn-delete' onclick=\"deleteChildBtn('\\#: 
 Id \\#')\"><span class='fa fa-trash'></span></button>" +
 "# } #"
);

获得了“无效模板”

另一种尝试是使用排除的javascript函数

columns.Bound(c => c.Bound).ClientTemplate("\\#: test(data) \\#").Encoded(false);

....
function test(data) {
   if (data.ItemState != 1) {
     return kendo.format("<button type='button' class='btn btn-delete' 
     onclick='deleteChildBtn({0})'><span class='fa fa-trash'></span> 
     </button>", data.Id);
    } else {
      return kendo.format("");
    } 
}

正在工作,但我已经获得了所需的值。但telerik网格未呈现html。所以它只显示原始的html

父级和子级网格的完整代码

@(Html.Kendo().Grid<Models.SalesPosition>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Template(@<p></p>).HtmlAttributes(new { style = "width:5px; text-align:left;" }).HeaderHtmlAttributes(new { style = "width:5px;" });
                columns.Bound(c => c.PositionNumber);
                columns.Bound(c => c.ItemDescription);
                columns.Bound(c => c.ItemCode);
                columns.Bound(c => c.DrawingNumberIndex)
                columns.Bound(c => c.SalesUnit).ClientTemplate("#: OrderedQuantity # #: SalesUnit #");
                if (User.IsInRole(ActiveDirectoryGroups.Admin))
                {
                    columns.Bound(c => c.Id).ClientTemplate(
                        "<button type='button' class='btn btn-delete' onclick=\"deleteBtn('#: Id #')\"><span class='fa fa-trash'></span></button>"
                        ).Width(80).Title("");

                    columns.Bound(c => c.Id).ClientTemplate(
                    "<button type='button' class='btn btn-add' onclick=\"callAddChildItemModal('#: Id #')\"><span class='fa fa-plus'></span></button>"
                    ).Width(80).Title("");
                }
            })
            .AutoBind(true)
            .DataSource(ds =>
                ds.Ajax()
                .PageSize(50)
                .Read(read => read.Action("GetWhereSalesOrder", "SalesPosition", new { salesOrder = ViewBag.Item.SalesOrder1 }))
                .ServerOperation(false)
            )
            .Scrollable(a => a.Height("auto"))
            .ClientDetailTemplateId("templateArticle")
            .Selectable()
            .Pageable(p => p.Refresh(false).Numeric(true).Enabled(true))
            .Events(events => events.Change("toggleRow").DataBound("setSalesPositionColor"))
)

// Subgrid
<script id="templateArticle" type="text/kendo-tmpl">
    @(Html.Kendo().Grid<Models.Item>()
        .Name("grid#=Id#")
        .Columns(columns =>
        {
            columns.Template(@<p></p>).HtmlAttributes(new { style = "width:5px; text-align:left;" }).HeaderHtmlAttributes(new { style = "width:5px;" });
                    columns.Bound(c => c.ItemDescription);
                    columns.Bound(c => c.ItemCode);
                    columns.Bound(c => c.DrawingNumberIndex);
                    columns.Bound(c => c.Amount);
                    columns.Bound(c => c.Size);
                    columns.Bound(c => c.Id).ClientTemplate(
                       "# if (\\#: data.ItemState \\# == 1) { #" +
                        "<button type='button' class='btn btn-delete' onclick=\"deleteChildBtn('\\#: Id \\#')\"><span class='fa fa-trash'></span></button>" +
 "# } #").Width(80).Title("");
            }
        })
              .AutoBind(true)
              .DataSource(ds =>
                   ds.Ajax()
                        .Read(read => read.Action("GetWhereSalesPositionId", "SalesPositionItem").Data("{ id: '#: Id #' # if (cbhShowOnlyInCrate.checked) { # , onlyInCrate: true # } #}"))
                         .ServerOperation(false)
                         )
                .Events(events => events.DataBound("articleDataBound"))
                .ToClientTemplate()
    )

1 个答案:

答案 0 :(得分:1)

经过更多的尝试和错误,我找到了可行的解决方案

columns.Bound(c => c.Id).ClientTemplate(
      "\\# if (ItemState != 1) { \\#" +
      "<button type='button' class='btn btn-delete' onclick=\"deleteChildBtn('\\#: Id \\#','#: Id #')\"><span class='fa fa-trash'></span></button>" +
      "\\# } \\#"
);