【问题标题】:Preventing editing a row in kendo grid?防止在剑道网格中编辑一行?
【发布时间】:2013-03-31 08:48:43
【问题描述】:

我正在使用剑道网格,在编辑一行时,我正在检查该行是否可编辑。因此,如果所选行不可编辑,如何使其不可编辑。我正在检查 edit 网格功能.

代码

$("#grid").kendoGrid({
    dataSource : ds,
    selectable : "multiple",
    sortable   : true,
    filterable : false,
    reorderable: true,
    scrollable : false,
    toolbar    : ["create"],
    columns: [
                { field: "event", width: "120px", title: "Event Type"},
                { field: "event_id", width: "120px", title: "Event ID"},
                { field: "addr_no_or_type", width: "120px", title:"Address"},
                { field: "event_rate", width: "100px", title: "Rate"},
                { field: "sched_date", width: "100px", title: "Scheduled"},
                { field: "complete_date", width: "100px", title:"Completed"},
                { field: "serial_no", width: "100px", title: "Serial #"},
                { command: ["edit", "destroy"], title: "Options", width: "170px"}
            ],
    editable: "inline",
    edit    : function(e){
        selectedRowIndex        =   $("#grid").data("kendoGrid").select().index();
        if (selectedRowIndex >= 0) {
            var grid            =   $("#grid").data("kendoGrid");
            var selectedItem    =   grid.dataItem(grid.select());
            var slno            =   selectedItem.serial_no;
            if(slno!=0){
                grid.cancelRow();
            }
        }
    }
});

但是当我使用它时,我在控制台中收到以下错误。

Uncaught TypeError: Cannot call method 'delegate' of null 

有人可以提出解决方法吗。谢谢。

【问题讨论】:

  • 基本上我会建议防止使用 dataBound 事件进行编辑,但这取决于当前的网格配置 - 你能分享网格代码吗?
  • 我已经编辑了代码,你能看一下吗
  • 尝试谷歌搜索 KendoUI 只读行

标签: javascript jquery html kendo-ui kendo-grid


【解决方案1】:

在当前情况下,我建议使用 dataBound 事件来遍历 dataSource view 数据并检查当前记录是否满足给定条件以禁用它的编辑按钮:

function onDataBound(e) {
    //this solution makes all rows editable / not editable initially
    var grid = e.sender;
    var data = grid.dataSource.view();

    for (var i = 0; i < data.length; i++) {
        //check your custom condition
        if (data[i].OrderID % 2 == 0) {
            var editButton = grid.tbody.find("tr[data-uid='" + data[i].uid + "'] .k-grid-edit");
            editButton.addClass("k-state-disabled").removeClass("k-grid-edit");
            //or
            //grid.tbody.find("tr[data-uid='" + data[i].uid + "'] .k-grid-edit").remove();
        }
    }
}

【讨论】:

  • 应该被屏蔽为答案!
【解决方案2】:

同意,即使我通过 Change 事件实现了行禁用功能。代码如下:

 function onRowSelect(val) {
    var curCell = $("#abc").find(".k-state-selected");
    if (curCell[0].innerText.indexOf('ABCD')>-1) {
        curCell[0].disabled = true;
    }

...


@(Html.Kendo().Grid<xyz>()
.Name("abc")  
.Selectable()   
.Events(e=>e.Change("onRowSelect"))

【讨论】:

    【解决方案3】:
    function onGridCellEdit(e) {
    
        this.closeCell();
    }
    

    此函数将在编辑行时调用,一旦此函数被命中,将不允许更改。

    【讨论】:

    • 您好,欢迎来到 Stack Overflow!请添加一些注释,而不是添加唯一的代码 sn-p。
    • 嘿嗨,这个函数将在编辑行时被调用,一旦这个函数被命中,这将不允许改变..
    • 它可以工作,但会在控制台中引发错误。这样效果更好:this.cancelRow();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多