javascript 使用本地 JSON 数据填充 ExtJS 3.4 组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14022335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate ExtJS 3.4 combobox with local JSON data
提问by Calypo Gunn
I'm using ExtJS 3.4 and I need to populate a combobox with the following data:
我正在使用 ExtJS 3.4,我需要使用以下数据填充组合框:
"[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]"
Which comes form a previous XMLHttpRequest, and I've got it stored in a variable so:
它来自以前的 XMLHttpRequest,我将它存储在一个变量中,因此:
my_variable = "[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]"
So, I have the following ExtJS 3.4 combobox:
所以,我有以下 ExtJS 3.4 组合框:
cbxSelDomini = new Ext.form.ComboBox({
hiddenName: 'Domini',
name: 'nom_domini',
displayField: 'nom_domini',
valueField: 'cod_domini',
mode: 'local',
triggerAction: 'all',
listClass: 'comboalign',
typeAhead: true,
forceSelection: true,
selectOnFocus: true,
store: mystore
});
This combobox is suposed to get data from an Ext.data.Store I named "mystore":
这个组合框用于从我命名为“mystore”的 Ext.data.Store 获取数据:
store: mystore = new Ext.data.Store({
autoload: true,
reader: new Ext.data.ArrayReader(
{
idIndex: 0 // id for each record will be the first element
}),
data: dataprova,
fields: [
{type: 'integer', name: 'cod_domini'},
{type: 'string', name: 'nom_domini'}
]
}),
My first problem is that at first instance, data is not loaded to dataStore, even telling explicitly: mystore.loadData(my_variable);
我的第一个问题是,首先,数据没有加载到 dataStore,甚至明确告诉: mystore.loadData(my_variable);
Can somebody try to tell me what I'm doing wrong? In fireBug I get errors like "this.data is not defined", "this.reader is not defined", or "b is undefined" and "h is undefined".
有人可以尝试告诉我我做错了什么吗?在 fireBug 中,我收到诸如“this.data 未定义”、“this.reader 未定义”或“b 未定义”和“h 未定义”之类的错误。
The same kind of errors come when I change data format as javascript array like:
当我将数据格式更改为 javascript 数组时,会出现相同类型的错误,例如:
var dataexample = [[1, 'Sant Esteve de Palautordera'], [2, 'Parc Natural del Montseny']];
and call dataexample on store "data" property.
并在存储“数据”属性上调用 dataexample。
I'm absolutely lost...
我彻底迷路了...
回答by Krzysztof
Data in variable my_variable
is in JSON format, so JsonReader
should be used. To use that reader you can user simply JsonStore
. Example:
变量my_variable
中的数据是JSON格式,所以JsonReader
应该使用。要使用该阅读器,您只需使用JsonStore
. 例子:
var data = '[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]';
var mystore = new Ext.data.JsonStore({
//autoload: true,
fields: [
{type: 'integer', name: 'cod_domini'},
{type: 'string', name: 'nom_domini'}
]
});
mystore.loadData(Ext.decode(data)); // decode data, because it is in encoded in string
var cbxSelDomini = new Ext.form.ComboBox({
hiddenName: 'Domini',
name: 'nom_domini',
displayField: 'nom_domini',
valueField: 'cod_domini',
mode: 'local',
triggerAction: 'all',
listClass: 'comboalign',
typeAhead: true,
forceSelection: true,
selectOnFocus: true,
store: mystore
});
Working sample: http://jsfiddle.net/Ajnw7/
工作示例:http: //jsfiddle.net/Ajnw7/