New useful store functions for Ext JS

Wed 10th Nov 2010 -- Lee Rowlands

Some useful additional store functions

  • getEncodedData
  • setAll
  • getBetween
  • min
  • max

Install as follows

Ext.override(Ext.data.Store, {
  getEncodedData: function (modifiedOnly){
    if(modifiedOnly === true) {
      c = this.getModifiedRecords();
    } else {
      c = this.getRange();
    }
    var p = new Array();
    if(c.length){
      for(i=0; i<c.length; i++) {
        p[i] = c[i].data;
      }
      return Ext.encode(p);
    } else {
      return '';
    }
  },
  setAll: function(f,v){
    this.each(function(r){
      r.set(f,v);
    })
  },
  getBetween: function(value, lower, upper){
    var rc;
    this.each(function(r){
      var d = r.data;
      if(value >= (1 * d[lower]) && value <= (1 * d[upper])) {
        rc = r;
        return false;
      }
    });
    return rc;
  },
  max : function(property,start,end){
    var rs = this.data.items, v = 0;
    start = start || 0;
    end = (end || end === 0) ? end : rs.length-1;
    for(var i = start; i <= end; i++){
      v = Math.max(v, (1 * rs[i].data[property]) || 0);
    }
    return v;
  },
  min : function(property,start,end){
    var rs = this.data.items, v = 0;
    start = start || 0;
    end = (end || end === 0) ? end : rs.length-1;
    for(var i = start; i <= end; i++){
      v = Math.min(v, (1 * rs[i].data[property]) || 0);
    }
    return v;
  }
})

 

Usage:

getEncodedData

Returns string encoded data suitable for submission in Ajax request.

Parameters:

  • modifiedOnly - if true only returns modified records

getBetween

Returns array of records where value between lower and upper

Parameters:

  • value: the field to search in
  • lower: lower bound
  • upper: upper bound

max

Returns maximum value for field property between records start and end

Parameters:

  • property: field value to return max from
  • start: start index of records to search (default 0)
  • end: end index of records to search (default store.getCount())

setAll

Sets value of field f to value v for all records in the store

Parameters:

  • f: field to update
  • v: value to set field to