How can I convert json into an extjs model? - json

I make the following json client side:
{
"employees": [
{
"firstName": "John",
"lastName": "Doe",
"computers": [
{
"name": "comp1"
},
{
"name": "comp2"
}
]
},
{
"firstName": "Anna",
"lastName": "Smith",
"computers": [
{
"name": "comp1"
},
{
"name": "comp2"
}
]
},
{
"firstName": "Peter",
"lastName": "Jones",
"computers": [
{
"name": "comp1"
},
{
"name": "comp2"
}
]
}
]
}
And I would like to stick it into this model:
Ext.define('Employees',{
extend: 'Ext.data.Model',
hasMany: [{
model: 'Computers',
name: 'computers',
}],
fields: [{name: 'firstname'}, {name: 'lastname'}]
});
Ext.define('Computers'{
extend: 'Ext.data.Model',
belongsTo: 'Employees',
fields: [{name: 'name'}]
});
How can I convert the json into my Employees extjs model?

You should have to define a data store like this:
Ext.create('Ext.data.Store', {
model: 'Employees',
storeId: 'myDataStore',
proxy: {
type: 'ajax',
url : 'path/to/your/json/file/Employees.json',
reader:{
type:'json',
root: 'employees'
}
}
});
and load it somewhere.

Related

ExtJS - Json to model parse parent but not childs correctly

I'm loading a json to my Model but it only recognize the parent model class but not the childs.
This is the json:
{
"Feedback": {
"id": 101,
"title": "asdsadsa",
"description": "sadasds",
"comments": [
{
"id": 43,
"feedbackFk": 101,
"comment": "Hi John , we are glad to receive feedback from you :) \\nOne of our technicians will reach back to you shortly.",
"commentDate": 1431488907881,
"owner": {
"id": 4,
"firstName": "Arnold",
"lastName": "Smoth",
"type": "3",
"email": "fzegarra#XXXX.com"
},
"responses": null,
"likes": null
}
],
"attachments": null,
"createdDate": 1431488906273,
"lastUpdateDate": 1431488906273,
"status": "OPEN",
"source": "ETMS",
"user": {
"id": 6,
"firstName": "John ",
"lastName": "Travolta",
"type": "1",
"email": "john.travolta#XXXXX.com"
}
}
}
This is parent code:
Ext.define('FBT.model.data.Feedback', {
extend: 'FBT.model.data.MappedModel',
fields: [ {
name: 'id',
type: 'auto'
}, {
name: 'title',
type: 'string'
}, {
name: 'description',
type: 'string'
}, {
name: 'createdDate',
type: 'date',
dateFormat: 'time'
}, {
name: 'lastUpdateDate',
type: 'date',
dateFormat: 'time'
}, {
name: 'status',
type: 'string'
}, {
name: 'source',
type: 'string'
} ],
belongsTo: [ {
name: 'reporter',
associationKey: 'user',
model: 'FBT.model.data.User'
} ],
hasMany: [ {
model: 'FBT.model.data.Comment',
name: 'comments',
associationKey: 'comments',
foreignKey: 'feedbackFk'
}, {
model: 'FBT.model.data.Attachment',
name: 'attachments',
associationKey: 'attachments'
} ],
getModelName: function() {
return 'Feedback';
}
});
This is one of the childs
Ext.define('FBT.model.data.Comment', {
extend: 'FBT.model.data.MappedModel',
fields: [ {
name: 'id',
type: 'auto'
}, {
name: 'feedbackFk',
type: 'int'
}, {
name: 'comment',
type: 'string'
}, {
name: 'commentDate',
type: 'date',
dateFormat: 'time'
} ],
belongsTo: [ 'Feedback', 'User' ],
hasMany: [ {
model: 'FBT.model.data.Comment',
name: 'responses',
associationKey: 'responses',
}, {
model: 'FBT.model.data.User',
name: 'likes'
} ]
});
but when I parse it, Comments are not recognized as a model

JSON source to datatables

