PDA

View Full Version : Json file containing arrays loading into store



debchatt1988@gmail.com
24 Apr 2014, 6:19 AM
I need to create a json store from the following json file using Ext JS 3.4. I need the data of the fields someheaders1, someheaders2, somedata1, somedata2 all these four and their data inside a single store. Till now what I have observed is that I can load any one of the following field (e.g.-either someheaders1 or someheaders2) in a single store .Json file :{ "value": { "datavalue": { "datavalueproperties": [ { "adress": "iiii","Name": "oooo"}],"someheaders1": [ { "value0": "abc","value1": "xyz"}],"someheaders2": [ { "value0": "ttt","value1": "oooo"}],"somedata1": [{ "value0": "vv" ,"value1": "yy" ,"value2": "ii"},{ "value0": "uu" ,"value1": "rr" ,"value2": "ww"}],"somedata2": [{ "value0": "oo" ,"value1": "uu" ,"value2": "yy"},{ "value0": "rr" ,"value1": "ee" ,"value2": "ooo"}]}}}

Kachopsticks
29 Apr 2014, 11:55 AM
I am going to take a shot at this, not sure if this is what you are going for. Assuming that one record is in the json format that your provided. You could try modeling a store like this (I would create a whole new file, newStore.js), and then load it with your data object...



Ext.ns('GLOBAL');
GLOBAL.newStore = Ext.extend(Ext.data.JsonStore, {
constructor: function(cfg) {
cfg = cfg || {};
GLOBAL.newStore.superclass.constructor.call(this, Ext.apply({
root: 'value.datavalue',
fields: [
{
name: 'someheaders1',
type: 'string',
mapping: 'someheaders1[0].value0'
},
{
name: 'someheaders1',
type: 'string',
mapping: 'someheaders1[0].value1'
},
{
name: 'someheaders2',
type: 'string',
mapping: 'someheaders2[0].value0'
},
{
name: 'someheaders2',
type: 'string',
mapping: 'someheaders2[0].value1'
},
{
name: 'somedata1',
type: 'string',
mapping: 'somedata1[0].value0'
},
{
name: 'somedata1',
type: 'string',
mapping: 'somedata1[0].value1'
},
{
name: 'somedata1',
type: 'string',
mapping: 'somedata1[0].value2'
},
{
name: 'somedata1',
type: 'string',
mapping: 'somedata1[1].value0'
},
{
name: 'somedata1',
type: 'string',
mapping: 'somedata1[1].value1'
},
{
name: 'somedata1',
type: 'string',
mapping: 'somedata1[1].value2'
},
{
name: 'somedata2',
type: 'string',
mapping: 'somedata2[0].value0'
},
{
name: 'somedata2',
type: 'string',
mapping: 'somedata2[0].value1'
},
{
name: 'somedata2',
type: 'string',
mapping: 'somedata2[0].value2'
},
{
name: 'somedata2',
type: 'string',
mapping: 'somedata2[1].value0'
},
{
name: 'somedata2',
type: 'string',
mapping: 'somedata2[1].value1'
},
{
name: 'somedata2',
type: 'string',
mapping: 'somedata2[1].value2'
}



]
}, cfg));
}
});
Ext.reg('newStore', GLOBAL.newStore);


Then just load your store...



loadData: function(data){
this.store = new GLOBAL.newStore();
if(this.rendered){
this.store.loadData(data);
},


Some things to note... Your arrays' will always need to be in the same format. If your arrays' are larger than specified in the store your data will not be found. I have also never mapped out an array in a store before, so I am not sure if it can be done (mapping: 'someheaders2[0].value0'). It also might be good to remove the top level of your object, and then just specify your root as datavalue. I am not sure if 'value.datavalue' will be acceptable as a root value. In the end you may just want to restructure your data object. It is a little odd becasue it contains nested arrays which to me look like multiple types of records. If you have multiple types of records I would think you would want multiple stores. To my understanding a store can contain multiple records of the same type, but it is not meant to have multiple types of records. I hope some of this helps. Good luck!