jquery - 我还需要什么才能使剑道网格自定义工具栏命令正常工作?

标签 jquery kendo-ui kendo-grid

我需要向我的 kendo-grid 添加自定义工具栏 命令,因此我搜索了kendo-ui > 关于 grid#configuration-toolbar 的文档我在哪里发现:

If an Array value is assigned (to a toolbar property), it will be treated as the list of commands displayed in the grid's Toolbar. Commands can be custom or built-in ("cancel", "create", "save", "excel", "pdf").

我为我的工具栏创建了一个自定义命令(也在这个问题 Adding custom button to KendoGrid Toolbar Issue 中建议)

toolbar: [
    {
        name: "copyRows",
        text: "Copy Rows",
        click: function (e) {
            alert('test');
        }
    },
],

具有单击事件处理程序的附加属性,如命令文档 columns.command.click Function 中所述:

The JavaScript function executed when the user clicks the command button. The function receives a jQuery Event as an argument.

...但是它不会触发单击事件,并且警报不会显示。

你知道我在这里缺少什么吗?

我测试的完整代码如下所示:

$("#grid").kendoGrid({
    columns: [{
        field: "name"
    }, ],
    editable: true,
    toolbar: [{
        name: "create",
        text: "New Row"
    }, {
        name: "copyRows",
        text: "Copy Rows",
        click: function (e) {
            alert('test');
        }
    }, ],
    dataSource: {
        data: [{
            name: "Jane Doe"
        }],        
    }
});

jsfiddle for custom toolbar command

最佳答案

我已经找到解决办法了。由于一些奇怪的未记录的原因,toobar 命令与 column 命令不是同一命令,并且不能以相同的方式进行自定义。它们唯一的共同点是工具栏命令可以调用列命令。工具栏中好像没有点击事件:

$("#grid").kendoGrid({
    columns: [{
        field: "name"
    },{
        command: [{
        name: "customCommand",
        text: "Column Command",
        click: function(e){
            alert('Column Command');
        }
    }]
    } ],
    editable: true,
    toolbar: [{
        name: "create",
        text: "New Row"
    }, {
        // This actually calls the column command with the same name.
        name: "customCommand",
        text: "Toolbar Command",
        // The click event never gets fired.
        click: function (e) {
            alert('Toolbar Command');
        }
    }, ],
    dataSource: {
        data: [{
            name: "Jane Doe"
        }],        
    }
});

demo at jsfiddle

但我不想在每一行中都有一个额外的按钮,只是为了使工具栏命令起作用,因此解决方案是使用带有自定义事件处理程序的工具栏自定义模板:

$("#grid").kendoGrid({
    columns: [{
        field: "name"
    }],
    editable: true,
    toolbar: [{
        template:
            '<a class="k-button k-button-icontext k-grid-add" href="\\#"><span class="k-icon k-add"></span>New Row</a>' +
            '<a class="k-button k-grid-custom-command" href="\\#">Toolbar Command</a>'
    }],
    dataSource: {
        data: [{
            name: "Jane Doe"
        }],
    }
});

$('a.k-grid-custom-command').click(function (e) {
    alert('Toolbar Command');
});

demo at jsfiddle

关于jquery - 我还需要什么才能使剑道网格自定义工具栏命令正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28024189/

相关文章:

jquery - 如何使用 jQuery 在 HTML 中切换隐藏/显示最接近的 UL?

javascript - 无法将数据从跨域 json 分配给 Ajax 中的变量

php - 解析 JSON 数据时 Highcharts 数据显示问题

kendo-ui - Kendo Grid - 查找单击单元格的列和行索引

javascript - 触发行特定的kendo ui Angular 2网格鼠标悬停事件

javascript - 验证 : remove custom success message when error occurs

asp.net-mvc-4 - Kendo DatePicker 格式验证和错误消息

javascript - 如何在剑道网格中自动启用或禁用滚动条?

javascript - 如何通过按钮选择或不选择剑道网格

javascript - 下拉字段选择/返回 null kendo ui grid - mvc