【发布时间】:2013-02-13 14:39:21
【问题描述】:
我的 json 数据如下所示:
({"success": "true", "message" : "OK","data":[{"id_metric":"1","name_filter":"doc filter","type_guicomp":"combobox"},{"id_metric":"1","name_filter":"severity","type_guicomp":"combobox"}]})
我希望能够检索 "id_metric" = 1 的 "type_guicomp" 字段值,因此结果将是:"type_guicomp":"combobox" 和 "type_guicomp":"combobox"。
我这样做是因为我需要将值 "combobox" 分配给一个变量。
我尝试了几件事,包括:
myStore.load({
scope: this,
callback : function(record, operation, success) {
console.log(record);
console.log(record.data);
}
还有:
var index = Ext.StoreMgr.lookup("myStore").findExact('id_metric',1);
var rec = Ext.StoreMgr.lookup("myStore").getAt(index);
console.log(rec);
这些解决方案返回 undefined 或 null。 所以当我在回调中这样做时:
var i = myStore.getCount();
console.log(i);
它返回了0。但是当我检查 json 输出时,它不是空的,里面实际上有数据。
我做错了什么?请提供任何帮助。
编辑
我的店铺是这样的:
Ext.define('Metrics.store.MyStore', {
extend: 'Ext.data.Store',
model: 'Metrics.model.MyModel',
autoLoad: true,
idProperty: 'id_metric',
proxy : {
type : 'ajax',
actionMethods : 'POST',
node : 'id_metric',
api : {
read : 'gui_comp_items.php' //the php script that gets data from db
},
reader: {
type: 'json',
successProperty: 'success',
messageProperty: 'message',
root: 'data'
}
}
});
我的模特:
Ext.define('Metrics.model.MyModel', {
extend: 'Ext.data.Model',
fields: [
{name : 'id_metric', type : 'int'},
{name : 'name_filter', type : 'string'},
{name : 'type_guicomp', type : 'string'},
{name : 'value', type : 'string'}]
});
【问题讨论】:
-
您的商店和模型配置是什么样的?
-
我已将它们添加到我的帖子中。
-
据我所知,idProperty 不是商店的属性,而是模型的属性,因此请考虑将其移至那里,这样您将返回两个具有相同值的 id_metric,因此它似乎不是唯一的。此外,您可以缩短代码
Ext.getStore('MyStore').findRecord('id_metric', 1)。如果没有任何错误,例如解码问题,请使用 firebug 等仔细检查响应。现在尝试在 load 的回调函数中转储数据,因为您可能会在它们被加载之前检索它们。