Iterate an ExtJS store, add result to Ajax request

Iterate an ExtJS store, add result to Ajax request

This sample shows how an ExtJS store can be iterad and add some of the results as an JSON string to your Ext.Ajax request. Useful if you want to add results of more than one store or just some fields to the request.

How it is done

var Grid = this.getMyGridGrid(); // your grid
var Store = Grid.getStore();     // your grid's store    
var selected = Store.getRange();  // getRange = select all records

var Excerpt = []; // start with empty array
Ext.each(selected, function(item) {
    // add the fields that you want to include
    var Obj = {
        first_field: item.get('first_field'),  
        second_field: item.get('second_field'),
        third_field: item.get('third_field')
    };
    Excerpt.push(Obj); // push this to the array
}, this);

Ext.Ajax.request({
   url: 'server/execute_this_url',  // your url
   method: 'POST',
   params: {
      groups: Ext.encode(Excerpt),  // include the array in the params
   },
   success: function(result, action) {
      // decode the result and do something with it...
      var jsonData = Ext.decode(result.responseText);

      // do something with returned records 
      var jsonRecords = jsonData.records;
      Ext.iterate(jsonRecords, function(item) {
          var rec = Store.findExact('some_key', item.some_key);
          if (rec > -1) {
             Store.getAt(rec).set('field_found', true);
          }
      });
  }
});

Dont forget to decode the string on the server.

More from same category