8

我在这里放了一个小提琴来证明这个问题。

http://jsfiddle.net/codeowl/fmzay/1/

只需删除一条记录,它应该回滚删除,因为我从destroy函数内部调用options.error。

为什么网格不回滚?

问候,

斯科特

标记:

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

JS:

var _data = [
        { Users_ID: 1, Users_FullName: 'Bob Smith', Users_Role: 'Administrator'  },
        { Users_ID: 2, Users_FullName: 'Barry Baker', Users_Role: 'Viewer'  },
        { Users_ID: 3, Users_FullName: 'Bill Cow', Users_Role: 'Editor'  },
        { Users_ID: 4, Users_FullName: 'Boris Brick', Users_Role: 'Administrator'  }
    ],
    _dataSource = new kendo.data.DataSource({ 
        data: _data,
        destroy: function (options) {
            options.error(new Error('Error Deleting User'));
        }
    });

$('#KendoGrid').kendoGrid({
    dataSource: _dataSource,
    columns: [
        { field: "Users_FullName", title: "Full Name" },
        { field: "Users_Role", title: "Role", width: "130px" },
        { command: ["edit", "destroy"], title: "&nbsp;", width: "180px" }
    ],
    toolbar: ['create'],
    editable: 'popup'
});
4

2 回答 2

18

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

所以你应该做的是在网格中添加一个error调用 a 的处理函数。cancelChanges

代码将是:

_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();
    }
});

更新的 JSFiddle 在这里:http: //jsfiddle.net/OnaBai/fmzay/3

于 2013-05-14T12:15:32.210 回答
3

OnaBai 答案的 ASP.NET-MVC 等效解决方案是:

<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"))
)
[...]

请注意,在每个 CRUD 请求出现错误时都会调用cancelChanges事件。

于 2015-11-12T11:02:24.657 回答