为什么KendoUI Grid在调用options.error函数时不会回滚删除?

时间:2013-05-14 06:41:17

标签: kendo-ui datasource kendo-grid

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

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

2 个答案:

答案 0 :(得分:17)

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

所以你应该做的是添加一个error hander函数来调用网格中的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

答案 1 :(得分:2)

The ASP.NET-MVC equivalent solution to the OnaBai answer would be:

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

Please be aware that the cancelChanges event will be called upon an error on every CRUD request.