Blog Archive

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


7 comments :

  1. If you define proxy update,delete,create when You call store.sync() , automatically the store call the wright methods for each data that is dirty.

    ReplyDelete
  2. i need somehow clear all records returned by getNewRecords() function in store exception listener ... Because i use autoSync on my store, when something bad happens, i need clear those not-synced records so store will not try sync them again and again with any next operation..

    any idea how to do it ? any advice will be highly appreciated :)

    ReplyDelete
  3. It is really nice for me to see you and your great hardwork again.Every piece of your work look excellent.Looking forward to learing more from you!

    ReplyDelete
  4. Hi,

    I am using a separate form to insert/update records this based on security rules.

    Is it also possible to add a (temporary) record to the store with a specific id.
    Keep this then as new.

    Then do an Ajax call and go to the database do an insert to the database with the form data and then return the full updated record and update this (temporary) record in the store?


    Let me know.
    Thx.

    ReplyDelete
  5. Can you share code in zip .. it will highly appreciated

    ReplyDelete