Ext JS - Combo only displays first letter of items in a store (SimpleStore)?
HI, I have a pretty simple Ext JS combobox that I'm just trying to bind to an array. Here is the config for the combo:
BPM.configs.ViewsCombo = {
xtype: 'combo',
emptyText: 'Select View',
disableKeyFilter: true,
triggerAction: 'all',
displayField: 'name',
mode: 'remote',
render: function(combo) {
this.store.load();
}
},
store: new Ext.data.SimpleStore({
proxy: new Ext.data.HttpProxy({
url: '/Service.svc/GetUserViewNames',
method: 'POST'
}),
root: 'GetUserViewNamesResult',
fields: ['name']
})
};
Here is response/json from Ajax call:
{"GetUserViewNam开发者_开发百科esResult":["something","tree"]}
But when i go to view the combo items, all I see is the letter 's' and 't' in the list. What gives ? is my returning array in the wrong format ?
Thanks so much.
well i figured out that the result needs to look like this :
{"GetUserViewNamesResult":[["something"],["tree"]]}.
which kinda sucks because now I have to change how my server side objects serialize :(
use this to change your array in to the required format
for ( var i = 0, c = cars.length; i < c; i++ ) {
cars[i] = [cars[i]];
}
referring to this how-to-bind-array-to-arrystore-in-order-to-populate-combo-in-extjs
Yes ExtJs still does not have a reader capable of dealing with lists of strings. On the server side (at least in Java, C#, etc.) this is often what you'll get when marshalling ENUM types.
I had to write my own class which is used in ExtJs 4.1 MVC style:
/**
* This extends basic reader and is used for converting lists of enums (e.g. ['a', 'b', 'c']) into lists of objects:
* [ {name: 'a'}, {name:'b'}, {name:'c'}]. All models using this type of reader must have a single field called name. Or you can
* pass a config option call 'fieldName' that will be used.
*
* This assumes that the server returns a standard response in the form:
* { result: {...., "someEnum" : ['a', 'b', 'c']},
* total: 10,
* success: true,
* msg: 'some message'
* }
*/
Ext.define('MY.store.EnumReader', {
extend: 'Ext.data.reader.Json',
alias: 'reader.enum',
//we find the Enum value which should be a list of strings and use the 'name' property
getData: function(data) {
var me = this;
//console.dir(data);
var prop = Ext.isEmpty(this.fieldName) ? 'name' : this.fieldName;
console.log('Using the model property: \''+ prop +'\' to set each enum item in the array');
try {
var enumArray = me.getRoot(data);
//console.dir(enumArray);
if (!Ext.isArray(enumArray)){
console.error("expecting array of string (i.e. enum)");
throw new Exception('not an array of strings - enum');
}
var enumToObjArray = Array.map(enumArray, function(item){
var obj = {};
obj[prop] = item;
return obj;
}
);
//console.dir(enumToObjArray);
var nodes = me.root.split('.');
var target = data;
var temp = "data";
Array.forEach(nodes, function(item, index, allItems){
temp += "['" + item + "']";
});
temp += " = enumToObjArray";
//console.log('****************' + temp + '*****************');
//evil 'eval' method. What other choice do we have?
eval(temp);
//console.dir(data);
return data;
}
catch(ex){
console.error('coudln\'t parse json response: ' + response.responseText);
return this.callParent(response);
}
}
}, function() {
console.log(this.getName() + ' defined');
});
Then, if you want to use this type of reader in your store you add:
requires: ['My.store.EnumReader'],
...
proxy: {
type: 'ajax',
url: ...
reader: {
type: 'enum',
精彩评论