salamey
salamey

Reputation: 3811

extjs 4 : get store record values

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

Upvotes: 2

Views: 18592

Answers (3)

Aashray
Aashray

Reputation: 2753

What i don't get is the way you have used myStore. You have used it both ways as a string as well as a variable. Which is it? If its a string, then myStore.load() will not work. If its a variable, you need to lose the quotes on Ext.StoreMgr.lookup(myStore). Where is the store variable being declared?

Upvotes: 1

Dev
Dev

Reputation: 3932

If your using ajax request you can get id_metric ...just have a look at the success function.

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.      

You can load the data here now

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

And you are using the mystore to load...is your Mystore's storeId:mystore, just have a look at at it once.

Upvotes: 0

Mike
Mike

Reputation: 1013

It could very well be that you are confusing the store by having 2 objects with the same id. (Your JSON code shows "id_metric":"1" twice).

Most likely however, the automatic conversion to the model fails. Try doing:

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

to see what the model looks like and if it is able to create an instance.

If that works (so you are able to create a model), drill down in your store to see if the data is actually in there. Look for this:

myStore.proxy.reader.jsonData

Also, as was pointed out in the comments, the idProperty is a model property, you should declare it in your model.

Upvotes: 0

Related Questions