PDA

View Full Version : How to retrive Ext.data.Store data in sencha



mukund1a
4 Nov 2011, 11:13 PM
var options_model=Ext.regModel('options_model', {
fields: ['option1', 'status1','option2', 'status2','option3', 'status3','option4', 'status4']
});
var options = new Ext.data.Store({
model: 'options_model',
data: [
{ id: 1, option1: 'Alope', status1: 'true',option2: 'Option2', status2: 'false',option3: 'Option3', status: 'false',option4: 'Option4', status4: 'false' }
]
});
i have the above code now I want to get the various options values ..
console.log(options.getById('option1'));
console.log(options.getAt(2));
undefine...............
how to proceed help

mitchellsimoens
5 Nov 2011, 5:56 AM
getById('option1') won't work because you are not looking for the correct id. getById(1) will get the record that id field is equal to 1.

getAt(2) won't work because you only have one record loaded. getAt(0) will return the first (and only) record for you.

AndreaCammarata
7 Nov 2011, 2:02 AM
var options_model=Ext.regModel('options_model', {
fields: ['option1', 'status1','option2', 'status2','option3', 'status3','option4', 'status4']
});
var options = new Ext.data.Store({
model: 'options_model',
data: [
{ id: 1, option1: 'Alope', status1: 'true',option2: 'Option2', status2: 'false',option3: 'Option3', status: 'false',option4: 'Option4', status4: 'false' }
]
});
i have the above code now I want to get the various options values ..
console.log(options.getById('option1'));
console.log(options.getAt(2));
undefine...............
how to proceed help

If you want to get a record by its id you first have to set the "idProperty" on your model, that, looking on how you are trying to get your record, should be "option1" even if would be more correct to set it on the id property that, you have defined in your data, but not in your model.

However, to set the idProperty to the field "option1", you only need to edit your model like follows:



var options_model=Ext.regModel('options_model', {
idProperty: 'option1',
fields: ['option1', 'status1','option2', 'status2','option3', 'status3','option4', 'status4']
});


In this way calling



console.log(options.getById('option1'));


You will be able to retrieve your first an only record.

Instead, console.log(options.getAt(2)), exactly like Mitchell told you, doesn't returns you anything, because it suppose that you have 3 records loaded in your store but you actually have only 1 record.

Hope this helps.

mitchellsimoens
7 Nov 2011, 6:20 AM
fyi... idProperty defaults to 'id' and you don't actually need to add it to your fields.

AndreaCammarata
7 Nov 2011, 6:31 AM
fyi... idProperty defaults to 'id' and you don't actually need to add it to your fields.

True if he have to get the value by the id property, but I just wanted clear that he can set a different id field if he need it.

However, reading again my example I've noticed I made confusion, because your fields name and data values are almost the same, so to get the first record, by setting the "idProperty" on your model to "option1" you should write



options.getById('Alope');


Hope this helps.