PDA

View Full Version : Ext.ux.SingleArrayStore



Dave.Sanders
17 Sep 2009, 7:53 AM
Hey all, I had some simple array data that I needed for a dropdown that I couldn't turn into a multi-dimensional array since the data was being generated elsewhere. Seeing another question on the forums about how to plug single-dimension arrays into a store, I decided to create a dirt-simple ux to handle this.


Ext.ux.SingleArrayReader = Ext.extend(Ext.data.ArrayReader, {
readRecords : function(o){
var a = [];
for (var i=0; i<o.length; i++) {
a[a.length] = [i, o[i]];
}
return Ext.ux.SingleArrayReader.superclass.readRecords.call(this, a);
}
});

Ext.ux.SingleArrayStore = Ext.extend(Ext.data.ArrayStore, {
constructor: function(config){
if (!config) {config = {};}
if (!config.fields) {
config.fields = ['id', 'text'];
}
//bypass the arraystore constructor on purpose
Ext.data.ArrayStore.superclass.constructor.call(this, Ext.apply(config, {
reader: new Ext.ux.SingleArrayReader(config)
}));
},
loadData : function(data, append){
Ext.data.ArrayStore.superclass.loadData.call(this, data, append);
}
});

Usage is very simple:


var myStore = new Ext.ux.SingleArrayStore();

If you don't pass any config, then it will create the fields "id" and "text" for you. Then you reference those in a combo the same way you reference any other store.

Let me know if you have any bugs or questions.

Dave

Condor
17 Sep 2009, 12:00 PM
An ArrayStore configured with expandData:true would also be able to read single-dimension array data.

(but it would create a store with only one field named 'field1')

Dave.Sanders
17 Sep 2009, 12:10 PM
Ah thanks. That would have worked too. Oh well.

Dave

devtig
9 Nov 2010, 3:25 AM
Where is expandData documented? I have a combo with a single field array as well. My data=['a', 'b', 'c']. Nothing more.

Condor
9 Nov 2010, 3:52 AM
expandData should be documented in ArrayStore, but it seems to be missing (care to post it in the Bugs section?).

But for static data you can also use:

var store = Ext.StoreMgr.lookup(['a', 'b', 'c']);
or in a combobox:

store: ['a', 'b', 'c']
(which creates a store with one field named 'field1')

devtig
9 Nov 2010, 4:42 AM
Thanks for the input. I posted the api doc bug here: http://www.sencha.com/forum/showthread.php?115162-config-option-expandData-for-ArrayStore-is-missing-in-API-docs&p=536710#post536710