ExtJs4: loading json data into a grid - javascript

i've seen there are many question about this argument but anyone has an answer that fits for me. So let's dig into the code:
Ext.define('Platform Member', {
extend: 'Ext.data.Model',
fields: [
{name: 'id'},
{name: 'name', type: 'string', convert: null, defaultValue: undefined},
{name: 'email', type: 'string', convert: null, defaultValue: undefined},
],
idProperty: 'id'
});
This is the grid, it is recognized and the grids are in the webpage.
var store = Ext.create('Ext.data.JsonStore', {
autoLoad: true,
model: "Platform Member",
proxy: {
type: 'ajax',
url: '../static/platform-member.json',
reader: {
type: 'json',
root: 'response/platform_members'
}
}
});
Question, is the root attribute the node of the json three which should be read? Cause the names of the grid's columns are the names of the fields of this node that should be read in the json file. The json file is below but i suggest you to read with this viewer.
The json.
Agnese

To begin with, the root is incorrect. It should be response.platform_members.
Also, the model name is a class name, so it should be PlatformMember.

Related

ExtJS grid not showing store data

My ExtJS Grid is not showing my store data. Using ExtJS 5
This is my grid (it's within a hbox):
{
xtype: 'grid',
title: 'Company Manager',
id: 'companyGrid',
flex: 1,
plugins: 'rowediting',
columnLines: true,
store: Ext.data.StoreManager.lookup('companyStore'),
columns: {
items: [{
header: 'ID',
dataIndex: 'id',
},
{
header: 'Name',
dataIndex: 'name',
},
{
header: 'Token',
dataIndex: 'access_token'
},
]
}
}
This is my store (I use the Google Extension of Sencha and it's filled with my data so this works + the ending }); were ignored by the coding block):
var companyStore = Ext.create('Ext.data.Store', {
storeId: 'companyStore',
proxy: {
type: 'ajax',
url: 'resources/data/company.json',
reader: {
type: 'json',
rootProperty: 'data'
}
},
fields: [{
name: 'id',
type: 'int'
}, {
name: 'name',
type: 'string'
}, {
name: 'access_token',
type: 'string'
}],
autoLoad: true
});
Does anyone know were I went wrong here?
I have tried: Reloading the store, checking if the store is actually filled, refreshing the grid view.
Nothing I tried worked and I decided to ask you guys for advice :)
#Evan Trimboli
You made me think and I fiddled arround for a second and found the following solution.
Instead of using the
store : Ext.data.StoreManager.lookup('companyStore'),
I used
bind : '{companyStore}',
And moved the define store towards the CompanyModel.js file :) now it works properly!!!!
Thanks :D

Laoding JSON data into Ext.data.Tree store on click on a button

I have created a tree panel in ExtJs 4.1 and that panel is associated with store. I have some JSON data available in a variable. How can I load that into into tree store on click of a button.
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'phone', type: 'string', mapping: 'phoneNumber'}
]
});
var data = {
users: [
{
id: 1,
name: 'Ed Spencer',
phoneNumber: '555 1234'
},
{
id: 2,
name: 'Abe Elias',
phoneNumber: '666 1234'
}
]
};
//note how we set the 'root' in the reader to match the data structure above
var store = Ext.create('Ext.data.TreeStore', {
autoLoad: false,
model: 'User',
proxy: {
type: 'memory',
}
});
How can I load data on click of a button?
var store = Ext.create('Ext.data.TreeStore', {
model: yourModel, // make sure the model fits
proxy: {
data : yourData, // this is your var with json
type: 'memory',
reader: {
type: 'json'
}
},
});
Check out the answers over here: Load static data to Ext.data.TreeStore
To run your code on a button click, do:
Ext.create('Ext.Button', {
text: 'Click me',
handler: function() {
// load the store
}
});
Try store.data = data, It appears that there is no methods like 'setData', just try store.data = data maybe it will work
you can use store.loadData method on buttons click. Check doc here.

Correct JSON and Model with TreeStore and TreePanel

