Kendo ui 为什么调用options.error函数时KendoUI网格不回滚删除?

Kendo ui 为什么调用options.error函数时KendoUI网格不回滚删除?,kendo-ui,datasource,kendo-grid,Kendo Ui,Datasource,Kendo Grid,我在这里放了一把小提琴来说明这个问题 只要删除一条记录,它就会在我调用destroy函数中的options.error时回滚删除操作 为什么网格没有回滚 问候, 斯科特 标记: <div id="KendoGrid"></div> 发出错误信号是不够的。假设删除记录时出错是不够的,因为KendoUI不知道该记录是否已在服务器中实际删除,并且回复是产生错误的回复。所以KendoUI方法是一种保守的方法:你必须决定做什么并明确地说出来: 因此,您应该添加一个hander函

我在这里放了一把小提琴来说明这个问题

只要删除一条记录,它就会在我调用destroy函数中的options.error时回滚删除操作

为什么网格没有回滚

问候,

斯科特

标记:

<div id="KendoGrid"></div>

发出错误信号是不够的。假设删除记录时出错是不够的,因为KendoUI不知道该记录是否已在服务器中实际删除,并且回复是产生错误的回复。所以KendoUI方法是一种保守的方法:你必须决定做什么并明确地说出来:

因此,您应该添加一个hander函数来调用网格中的

守则是:

_dataSource = new kendo.data.DataSource({
    transport: {
        read: function(options) {
            options.success(_data);
            console.log('Read Event Has Been Raised');
        },
        destroy: function (options) {
            options.error(new Error('Error Deleting User'));
            console.log('Destroy Event Has Been Raised');
        }
    },
    schema: {
        model: {
            id: "Users_ID",
            fields: {
                Users_ID: { editable: false, nullable: true },
                Users_FullName: { type: "string", validation: { required: true } },
                Users_Role: { type: "string", validation: { required: true } }
            }
        }
    },
    error: function(a) {
        $('#KendoGrid').data("kendoGrid").cancelChanges();
    }
});
<script type="text/javascript">
function cancelChanges(e) {
    e.sender.cancelChanges();
}
</script>

@Html.Kendo().Grid<MyClass>()
.DataSource(dataSource =>
    dataSource
    .Ajax()
    .Read(read => read.Action("Read", "MyController"))
    .Destroy(destroy => destroy.Action("Destroy", "MyController"))
    .Events(evt => evt.Error("cancelChanges"))
)
[...]

这里更新的JSFIDLE:

与OnaBai答案相当的ASP.NET-MVC解决方案是:

_dataSource = new kendo.data.DataSource({
    transport: {
        read: function(options) {
            options.success(_data);
            console.log('Read Event Has Been Raised');
        },
        destroy: function (options) {
            options.error(new Error('Error Deleting User'));
            console.log('Destroy Event Has Been Raised');
        }
    },
    schema: {
        model: {
            id: "Users_ID",
            fields: {
                Users_ID: { editable: false, nullable: true },
                Users_FullName: { type: "string", validation: { required: true } },
                Users_Role: { type: "string", validation: { required: true } }
            }
        }
    },
    error: function(a) {
        $('#KendoGrid').data("kendoGrid").cancelChanges();
    }
});
<script type="text/javascript">
function cancelChanges(e) {
    e.sender.cancelChanges();
}
</script>

@Html.Kendo().Grid<MyClass>()
.DataSource(dataSource =>
    dataSource
    .Ajax()
    .Read(read => read.Action("Read", "MyController"))
    .Destroy(destroy => destroy.Action("Destroy", "MyController"))
    .Events(evt => evt.Error("cancelChanges"))
)
[...]

功能更改(e){
e、 sender.cancelChanges();
}
@Html.Kendo().Grid()
.DataSource(DataSource=>
数据源
.Ajax()
.Read(Read=>Read.Action(“Read”,“MyController”))
.Destroy(Destroy=>Destroy.Action(“Destroy”、“MyController”))
.Events(evt=>evt.Error(“取消更改”))
)
[...]

请注意,每次CRUD请求出错时都会调用cancelChanges事件。

您确定调用了
destroy
函数吗?调用得好。好的,我已经修改过了,现在它被调用了。但仍然是相同的行为,没有回滚,而且如果删除了多条记录,则销毁事件会引发太多次。。。好朋友。现在为什么毁灭事件被提起这么多次。。。每次我按下删除按钮,它就会再次被触发。看看控制台,看看我的意思:那实际上是该版本中的一个bug。若你们检查一下最新的你们会发现它是有效的啊,是的,我一定是从一把旧小提琴开始的。那太好了。再次感谢您的支持。非常感谢!!不需要选择kendoGrid。引用就在那里:this.cancelChanges();美好的使用“e.sender”比像接受的答案中那样使用jquery获取对网格的引用要好。@AugustoBarreto:
e.sender
将引用数据源,而不是网格。网格将位于
e.sender.options.table.parent('div')[0]
,这看起来并没有太好;)@马沙尔先生,好的。谢谢