【问题标题】:How to set cutom template for kendo grid columns如何为剑道网格列设置自定义模板
【发布时间】:2020-04-06 16:19:34
【问题描述】:

我需要根据值设置剑道网格动作按钮图标。我的代码如下,

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"
            }
        ]
    });

}

我写了一个自定义模板函数如下,

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>';
    }
}

但是当我调试时,我看到了模型值的以下值。

我也关注了this sample code。在这里你可以看到,我也像示例代码一样设置了自定义模板。请帮我解决这个问题。为什么我无法从 comandTemplate 函数访问模型 IsActive 值。

更新

点击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;
        }
}

是否有任何可能的方式从上述模板函数或任何其他方式访问数据?

【问题讨论】:

    标签: javascript jquery kendo-ui kendo-grid


    【解决方案1】:

    我会建议一种不同的方法,因为在渲染和填充网格时您无法访问网格数据。

    我的建议是使用两个操作并根据标志隐藏它(在您的情况下为 IsActive)。

    类似这样的:Custom command

    注意:在visible函数中你可以访问项目!

    编辑:您可以访问它并在dataBound 上遍历所有数据进行更改。 检查这个例子:Data bound

    【讨论】:

    • 请看我更新的问题。当我单击hideRow 时,我可以访问数据。是否有任何可能的方法来对模板中的访问值做同样的事情?
    • 您可以通过点击和可见事件访问它。您需要在填充和渲染网格时访问它,我认为这是不可能的。
    • 你可以在网格的数据绑定事件上做,我更新了答案
    • 正如我之前所说的......你不能command的模板内访问dataItem
    • 在你的情况下我会这样做:dojo.telerik.com/OvobaKEg
    【解决方案2】:

    我没有看到依赖网格命令的优势。您可以自己渲染任何您想要的按钮,并使用 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 }]
      });
    

    可运行的道场:https://dojo.telerik.com/@GaloisGirl/eTiyeCiJ

    【讨论】:

    • 为什么这在 IE 浏览器中不起作用。我认为是因为数组功能click(evt =&gt;。还有其他方法吗?
    • 请看我的完整代码:pastebin.com/idEcS9CZ。在那里const isActive = dataItem.isActive; 值显示为未定义,我可以知道为什么吗?请帮帮我
    • @Adam console.log 您的数据项,检查那里的内容并记住属性区分大小写。我的道场基于@dev_in_progress 的道场,它以小写字母开头。
    • 它显示为Undefined 请看这张图片i.imgur.com/0I2ZAkB.png 我想我可以通过操作命令访问dataitem :( 请同时查看我的完整代码
    • 我按如下方式尝试了您的代码,pastebin.com/2cAxj8Mb。我在动作命令中添加了动作,它也返回Undefined。这是主要问题。请看pastebin代码(我有点改变你的代码)
    猜你喜欢
    • 2014-10-21
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多