-
4 Nov 2011 11:13 PM #1
How to retrive Ext.data.Store data in sencha
How to retrive Ext.data.Store data in sencha
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
-
5 Nov 2011 5:56 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
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.Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
7 Nov 2011 2:02 AM #3
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:
In this way callingCode:var options_model=Ext.regModel('options_model', { idProperty: 'option1', fields: ['option1', 'status1','option2', 'status2','option3', 'status3','option4', 'status4'] });
You will be able to retrieve your first an only record.Code:console.log(options.getById('option1'));
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.Sencha Inc
Andrea Cammarata, Solutions Engineer
CEO at SIMACS
@AndreaCammarata
www.andreacammarata.com
github: https://github.com/AndreaCammarata
-
7 Nov 2011 6:20 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
fyi... idProperty defaults to 'id' and you don't actually need to add it to your fields.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
7 Nov 2011 6:31 AM #5
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
Hope this helps.Code:options.getById('Alope');Sencha Inc
Andrea Cammarata, Solutions Engineer
CEO at SIMACS
@AndreaCammarata
www.andreacammarata.com
github: https://github.com/AndreaCammarata


Reply With Quote