jquery – 我还需要什么来使kendo网格的自定义工具栏命令工作?

我需要向我的kendo-grid添加一个自定义工具栏命令,所以我搜索了关于 grid#configuration-toolbar的kendo-ui文档,我发现了:

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

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

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

…但是它不会触发click事件并且警报不会显示.

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

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

$("#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命令与列命令不是相同的命令,并且不能以相同的方式自定义.他们唯一的共同点是工具栏命令可以调用列命令.工具栏中似乎没有点击事件:

$("#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

相关文章
相关标签/搜索