I have this code:
testdata = [{
"hasresults": true,
"resultscount": 5,
"dob": null,
"chart": {
"rows": [
{
"chart": "BAR000",
"firstname": "RUSSELL",
"lastname": "BARON"
},
{
"chart": "BAR001",
"firstname": "BRUSELL",
"lastname": "BARON"
},
{
"chart": "BAR002",
"firstname": "GARY",
"lastname": "BARON"
}
]
}
}];
$('#test').dataTable({
"aaData": testdata,
"aoColumns": [{
"mDataProp": "chart"
}, {
"mDataProp": "firstname"
}, {
"mDataProp": "lastname"
}]
});
Can someone help me why is this not working? It seems that if I removed the following, it will work:
"hasresults": true,
"resultscount": 5,
"dob": null,
"chart": {
Not working fiddle
Working fiddle
You just need to address testdata the correct way. testdata is an array holding an object which have another object, chart, holding an array, rows.
$('#test').dataTable({
"aaData": testdata[0].chart.rows, //<------
"aoColumns": [{
"mDataProp": "chart"
}, {
"mDataProp": "firstname"
}, {
"mDataProp": "lastname"
}]
});
Your code working here -> http://jsfiddle.net/j1fvL96e/

convert nested json to csv using node.js

I want to convert a nested json to csv file using node.js.
my JSON structure:
[
{
"Make": "Nissan",
"Model": "Murano",
"Year": "2013",
"Specifications": {
"Mileage": "7106",
"Trim": "SAWD"
},
"Items": [
{
"flavor": {
"name": "Cherry",
"id": 1
},
"packSize": {
"name": "200ML",
"id": 1
}
},
{
"flavor": {
"name": "Vanilla",
"id": 2
},
"packSize": {
"name": "300ML",
"id": 2
}
}
]
},
{
"Make": "BMW",
"Model": "X5",
"Year": "2014",
"Specifications": {
"Mileage": "3287",
"Trim": "M"
},
"Items": [
{
"flavor": {
"name": "Cherry",
"id": 1
},
"packSize": {
"name": "200ML",
"id": 1
}
},
{
"flavor": {
"name": "Vanilla",
"id": 2
},
"packSize": {
"name": "300ML",
"id": 2
}
}
]
}
]
I have used 'json-2-csv' module but it only converts the simple structure not the nested structure.
only the 'make','model','year' and 'specification' is converted,'items' are not converted
How to do this???
You can use the module jsonexport its pretty easy, check this sample:
Here is the output using the json you provided and jsonexport:
Sample:
var jsonexport = require('jsonexport');
var contacts = [{
name: 'Bob',
lastname: 'Smith',
family: {
name: 'Peter',
type: 'Father'
}
},{
name: 'James',
lastname: 'David',
family:{
name: 'Julie',
type: 'Mother'
}
},{
name: 'Robert',
lastname: 'Miller',
family: null,
location: [1231,3214,4214]
},{
name: 'David',
lastname: 'Martin',
nickname: 'dmartin'
}];
jsonexport(contacts,function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
The ouput:
lastname;name;family.type;family.name;nickname;location
Smith;Bob;Father;Peter;;
David;James;Mother;Julie;;
Miller;Robert;;;;1231,3214,4214
Martin;David;;;dmartin;
Source: https://www.npmjs.com/package/jsonexport
Do you always have the same numbers of columns ?
ie : Do you have a fixed (or max number) of items ?
Make;Model;Year;Mileage;Trim;Item_1_flavor_name;Item_1_packSize_name;Item_2_flavor_name;Item_2_packSize_name

Using a proxy in a store is not working?

hi i create a store connect to contacts.json is not working ,here is my store
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
requires: ['Ext.data.proxy.Ajax'],
alias: 'store.List',
config: {
model: 'senchatest.model.Contact',
proxy: {
type: 'ajax',
url: 'contacts.json',
reader: {
type: 'json'
}
}
}
});
and this is my modal
Ext.define('senchatest.model.Contact', {
extend: 'Ext.data.Model',
config: {
fields: ['firstName', 'lastName']
}
});
and it is my json file
[
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
what is wrong its not display a data
instead of proxy directly use data it work
eg:
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
requires: ['Ext.data.proxy.Ajax'],
alias: 'store.List',
config: {
model: 'senchatest.model.Contact',
data :[
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
}
});
This code is working but proxy code is not working what is a issue
Take a look at http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.reader.Reader, it has a perfect example how you can do what you desire
Store
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
alias: 'store.List',
// use the model #Oğuz Çelikdemir suggested
model: 'senchatest.model.Contact',
proxy: {
type: 'ajax',
url : 'contacts.json',
reader: {
type: 'json',
root: 'contacts'
}
},
});
contacts.json
{
"success": true,
"contacts": [
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
}
Modify the model as below ( config property isn't necessary ) :
Ext.define('senchatest.model.Contact', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'}
]
});
EDIT :
I create a Fiddle on Sencha, check it out.
JSON Sample
you may use
autoLoad: true,
proxy: {
type: 'ajax',
url : 'contacts.json'
}

extJs - populate grid with array of arrays

I have an array of persons, each person has an array of contacts. The data is in json format:
[
{
"id": 7900,
"position": "Tillitsvalgt",
"person": {
"id": 1000001,
"lastName": "Andrushko",
"contacts": [
{
"id": 1000001,
"personId": 1000001,
"type": {
"id": 5,
"name": "Mobile phone"
},
"value": "25417"
},
{
"id": 1000002,
"personId": 1000001,
"type": {
"id": 35,
"name": "Mobile phone"
},
"value": "945417"
}
]
}
},
{
"id": 7900,
"position": "Tillitsvalgt",
"person": {
"id": 1000001,
"lastName": "Andrushko",
"contacts": [
{
"id": 1000001,
"personId": 1000001,
"type": {
"id": 5,
"name": "Mobile phone"
},
"value": "25417"
},
{
"id": 1000002,
"personId": 1000001,
"type": {
"id": 35,
"name": "Mobile phone"
},
"value": "945417"
}
]
}
}
]
I want to display two grids, first one with persons and the second one with contacts for selected person. I have created two models:
Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [
{
mapping: 'person.lastName',
name: 'lastName'
}
],
hasMany: [
{
associationKey: 'person.contacts',
model: 'Contact',
name: 'contacts'
}
],
proxy: {
type: 'ajax',
url: 'MemberServlet?operation=get',
reader: {
type: 'json'
}
}
});
and a model for contacts:
Ext.define('Contact', {
extend: 'Ext.data.Model',
fields: [
{
mapping: 'type.name',
name: 'type'
},
{
name: 'value'
}
],
belongsTo: {
model: 'Person'
}
});
In such configuration my second grid displays contacts for all persons, I want it to display only for selected person. How can I do it?