简体   繁体   English

如何为剑道网格列设置自定义模板

[英]How to set cutom template for kendo grid columns

I need to set Kendo grid action button Icon based on value.我需要根据值设置剑道网格动作按钮图标。 My code as follows,我的代码如下,

function InitProductServicesGrid() {
    var prodServiceDataSource = new kendo.data.DataSource({
        transport: {
            type: "json",
            read:
                {
                    url: SERVER_PATH + "/LTSService/ProductsService.asmx/GetProductServiceDetailsList",
                    type: "POST",
                    contentType: 'application/json',
                    data: GetAdditonalData,
                    datatype: "json"
                },
            update:
            {
                url: SERVER_PATH + "/LTSService/ProductsService.asmx/SaveProductService",
                type: "POST",
                contentType: 'application/json',
                datatype: "json"
            }
        },
        schema: {
            data: function (result) {
                return JSON.parse(result.d);
            },
            model: {
                id: "Id",
                fields: {
                    Id: { type: "int" },
                    ServiceTime: { type: "string" },
                    IsActive: { type: "boolean"}
                }
            }
        },
        requestEnd: function (e) {
            if (e.type === "destroy") {
                var grid = $("#productServicesGrid").data("kendoGrid");
                grid.dataSource.read();
            }
        },
        error: function (e) {
            e.preventDefault();
            if (e.xhr !== undefined && e.xhr !== null) {
                var messageBody = e.xhr.responseJSON.Message;
                ShowGritterMessage("Errors", messageBody, false, '../App_Themes/Default/LtsImages/errorMessageIcon_large.png');
                var grid = $("#productServicesGrid").data("kendoGrid");
                grid.cancelChanges();
            }
        },
        pageSize: 20,
    });

    $("#productServicesGrid").kendoGrid({
        dataSource: prodServiceDataSource,
        sortable: true,
        filterable: false,
        pageable: true,
        dataBound: gridDataBound,
        editable: {
            mode: "inline",
            confirmation: false
        },
        columns: [
            { field: "Id", title: "", hidden: true },
            {
                field: "ServiceTime",
                title: "Time Standard",
                sortable: false,
                editor: function (container, options) {
                    var serviceTimeTxtBox = RenderServiceTime();
                    $(serviceTimeTxtBox).appendTo(container);
                },
                headerTemplate: '<a class="k-link" href="#" title="Time Standard">Time Standard</a>'
            },
            {
                title: "Action", command: [
                    {
                        name: "hideRow",
                        click: hideRow,
                        template: comandTemplate
                    }
                ],
                width: "150px"
            }
        ]
    });

}

I wrote a custom template function as follows,我写了一个自定义模板function如下,

function comandTemplate(model) {

    if (model.IsActive == true) {
        return '<a title="Hide" class="k-grid-hideRow k-button"><span class="k-icon k-i-lock"></span></a><a title="Hide"></a>';
    }
    else {
        return '<a title="Show" class="k-grid-hideRow k-button"><span class="k-icon k-i-unlock"></span></a><a title="Show"></a>';
    }
}

But when I debug the I saw the following value for model value.但是当我调试时,我看到了 model 值的以下值。

在此处输入图像描述

I followed this sample code as well.我也遵循了这个示例代码 here you can see, I also set the custom template like the sample code.在这里你可以看到,我也像示例代码一样设置了自定义模板。 Please help me to solve this.请帮我解决这个问题。 Why I can't access model IsActive value from comandTemplate function.为什么我无法从comandTemplate访问 model IsActive值。

Updated更新

When clicking hideRow action, I access the dataItem as follows.单击hideRow操作时,我按如下方式访问 dataItem。

function hideRow(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));

        if (dataItem.IsActive == true) {
            dataItem.IsActive = false;
        }
        else {
            dataItem.IsActive = true;
        }
}

Is there any possible way to access data from template function as above or any other way?是否有任何可能的方式从上述模板 function 或任何其他方式访问数据?

I would suggest a different approach because you can't access grid data while rendering and populating grid.我建议采用不同的方法,因为在渲染和填充网格时无法访问网格数据。

My suggestion is to use two actions and hide it based on the flag (in your case IsActive ).我的建议是使用两个动作并根据标志隐藏它(在你的情况下IsActive )。

Something like this: Custom command像这样的东西:自定义命令

NOTE : in visible function you can access item!注意:在visible的 function 中,您可以访问项目!

EDIT : you can access it and change it on dataBound traversing through all data.编辑:您可以访问它并在遍历所有数据的dataBound上更改它。 Check this example: Data bound检查这个例子:数据绑定

I don't see the advantage of relying on the grid commands.我看不到依赖网格命令的优势。 You can render any button you want yourself and and use the dataBound event to bind a click handler:你可以自己渲染任何你想要的按钮,并使用 dataBound 事件来绑定一个点击处理程序:

  $("#grid").kendoGrid({
    columns: [
      { field: "name" },
      {
        template: function(dataItem) {
            const isActive = dataItem.isActive;
            return `<a title=${isActive ? "Hide": "Show"} class="k-grid-hideRow k-button"><span class="k-icon k-i-${isActive ? 'lock' : 'unlock'}"></span></a>`
        }      
      }
    ],
    dataBound: function(e) {
      e.sender.tbody.find(".k-grid-hideRow").click(evt => {
        const row = evt.target.closest("tr")
        const dataItem = e.sender.dataItem(row)
        dataItem.set("isActive", !dataItem.isActive)
      })            
    },
    dataSource: [{ name: "Jane Doe", isActive: false }, { name: "Jane Doe", isActive: true }]
  });

Runnable Dojo: https://dojo.telerik.com/@GaloisGirl/eTiyeCiJ可运行 Dojo: https://dojo.telerik.com/@GaloisGirl/eTiyeCiJ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM