Javascript 如何检查 Kendo Grid 是否对其进行了更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12720543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if a Kendo Grid has had changes made to it?
提问by muthucsm
How can I check if a Kendo Gridhas changes? I heard that there is a dirty
property, but I cant find it.
如何检查Kendo Grid是否有更改?我听说有一个dirty
属性,但我找不到它。
采纳答案by carlg
Added rows will have the dirty property set to true and so will updated rows. But, deleted rows are stored elsewhere (in the _destroyed collection). Pass this function the datasource of your grid to see if it has changes.
添加的行会将脏属性设置为 true,更新的行也会如此。但是,删除的行存储在其他地方(在 _destroyed 集合中)。将此函数传递给网格的数据源以查看它是否有更改。
function doesDataSourceHaveChanges(ds)
{
var dirty = false;
$.each(ds._data, function ()
{
if (this.dirty == true)
{
dirty = true;
}
});
if (ds._destroyed.length > 0) dirty = true;
return dirty;
}
回答by Chris Pietschmann
You can use the 'hasChanges' method on the Grid's underlying DataSource:
您可以在网格的底层数据源上使用“hasChanges”方法:
grid.dataSource.hasChanges();
$('#divGrid').data('kendoGrid').dataSource.hasChanges();
回答by Petur Subev
You can get notified and use the change event of the dataSource which will occur wherever you page/sort/group/filter/create/read/update/delete record.
您可以获得通知并使用数据源的更改事件,该事件将在您页面/排序/组/过滤器/创建/读取/更新/删除记录的任何地方发生。
To attach a handler to it use:
要将处理程序附加到它,请使用:
$('#YourGrid').data().kendoGrid.dataSource.bind('change',function(e){
//the event argument here will indicate what action just happned
console.log(e.action)// could be => "itemchange","add" or "remove" if you made any changes to the items
})
Update: If the user has updated any of the models .hasChanges() method of the dataSource will return true.
更新:如果用户更新了任何模型,dataSource 的 .hasChanges() 方法将返回 true。
回答by Ricardo Bermudez
grid.dataSource.hasChanges will let you know if the datasource has changed
grid.dataSource.hasChanges 将让您知道数据源是否已更改
if (datasource.hasChanges() === true) {
alert('yes');
} else {
alert('no');
}
回答by Marco
The most convinient way to go is to use datasource.hasChanges()
. However, this requires that the Id
field is defined in the schema.
最方便的方法是使用datasource.hasChanges()
. 但是,这需要Id
在架构中定义该字段。
From the docs:
从文档:
Checks if the data items have changed. Requires an [ID field] to be configured in schema.model.id, otherwise will always return true.
检查数据项是否已更改。需要在schema.model.id中配置[ID字段],否则将始终返回true。
If you do not have an Id field defined, you can use one of the countless ways to iterate over the data itself.
如果您没有定义 Id 字段,您可以使用无数种方法中的一种来迭代数据本身。
var isDataSourceDirty = function (ds) {
var dirty = ds._data.filter(function(x){
return x.dirty === true;
});
return dirty.length > 0 || ds._destroyed.length;
};
回答by Ann B. G.
worth a try:
值得一试:
var hasDirtyRow = $.grep(gridDataSource.view(), function(e) { return e.dirty === true; });
if (hasDirtyRow.length != 0)
{
// grid has dirty row(s)
}