2

my json data looks like this :

({"success": "true", "message" : "OK","data":[{"id_metric":"1","name_filter":"doc filter","type_guicomp":"combobox"},{"id_metric":"1","name_filter":"severity","type_guicomp":"combobox"}]})

I'd like to be able to retrieve the "type_guicomp" field value for the "id_metric" = 1 , so the result would be : "type_guicomp":"combobox" and "type_guicomp":"combobox".

I do this because I need the value "combobox" to assign it to a variable. I've tried several things including :

myStore.load({
        scope: this,
        callback : function(record, operation, success) {
            console.log(record);
            console.log(record.data);
            }

And :

var index = Ext.StoreMgr.lookup("myStore").findExact('id_metric',1);
var rec = Ext.StoreMgr.lookup("myStore").getAt(index);
console.log(rec);

These solutions return undefined or null. So when I did this in the callback :

var i = myStore.getCount();
console.log(i);

It returned 0. But when I check the json output, it's not empty and there's actually data in it.

What am I doing wrong? Please any help would be appreciated.

EDIT

My store looks like this :

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'
    }
}
});

My Model :

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'}]  
});
4

3 回答 3

1

我不明白的是你使用的方式myStore。您已经将它用作字符串和变量两种方式。它是哪一个?如果它是一个字符串,那么myStore.load()将不起作用。如果它是一个变量,您需要丢失Ext.StoreMgr.lookup(myStore). store 变量在哪里声明?

于 2013-02-28T09:45:04.453 回答
0

如果您使用 ajax 请求,您可以获得id_metric ...看看成功函数。

Ext.Ajax.request({
    url : 'blabla.tml',
    method:POST,                                                                      
    params : {
        item : Data
    },
    success : function(response) {
        var res = response.responseText;                                                                        
        alert(Ext.decode(res));
        var jsonData = Ext.decode(res);
        var data = jsonData.data; // here data will contain ur data with root data.      

您现在可以在此处加载数据

    Ext.getStore('Mystore').loadData(data);                                             
    }
});

而您正在使用 mystore 加载...是您的 Mystore 的 storeId:mystore,只需查看一次即可。

于 2013-03-01T11:51:26.520 回答
0

很可能是因为您有 2 个具有相同 id 的对象而混淆了商店。(您的 JSON 代码显示 "id_metric":"1" 两次)。

然而,最有可能的是,自动转换为模型失败。尝试做:

var model = Ext.create("Metrics.model.MyModel", {"id_metric":"1","name_filter":"doc filter","type_guicomp":"combobox"});

查看模型的外观以及它是否能够创建实例。

如果可行(这样您就可以创建模型),请深入您的商店以查看数据是否确实存在。寻找这个:

myStore.proxy.reader.jsonData

此外,正如评论中所指出的, idProperty 是一个模型属性,您应该在模型中声明它。

于 2013-07-08T08:40:34.030 回答