【问题标题】:Client template not working in kendo grid客户端模板在剑道网格中不起作用
【发布时间】:2019-05-14 15:07:39
【问题描述】:

我的剑道网格有一些列。我正在尝试使用客户端模板有条件地隐藏一个复选框。当我使用客户端模板时,它工作正常。上面的代码在条件为真时隐藏元素。

 @(Html.Kendo().Grid<Spectrum.Model.CustomerInterestItem>()
                    .Name("customerInterestInfoGrid")
                    .AutoBind(true)
                    .Columns(columns =>
                    {
                        columns.Bound(c => c.ID).Width(200);
                                                    columns.Bound(c => c.SubscriptionEndDate).Title("End Date").Format("{0:MMM dd, yyyy}").Width(130);
                        columns.Bound(c => c.Notes).Width(200);

                        columns.Template(@<text></text>)
                        .ClientTemplate("#if (!InterestTypeID == 99) {#"
                        +"<input name='chkSubscribed' class='subscribedClass' type='checkbox' data-bind='checked: IsSubscribed' #= IsSubscribed ? checked='checked' : '' #/>"
                        + "#} #").Width(130).Title("Subscribed");

                        columns.Command(command => { command.Edit(); command.Destroy(); });
                    })
                    .DataSource(dataSource => dataSource
                    .Ajax()
                   .Model(model => { model.Id(c => c.ID); model.Field(f => f.Notes).Editable(false); model.Field(f => f.IsSubscribed).Editable(false); })
                    .Read(cfg => cfg.Action("testGridRead", "Customer").Data("customerIDData"))
                     .Update(cfg => cfg.Action("EditInterestItem", "Customer"))
                    .Destroy(cfg => cfg.Action("DeleteInterestItem", "Customer"))
                    .ServerOperation(false)
                    )

                    .Pageable(pageable => pageable
                    .Enabled(true)
                    .PageSizes(new int[3] { 10, 25, 50 })
                    .Refresh(true))
                    .Scrollable()
                    .Selectable()
                    .Sortable()
    )

以上代码将隐藏复选框。但我的问题是,当我单击更新按钮时,复选框变得可见。我不知道原因。

【问题讨论】:

  • 在模板开头添加# console.log(data) #并检查数据。

标签: asp.net-mvc-4 kendo-ui kendo-grid kendo-asp.net-mvc kendo-ui-mvc


【解决方案1】:

试试下面的。您可以在客户端模板本身中给出条件

    columns.Bound(c => c).Width(50).Title("Subscribed").ClientTemplate("<input type='checkbox' name='chkSubscribed' class='subscribedClass' #=InterestTypeID =='99' ? \"checked='checked' \" : '' # />");

【讨论】:

    【解决方案2】:

    Kendo 将在编辑模式下根据您的模型数据类型添加一个编辑器。所以要阻止 Kendo 添加编辑器:-

    1. 您可以在模型中将该字段设置为可编辑的 false。但是如果你 将其设置为可编辑-> false 您需要显式发送值到 控制器网格模型不会传递值。
    2. 使用与您的客户端模板相同的代码将editor template 绑定到该列。一世 在 JavaScript 中创建了一个示例代码 sn-p 供您参考。
    <div id="example">
        <div id="grid"></div>
        <script>
        $(document).ready(function () {
            crudServiceBaseUrl = "https://demos.telerik.com/kendo-ui/service",
            dataSource = new kendo.data.DataSource({
                transport: {
                 read:  {
                     url: crudServiceBaseUrl + "/Products",
                     dataType: "jsonp"
                 },
                 update: {
                     url: crudServiceBaseUrl + "/Products/Update",
                     dataType: "jsonp"
                 },
                 destroy: {
                     url: crudServiceBaseUrl + "/Products/Destroy",
                     dataType: "jsonp"
                 },
                 create: {
                     url: crudServiceBaseUrl + "/Products/Create",
                     dataType: "jsonp"
                 },
                 parameterMap: function(options, operation) {
                     if (operation !== "read" && options.models) {
                         return {models: kendo.stringify(options.models)};
                     }
                 }
             },
             batch: true,
             pageSize: 20,
             schema: {
                 model: {
                     id: "ProductID",
                     fields: {
                         ProductID: { editable: false, nullable: true },
                         ProductName: { validation: { required: true } },
                         UnitPrice: { type: "number", validation: { required: true, min: 1} },
                         Discontinued: { type: "boolean" },
                         UnitsInStock: { type: "number", validation: { min: 0, required: true } }
                     }
                 }
             }
         });
    
        $("#grid").kendoGrid({
                 dataSource: dataSource,
                 pageable: true,
                 height: 550,
                 toolbar: ["create"],
                 columns: [
                     "ProductName",
                     { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "120px" },
                     { field: "UnitsInStock", title:"Units In Stock", width: "120px" },
                     { field: "Discontinued", title: "Discontinued", width: "80px", 
                       template: "#if (UnitPrice > 20) {# <input name='chkSubscribed' class='subscribedClass' type='checkbox' data-bind='checked: Discontinued' #= Discontinued ? checked='checked' : '' #/> #} #",
                       editor: customBoolEditor },
                     { command: ["edit", "destroy"], title: "&nbsp;", width: "250px" }],
                 editable: "inline"
             });
         });
    
        function customBoolEditor(container, options) {
            var guid = kendo.guid();
            if(options.model.UnitPrice > 20) {                 
                $('<input class="k-checkbox" id="' + guid 
                   + '" type="checkbox" name="Discontinued" data-type="boolean" data-bind="checked:Discontinued">').appendTo(container);
                $('<label class="k-checkbox-label" for="' + guid + '">&#8203;</label>').appendTo(container);                  
           }
        }
        </script>        
    </div>
    

    【讨论】:

    • 请说明如何在我的项目中调用customBoolEditor函数。
    • 因为你使用的是脚本
    • 您需要添加一个Editor Template。这涉及使用列属性 .EditorTemplateName("_EditorPartialViewName") 或在模型属性上使用 UIHint 注释
    • 我还有一个关于剑道网格的问题。我的网格单元格显示 html 标签而不是数据。你对此有什么想法吗
    • 您可以使用 dojo 并在上面的 sn-p 中重现问题,或者使用您的代码 sn-p 更新您上面的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2016-06-06
    • 1970-01-01
    相关资源
    最近更新 更多