21

How can I check if a Kendo Grid has changes? I heard that there is a dirty property, but I cant find it.

gitsitgo
  • 6,589
  • 3
  • 33
  • 45
muthucsm
  • 313
  • 1
  • 2
  • 4
  • If you have set `batch: true`, there is a `change` event which indicates whenever a change has occured. – Samuel Caillerie Oct 04 '12 at 15:31
  • Just noticed that you need to define the dataSource schema if you want the hasChanges() function to work(or the dirty property to appear on a dataItem). – Misi Jul 14 '15 at 14:13

6 Answers6

39

You can use the 'hasChanges' method on the Grid's underlying DataSource:

grid.dataSource.hasChanges();

$('#divGrid').data('kendoGrid').dataSource.hasChanges();
Chris Pietschmann
  • 29,502
  • 35
  • 121
  • 166
21

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.

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;
}
carlg
  • 1,802
  • 4
  • 25
  • 39
9

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.

Petur Subev
  • 19,983
  • 3
  • 52
  • 68
3

grid.dataSource.hasChanges will let you know if the datasource has changed

                            if (datasource.hasChanges() === true) {
                                alert('yes');
                            } else {
                                alert('no');
                            }
2

The most convinient way to go is to use datasource.hasChanges(). However, this requires that the Id field is defined in the schema.

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.

If you do not have an Id field defined, you can use one of the countless ways to iterate over the data itself.

var isDataSourceDirty = function (ds) {
    var dirty = ds._data.filter(function(x){
        return x.dirty === true;
    });
    return dirty.length > 0 || ds._destroyed.length;
};
Marco
  • 22,856
  • 9
  • 75
  • 124
1

worth a try:

var hasDirtyRow = $.grep(gridDataSource.view(), function(e) { return e.dirty === true; });
if (hasDirtyRow.length != 0)
{
     // grid has dirty row(s)
}
Ann B. G.
  • 304
  • 2
  • 6