Populate Combo box with JSON file, ExtJs 4.x.x - json

Ext.define('Form', {
extend: 'Ext.data.Model',
fields: [
{name:'type', type:'String'}
]
});
var store = Ext.create('Ext.data.ArrayStore', {
model: 'Form',
autoLoad: true,
proxy: {
type: 'ajax',
url: '/test.json',
reader: {
type : 'json',
root: 'types'
}
}
});
Ext.define('DForms', {
extend: 'Ext.panel.Panel',
bodyPadding: 12,
layout : 'auto',
autoScroll : true,
items: [{
xtype:'selectcombo',
queryMode:'local',
emptyText: 'Select Condition',
store:store,
displayField: 'type',
valueField: 'type',
width : 200,
typeAhead : true
}],
});
When this loads, the selectcombo is empty, nothing gets loaded, i have searched through many sites, and can't find anything to help. Any suggestions would be great

The xtype you look for is combo, the store type is JsonStore because the ArrayStore will not interpret the json from the types root as you might expect. I can not simulate the ajax request here though.
Ext.onReady(function() {
Ext.define('Form', {
extend: 'Ext.data.Model',
fields: [{
name: 'type',
type: 'String'
}]
});
var store = Ext.create('Ext.data.Store', {
model: 'Form',
data: [{
type: 'Aaaaaaa'
}, {
type: 'Bbbbbb'
}, {
type: 'Ccccccccccc'
}, {
type: 'Ddddddd'
}]
});
Ext.define('DForms', {
extend: 'Ext.panel.Panel',
bodyPadding: 12,
layout: 'auto',
autoScroll: true,
items: [{
xtype: 'combo',
queryMode: 'local',
emptyText: 'Select Condition',
store: store,
displayField: 'type',
valueField: 'type',
width: 200,
typeAhead: true
}],
});
Ext.create('DForms', {
renderTo: Ext.getBody()
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all-debug.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/resources/css/ext-all.css" />

I think type in fields is case-sensitive. So try
fields :[{name : 'type', type : 'string'}]
If still not working, as Tejas1991 pointed out check the content of your json.

Related

json data is not loading in Ext.tree.panel

I have defined Ext.tree.Panel and on change of combo value, I am calling
tree.getStore().load();
Here is my treepanel code
Ext.define('InboxTreePanel', {
extend: 'Ext.tree.Panel',
xtype: 'inboxtreepanel',
height: 250,
initComponent: function() {
var me = this;
Ext.applyIf(me, {
store: Ext.create('Ext.data.TreeStore', {
fields: [{
name: 'id',
type: 'string'
}, {
name: 'text',
type: 'string'
}],
autoLoad: false,
proxy: {
type: 'jsonajax',
url: app_global.serviceExtn + 'browseS3.do',
reader: {
type: 'json',
root: 'results'
}
},
root: {
loaded: false
}
}),
columns: [{
xtype: 'treecolumn',
dataIndex: 'text',
flex: 1,
text: 'Nodes',
editor: 'textfield'
}]
});
me.callParent(arguments);
}
});
In return I am getting json data but data is not loading in treepanel
{"success":true,
"code":null,
"message":null,
"data":[{"id":"root/Date-2015-08-30","text":"Date-2015-08-30","leaf":false},
{"id":"root/Date-2015-08-31","text":"Date-2015-08-31","leaf":false},
{"id":"root/Date-2015-09-08","text":"Date-2015-09-08","leaf":false},
{"id":"root/Date-2015-09-09","text":"Date-2015-09-09","leaf":false}],
"totalCount":0}
Can anyone help why data is not loading in treepanel? What is wrong in my code

Can't get 'hasMany' data from nest JSON

I have a nested JSON and I cannot seem to extract the nested array. I am not sure if I set up my store and models correctly. It seems right. As you can see at the bottom of this post, when I try to get the nested part out, it doesn't work.
JSON data:
{
"data":{
"name":"John",
"age":"23",
"gender":"Male",
"purchase":[
{
"item":"shirt",
"qty":1,
"price":10
},
{
"item":"hat",
"qty":2,
"price":25
},
{
"item":"pants",
"qty":1,
"price":15
}
]
},
"success":true,
"errorMsg":""
}
Store:
Ext.define('Pvm.store.MyStore', {
extend: 'Pvm.store.PvmBaseStore',
requires: [
'Pvm.model.Customer'
],
model: 'Pvm.model.Customer',
autoLoad: false
});
Customer Model:
Ext.define('Pvm.model.Customer', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json'
],
fields: [{
name: 'name',
type: 'auto'
}, {
name: 'age',
type: 'auto'
}, {
name: 'gender',
type: 'auto'
}],
associations: {
type: 'hasMany',
model: 'Pvm.model.Purchase',
name: 'purchase',
associationKey: 'purchase'
},
proxy: {
type: 'ajax',
actionMethods: {
create: 'POST',
read: 'POST', // changed read's method to POST (from GET) so we get the parameters as form data (not in URL, for security reasons)
update: 'POST',
destroy: 'POST'
},
url: '/pvmsvr/uiapi?cmd=readXml',
reader: {
type: 'json',
rootProperty: 'data'
}
}
});
Purchase Model:
Ext.define('Pvm.model.Purchase', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json'
],
fields: [{
name: 'item',
type: 'auto'
}, {
name: 'qty',
type: 'auto'
}, {
name: 'price',
type: 'auto'
}],
associations: {
type: 'belongsTo',
model: 'Pvm.model.Customer'
}
});
Controller Code:
onStoreLoaded: function(store, records, successful, eOpts) {
console.log(store); // works
console.log(records); // works
console.log(records[0].get('name')); // works
console.log(records[0].purchase(); // doesn't work; returns 'undefined'
}
I am idiot. I needed the child model's class name in the parent's requires...

Not Able to load data into a listview from store in Sencha touch

I am a beginner in sencha programming trying to build an app that takes in data from a remote server and displays it as a list in my app. I am trying to load data from a JSON store into a listview. My Store Code is:
Ext.define('RestaurantGlobal.store.CategoryColumnsStore', {
extend: 'Ext.data.Store',
requires: [
'RestaurantGlobal.model.CategoryColumns'
],
config: {
model: 'RestaurantGlobal.model.CategoryColumns',
storeId: 'CategoryColumnsStore',
proxy: {
type: 'ajax',
url: 'http://eatpunjab.gramapp.in/Data/?Model=Category&All=True',
reader: {
type: 'json',
rootProperty: 'fields'
},
autoLoad: true
}
}
});
My Model Code is:
Ext.define('RestaurantGlobal.model.CategoryColumns', {
extend: 'Ext.data.Model',
uses: [
'RestaurantGlobal.model.BaseModelColumns'
],
config: {
fields: [
{
name: 'Name',
type: 'string'
},
{
name: 'Image',
type: 'string'
}
],
}
});
and my view code is:
var categorystore = Ext.create("RestaurantGlobal.store.CategoryColumnsStore");
Ext.define('RestaurantGlobal.view.CategoriesPage', {
extend: 'Ext.Panel',
config: {
styleHtmlCls: 'maincontainer',
styleHtmlContent: true,
layout: {
type: 'vbox'
},
items: [
{
xtype: 'titlebar',
flex: 0.5,
docked: 'top',
title: 'Menu'
},
{
xtype: 'list',
flex: 4,
cls:'customcontainer',
margin: '0 10 0 10',
itemTpl: [
'<img src="http://eatpunjab.gramapp.in/media/{Image}" width="40" height="40"/><h2>{Name}</h2>'
],
scrollToTopOnRefresh: false,
store: categorystore
},
]
}
});
When I open the app in my browser, I get a blank listview.
What is missing in my code that is causing the data to not be loaded into the listview?
autoLoad is a config option for the store, but you've put it in the proxy.

EXTjs treegrid - correct model

A very simple example, but it does not work. List of Working Groups looped. What am I doing wrong? How to correct write a model for this json. Docs
http://docs.sencha.com/extjs/4.0.7/#!/api/Ext.data.reader.Reader
Below is a screenshot result.
https://ps.vk.me/c537518/u155966612/doc/2ee8ec3e7718/1234.png
JS code.
Ext.define('WorckGroup', {
extend: 'Ext.data.Model',
fields: [
{ name: 'text', type: 'string', mapping: 'WorckGroup' },
],
hasMany: { name: 'children', model: 'UserReport', associationKey: 'UserReport' }
});
Ext.define('UserReport', {
extend: 'Ext.data.Model',
fields: [
{ name: 'text', type: 'string', mapping: 'Specialist' },
],
belongsTo: 'WorckGroup'
});
var store = new Ext.data.TreeStore({
model: 'WorckGroup',
proxy: {
type: 'ajax',
url: '/omnireports/ajaxgrid'
},
autoLoad: true,
folderSort: true
});
Ext.define('basegrid', {
extend: 'Ext.tree.Panel',
xtype: 'tree-grid',
title: 'Core Team Projects',
height: 300,
useArrows: true,
rootVisible: false,
multiSelect: true,
singleExpand: false,
initComponent: function () {
this.width = 500;
Ext.apply(this, {
store: store,
columns: [{
xtype: 'treecolumn',
text: 'Task',
flex: 2,
sortable: true,
dataIndex: 'text'
},]
});
this.callParent();
}
});
json
[{"WorckGroup":"Группа поддержки 3D",
"UserReport":[
{"Specialist":"Вагин Александр Константинович","SCallCount":64,"AverageDuration":0.1136067,"leaf":true},
{"Specialist":"Кистерева Татьяна Алексеевна","SCallCount":171,"AverageDuration":0.1058641,"leaf":true},
{"Specialist":"Лысенко Алексей Валентинович","SCallCount":71,"AverageDuration":0.4269940,"leaf":true}]},
{"WorckGroup":"Группа поддержки сервиса КСУП",
"UserReport":[
{"Specialist":"Болгов Руслан Евгеньевич","SCallCount":62,"AverageDuration":0.1093749,"leaf":true},
{"Specialist":"Костин Алексей Алексеевич","SCallCount":17,"AverageDuration":0.1125816,"leaf":true},
{"Specialist":"Мироненко Анна Владимировна","SCallCount":172,"AverageDuration":0.0632347,"leaf":true},
{"Specialist":"Ползиков Мирослав Александрович","SCallCount":315,"AverageDuration":0.0945766,"leaf":true},
{"Specialist":"Тахтенков Николай Владимирович","SCallCount":7,"AverageDuration":0.2342261,"leaf":true}]}

Why can't I get a simple Combobox to work in EXTjs 4 using a JSONStore?

This is driving me insane. I have a JS file that is:
Ext.onReady(function(){
Ext.QuickTips.init();
var SitesStore = new Ext.data.JsonStore({
autoLoad: true,
fields: [{
name: 'id',
mapping: 'id'
}, {
name: 'name',
mapping: 'name'
}],
proxy: {
type: 'ajax',
url : '/sites/getsites.do'
},
storeId: 'SitesStore',
root: 'sites',
url: '/sites/getsites.do',
xtype: 'jsonstore'
});
SitesStore.load(function(data){
Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose Site...',
store: SitesStore,
data: data[0].raw["sites"],
queryMode: 'remote',
displayField: 'name',
valueField: 'id',
renderTo: "timesheetSearch"
});
console.log(data[0].raw["sites"]);
});
}); //end onReady
My /sites/getsites.do returns JSON data in the following format:
{
-sites: [
-{
id: "12345678"
name: "Blah Warehouse"
},
-{
id: "5999B91DF6C0"
name: "WalMart Warehouse"
},
...
}
I realize the data[0].raw["sites"] probably isn't the preferred way to access the data but I do confirm that the data is being populated and I'm getting back 136 sites.
The combobox DOES render. However, the live search doesn't work and there are no entries.
I'm new to ExtJS.
Any tips would be appreciated.
Thanks
UPDATE
This code WORKS
var SitesStore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: ['id','name'],
data: [{'id':'aaaa', 'name':'Honeywell'}],
storeId: 'SitesStore',
root: 'sites'
});
Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose Site...',
store: SitesStore,
queryMode: 'remote',
displayField: 'name',
triggerAction: 'all',
valueField: 'id',
renderTo: "timesheetSearch"
});
This does NOT
var SitesStore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: ['id','name'],
proxy: {
type: 'ajax',
url: '/sites/getsites.do'
},
storeId: 'SitesStore',
root: 'sites'
});
Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose Site...',
store: SitesStore,
queryMode: 'remote',
displayField: 'name',
triggerAction: 'all',
valueField: 'id',
renderTo: "timesheetSearch"
});
When I run it without a proxy (and specify a url) I get an error saying I didn't specify a proxy.
Thanks
UPDATE
UGH!!!!!!
I got it. Here is the correct JSONStore
var SitesStore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: ['id','name'],
proxy: {
type: 'ajax',
url: '/sites/getsites.do',
reader: {
type:'json',
root: 'sites'
}
},
storeId: 'SitesStore',
root: 'sites'
});
Thanks everyone. Couldn't believe how hard this was. Mainly because I couldn't find a good tutorial. lol
UPDATE
I was able to figure this out. Here is the working code:
var SitesStore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: ['id','name'],
proxy: {
type: 'ajax',
url: '/sites/getsites.do',
reader: {
type:'json',
root: 'sites'
}
},
storeId: 'SitesStore',
root: 'sites'
});
Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose Site...',
store: SitesStore,
queryMode: 'remote',
displayField: 'name',
triggerAction: 'all',
valueField: 'id',
renderTo: "timesheetSearch"
});
I never could get JSONStore to work so I created a standard Store and had to match the reader attribute with my JSON data.
Hope this helps someone.
That's a bit dirty and my first ExtJS4 lines (I am used to v2.x/3.x) but it should work. As I see you use a browser with console so you will be able to debug if there are some errors. Also you should take a look at the API
var SitesStore = new Ext.data.JsonStore({
autoLoad: true,
fields: [{
name: 'id',
mapping: 'id'
}, {
name: 'name',
mapping: 'name'
}],
proxy: {
type: 'ajax',
url : '/sites/getsites.do'
},
storeId: 'SitesStore',
root: 'sites',
url: '/sites/getsites.do'
// ,xtype: 'jsonstore'
});
Ext.onReady(function(){
Ext.QuickTips.init();
Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose Site...',
store: SitesStore,
// data: data[0].raw["sites"],
queryMode: 'remote',
displayField: 'name',
valueField: 'id',
renderTo: "timesheetSearch",
listeners:{
scope: this,
'select': function(field, value){
console.log(value);
}
}
});
});