Kendo Grid:禁用行编辑

use*_*358 3 asp.net-mvc grid jquery kendo-ui

我有一个可编辑的网格,并希望根据条件使一些行不可编辑.

任何人都可以请教我怎样才能做到这一点.

谢谢

Ona*_*Bai 7

开箱即用,没有允许控制每行版本的功能.您尝试编辑行时可以执行的是退出编辑.

edit一旦单元格进入编辑模式,就会触发一个事件.一旦发现您的条件成立,您可以做的就是关闭该单元格.

示例:我们有一个具有以下schema定义的网格:

schema  : {
    model: {
        fields: {
            Id       : { type: 'number' },
            FirstName: { type: 'string' },
            LastName : { type: 'string' },
            City     : { type: 'string' }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我们不希望让行这版CitySeattle.该edit处理程序应定义为:

var grid = $("#grid").kendoGrid({
    dataSource: ds,
    editable  : true,
    edit      : function (e) {
       // e.model contains the model corresponding to the row that is being edited.
        var data = e.model;
        if (data.City === "Seattle") {
            // Close the cell (exit edition mode)
           this.closeCell();
        }
        e.preventDefault();
    },
    pageable  : true,
    columns   :
            [
                { field: "FirstName", width: 90, title: "First Name" },
                { field: "LastName", width: 90, title: "Last Name" },
                { field: "City", width: 100 }
            ]
}).data("kendoGrid");
Run Code Online (Sandbox Code Playgroud)

问题是edit在单元格实际处于编辑模式后调用处理程序,因此关闭可能会产生一些闪烁,但在大多数情况下它应该可以工作.

第二个选项是将网格定义为不可编辑,editCell如果条件为真,则手动调用:

在这种情况下,您将gridas 定义为:

var grid = $("#grid").kendoGrid({
    dataSource: ds,
    editable  : false,
    pageable  : true,
    columns   :
            [
                { field: "FirstName", width: 90, title: "First Name" },
                { field: "LastName", width: 90, title: "Last Name" },
                { field: "City", width: 100 }
            ]
}).data("kendoGrid");
Run Code Online (Sandbox Code Playgroud)

然后click为单元格定义处理程序:

grid.tbody.on("click", "td", function (e) {
    // Close any possible cell already in edit mode
    grid.closeCell();
    // Find data corresponding to clicked cell
    var data = grid.dataItem($(e.target).closest("tr"));
    // Check condition
    if (data.City !== "Seattle") {
        // Enter edition mode
        grid.editCell(e.target);
    }
});
Run Code Online (Sandbox Code Playgroud)

当我检索datarow对应单击表格单元格,并在条件检查.如果条件匹配,那么我打开单元格.

虽然这没有闪烁,但它不是我的首选,因为您需要小心地触发save保存单元格,尽管您说网格不可编辑,但您正在编辑它.

在这里运行第一个实现的示例:http://jsfiddle.net/OnaBai/NWw7T/ ,第二个在这里:http://jsfiddle.net/OnaBai/NWw7T/1/

对于"incell"以外的版本模式,实现相同功能的最简单方法是创建自定义编辑按钮,该按钮控制行是否应该进入编辑模式.