ExtJs 4 get New, Updated and Deleted Records from a Grid Store

In case you are making changes to your Grid and want to query all the records that were added, updated or deleted the first you must get access to the Grid store from Grid Panel
  • grid = this;
  • store = grid.getStore();

For New Records

var updatedRecords = this.getStore().getNewRecords();

getNewRecords( ) : Ext.data.Model[]
Returns all Model instances that are either currently a phantom (e.g. have no id), or have an ID but have not yet been saved on this Store (this happens when adding a non-phantom record from another Store into this one)

For Updated Records

var updatedRecords = this.getStore().getUpdatedRecords();

getUpdatedRecords( ) : Ext.data.Model[]
Returns all Model instances that have been updated in the Store but not yet synchronized with the Proxy

For Deleted Records

var updatedRecords = this.getStore().getRemovedRecords();

getRemovedRecords( ) : Ext.data.Model[]
Returns any records that have been removed from the store but not yet destroyed on the proxy.


Tip: Please make sure that you specify the idProperty in the store's model.

If you forget to specify an idProperty then all records in the store will be considered New Records and when you try to get updated Records will get an empty List or your store sync will call the create api instead of update.

idProperty : String
The name of the field treated as this Model's unique id. Defaults to 'id'.

How to get the store's New/Updated records and create an Ajax Request

 parms = []; 
 var updatedRecords = this.getStore().getUpdatedRecords();
 Ext.each(updatedRecords,function(record){
  parms.push(record.data);
 });
 var newRecords = this.getStore().getNewRecords();
 Ext.each(newRecords,function(record){
  parms.push(record.data);
 });
   
 if(parms.length > 0){
  this.el.mask('Saving Your Changes...');
  Ext.Ajax.request({
   url : '/MyAJAXRequest',
   params : {
    action: 'addUpdateRecords',
    records : Ext.encode(parms)
   },
   scope : this,
   success : this.onSaveChangesSuccess,
   failure : this.onSaveChangesFailure
  });   
 }

Recommended Reading

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.