I have a JSON String comes from the server side:
{"success":"true","total":"6","list":
[{"id":"1","name":"folder1","parentid":"null","type":"0"},
{"id":"2","name":"r1","parentid":"1","type":"1"},
{"id":"3","name":"folder2","parentid":"1","type":"0"},
{"id":"4","name":"r2","parentid":"3","type":"1"},
{"id":"5","name":"r3","parentid":"null","type":"1"},
{"id":"6","name":"folder3","parentid":"null","type":"0"}]}
How do I turn that into a tree? Can anyone suggest me how to get the elements in the list (id, name, parentid, type)?
I used the following structure:
Model
Ext.define('MyApp.model.MyTreeModel', {
extend: 'Ext.data.Model',
fields: [
'someStringIdentifier',
'children',
'dataForThisNode',
],
});
Store
Ext.define('MyApp.store.MyTreeStore', {
extend: 'Ext.data.TreeStore',
model: 'MyApp.model.MyTreeModel',
storeId: 'cubeDynamicStoreId',
autoLoad: false,
proxy: {
type: 'ajax',
api: {
read : '/myapp/rest/myquery',
},
reader: {
type: 'json',
root: 'children',
successProperty: 'success',
idProperty: 'id',
},
},
// bug in extjs4.1 autoLoad is ignored
// specifying "loaded: true" resolves the problem
root: {
expanded: true,
loaded: true,
},
});
Sample JSON ( use http://jsonviewer.stack.hu/ to visualize)
Use the leaf property to stop expansion at any node
{"children":{"leaf":false,"someStringIdentifier":"Total","dataForThisNode":{},"children":[{"leaf":true,"someStringIdentifier":"data1","dataForThisNode":{},"children":[{"leaf":false,"someStringIdentifier":"2012","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2013","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2014","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2015","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2016","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2017","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2018","dataForThisNode":{},"children":null}]},{"leaf":true,"someStringIdentifier":"data2","dataForThisNode":{},"children":[{"leaf":false,"someStringIdentifier":"2012","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2013","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2014","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2015","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2016","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2017","dataForThisNode":{},"children":null},{"leaf":false,"someStringIdentifier":"2018","dataForThisNode":{},"children":null}]}]},"success":true}
Let's start with the store definition:
Ext.define('app.store.Tasks', {
extend: 'Ext.data.TreeStore',
model: 'app.model.Task',
autoSync: true,
autoLoad: true,
root: {
text: 'Root',
id: 'NULL',
expanded: true
},
});
The important thing to note is that we are extending TreeStore here. Thus, our model records will be wrapped with Ext.data.NodeInterface, which included many tree-related fields (like, for example, parentNode).
Then to the model definition:
Ext.define('app.model.Task', {
extend: 'Ext.data.Model',
fields: [
{name: 'name' , type: 'string'},
{name: 'iconCls' , type: 'string', defaultValue: 'treenode-no-icon'},
{name: 'expanded' , type: 'boolean', defaultValue: true, persist: false},
{name: 'index' , type: 'int'},
],
proxy: {
type: 'direct',
api: {
create: Tasks.Create,
read: Tasks.Read,
update: Tasks.Update,
destroy: Tasks.Destroy,
},
},
});
The only 'real' field we've defined is name, All the others are part of NodeInterface: iconCls has a default value of no icon; expanded is set with persist:false so collapsing/expanding a node won't result in an update server call; index is included so if the user re-order nodes (using drag and drop) server calls will be made.
Notice that there's no id field since a a model's default idProperty is id, ExtJS is smart enough to figure that if your JSON has id field in it - it represents the unique id of the record.
Also note there's no parentId field - by providing correct JSON (with nodes having children property), NodeInterface work out the correct parentNode of each node.
Then the JSON:
{
"success":true,
"children":[
{
"id":"1",
"name":"Home",
"children":[
{
"id":"6",
"name":"Emails",
"leaf":true
},
{
"id":"7",
"name":"Bath",
"leaf":true
}
],
"leaf":false
},
]
}
That's pretty much it!
The first answer on this subject from Izhaki is great, but it seems misleading and will not work as is if your expectation is to see the description for nodes. I spent several hours struggling with this. The only way to see this description is to rename "name" with "text". see below.
Ext.define('app.model.Task', {
extend: 'Ext.data.Model',
fields: [
{name: 'text' , type: 'string'},
{name: 'iconCls' , type: 'string', defaultValue: 'treenode-no-icon'},
{name: 'expanded' , type: 'boolean', defaultValue: true, persist: false},
{name: 'index' , type: 'int'},
],

ExtJS 4 Beta 3 Model Won't Load Data From JSON File

The model:
Ext.ns('Workout.Models.user');
Ext.regModel('User', {
fields: [{
name: 'member_id',
type: 'int'
}, {
name: 'first_name',
type: 'string'
}, {
name: 'last_name',
type: 'string'
}, {
name: 'username',
type: 'string'
}, {
name: 'password',
type: 'string'
}, {
name: 'dob',
type: 'date',
dateFormat: 'Y-m-d'
}, {
name: 'email_address',
type: 'string'
}, {
name: 'is_active',
type: 'int'
}],
proxy: {
type: 'ajax',
format: 'json',
url: '../../_dev/json_fixtures/users.json',
reader: {
type: 'json',
root: 'users'
},
root: 'users'
}
});
The Store:
Ext.ns('Workout.Stores');
Workout.Stores.user = new Ext.data.Store({
model: 'User',
storeId : 'Workout.Stores.user',
sorters: [
'last_name',
'first_name',
'member_id'
],
autoLoad: true
});
The Grid:
Ext.ns('Workout.User');
Workout.User.grid = new Ext.grid.Panel({
store: 'Workout.Stores.user',
columns:[{
text: 'Created At',
dataIndex: 'created_at'
}, {
text: 'First Name',
dataIndex: 'first_name'
}]
});
The JSON File
{
"users":[{
"created_at":"2011-04-01 14:13:34",
"member_id":"14453",
"first_name":"Jemima",
"last_name":"Petersen",
"username":"jpeterson",
"password":"TDW29HOH7WY",
"dob":"1960-07-03",
"email_address":"at.velit.Pellentesque#sociis.com"
}]
}
Wheh I load my HTML page, the grid is empty. However, if I supply raw data to the store via the data param, it loads. If I call User.load() manually via the console, nothing happens. If i call User.load() and pass in a valid JSON object,nothing happens.
Is there something I'm missing / not doing right?
You have done everything expect set the height of your grid panel. You need to set the height to display the records. Here is what I would add to your grid panel config:
height: 300
Now, apart from this, you have other problems like you have not defined created_at in your User model. If you plan to display the value in your grid, you need to update your model as well.
Ext.define(
'BK.store.Categories'
, { extend : 'Ext.data.Store'
, model : 'BK.model.Category'
, autoload : true
, proxy : { type : 'ajax'
, format : 'json'
, root : 'results'
, api : { read : 'data/data1.json' }
, reader : new Ext.data.JsonReader({ type : 'json'
, root : 'results'
, successProperty : 'success'
})
}
}
);
when I use data hardcoded in the store, it works OK (so model, view, controller are OK), as soon as I use the proxy it behaves as if autoload were FALSE, no net request

Setting a value from the server in a ComboBox in ExtJS

I am using a DataStore with a JsonReader to populate the ComboBox, but the proper value is not being marked as selected.
ComboBox:
{
fieldLabel: 'Business Unit',
xtype:'combo',
width:167,
name: 'business_Unit',
hiddenName: 'businessUnit',
store: businessUnitStore,
displayField: 'buName',
valueField: 'buId',
mode: 'remote',
triggerAction: 'all',
typeAhead: false,
editable: false
}
and I use a JsonReader in my form.
var leadReader = new Ext.data.JsonReader({
root: 'data',
totalProperty: 'total',
id: 'leadId'
}, [
{name:'title', type: 'string'},
{name:'firstName', type: 'string'},
{name:'lastName', type: 'string'},
{name:'designation', type: 'string'},
{name:'business_Unit', type: 'string', mapping: 'businessUnit.buName'},
]);
This is the JSON response:
{"data":{"leadId":22,"firstName":"fname","lastName":"lname","designation":"President","businessUnit":{"buId":4,"buName":"US","buDescription":""}},"success":true}
I want the BusinessUnit = US selected in the combobox and also have all the other options available for selection in the combo when I load the form.
editForm.getForm().load({url:fetchUrl, method: 'GET'});
Everything works fine, except that the BusinessUnit=US is not selected in the combo.
The name field on the combo doesn't match the field in the json response (business_Unit vs businessUnit) but also I don't think BasicForm.load will work with nested objects. After the load call you may have to manually load it.
editForm.getForm().load({url: fetchUrl, method: 'GET', success: function(form, action) {
var combo = form.findField('business_Unit');
combo.setValue(action.result.data.businessUnit.buiId);
}
});

Categories

Resources