PDA

View Full Version : Add filterBy capability during data load instead of after



bradlymathews
17 Jul 2008, 9:25 AM
Animal came up with a nifty Store.loadRecords() override that calls filterBy() during data load rather than after, which requires the element to be re-rendered.

Adds the autoFilter config parameter which takes a function just like filterBy().


Ext.override(Ext.data.Store, {
loadRecords : function(o, options, success){
if(!o || success === false){
if(success !== false){
this.fireEvent("load", this, [], options);
}
if(options.callback){
options.callback.call(options.scope || this, [], options, false);
}
return;
}
var r = o.records, t = o.totalRecords || r.length;
if(!options || options.add !== true){
if(this.pruneModifiedRecords){
this.modified = [];
}
for(var i = 0, len = r.length; i < len; i++){
r[i].join(this);
}
if(this.snapshot){
this.data = this.snapshot;
delete this.snapshot;
}
this.data.clear();
this.data.addAll(r);
this.totalLength = t;
this.applySort();
this.applyFilter();
this.fireEvent("datachanged", this);
}else{
this.totalLength = Math.max(t, this.data.length+r.length);
this.add(r);
this.applyFilter();
}
this.fireEvent("load", this, r, options);
if(options.callback){
options.callback.call(options.scope || this, r, options, true);
}
},

applyFilter: function() {
if (typeof this.autoFilter == "function") {
this.filterBy(this.autoFilter);
}
}
});
Example usage:


var myStore = new Ext.data.Store({
reader: new Ext.data.JsonReader(),
proxy: new Ext.data.MemoryProxy(sitesData),
autoFilter: doShowRecord
});
- Brad

Alan Heywood
18 Aug 2009, 6:48 PM
I have two stores which are completely populated with data, two grids in master detail fashion, then I show the records in the second grid that are related to the selected record in the master grid.

The plugin is just what I needed.

I suggest that this is a common scenario and that the functionality should be built into the core.