-
30 Jan 2013 3:27 PM #1
Answered: Creating store from another store
Answered: Creating store from another store
Hey all,
I'm trying to take a store that has letter grades ( "A", "B",etc..) and create a new store that has percentages (95,85,etc). I think I almost have it working but something isnt quite right. Here is my function:
Note: Both stores have a 'grade' field and a 'firstaction' field. I just want to take the string grade field from the first store and make it an int field. The firstaction field should just copy over.Code:createStore:function(){ //console.log("in the create store function"); var store = Ext.data.StoreManager.lookup('Plot'); var storeArray = []; Ext.each(store.getRange(), function (item, idx, a) { //console.log(item.get('grade')); storeArray.push([{"grade":item.get('grade'),"firstaction":item.get('firstaction')}]); }); //console.log(storeArray); var newStore = Ext.create('Ext.data.Store',{ model: 'PL.model.NewPlot', fields: [{ name: 'grade', convert: function(value,record){ //console.log(value); //console.log(record); if (value == 'A'){ return 95; }if (value == 'B'){ return 85; }if (value == 'C'){ return 75; }if (value == 'D'){ return 65; }if (value == 'F'){ return 55; } } },{ name: 'firstaction', mapping: 'firstaction' }], data: storeArray }); return newStore; },
-
Best Answer Posted by alex9311
Thanks! Once I started looking more into my array I realized I could just convert the strings to ints when I construct that array and map it directly. Here is the working code:
Of course any comments on efficiency and coding style are appreciatedCode:createStore:function(){ var store = Ext.data.StoreManager.lookup('Plot'); var storeArray = []; Ext.each(store.getRange(), function (item, idx, a) { var oldgrade = item.get('grade'); var newgrade = ""; if (oldgrade == 'A'){ newgrade = 95; }if (oldgrade == 'B'){ newgrade = 85; }if (oldgrade == 'C'){ newgrade = 75; }if (oldgrade == 'D'){ newgrade = 65; }if (oldgrade == 'F'){ newgrade = 55; } storeArray.push({"grade":newgrade,"firstaction":item.get('firstaction')}); }); var newStore = Ext.create('Ext.data.Store',{ model: 'PL.model.NewPlot', fields: [{ name: 'grade', mapping: 'grade' },{ name: 'firstaction', mapping: 'firstaction' }], data: storeArray }); return newStore; },
-
30 Jan 2013 4:00 PM #2Sencha - Ext JS Dev Team
- Join Date
- Apr 2007
- Location
- Sydney, Australia
- Posts
- 15,242
- Vote Rating
- 106
- Answers
- 187
You're adding an extra array:
Code:storeArray.push([{"grade":item.get('grade'),"firstaction":item.get('firstaction')}]);Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
30 Jan 2013 4:11 PM #3
Thanks! Once I started looking more into my array I realized I could just convert the strings to ints when I construct that array and map it directly. Here is the working code:
Of course any comments on efficiency and coding style are appreciatedCode:createStore:function(){ var store = Ext.data.StoreManager.lookup('Plot'); var storeArray = []; Ext.each(store.getRange(), function (item, idx, a) { var oldgrade = item.get('grade'); var newgrade = ""; if (oldgrade == 'A'){ newgrade = 95; }if (oldgrade == 'B'){ newgrade = 85; }if (oldgrade == 'C'){ newgrade = 75; }if (oldgrade == 'D'){ newgrade = 65; }if (oldgrade == 'F'){ newgrade = 55; } storeArray.push({"grade":newgrade,"firstaction":item.get('firstaction')}); }); var newStore = Ext.create('Ext.data.Store',{ model: 'PL.model.NewPlot', fields: [{ name: 'grade', mapping: 'grade' },{ name: 'firstaction', mapping: 'firstaction' }], data: storeArray }); return newStore; },


Reply With Quote