How to bind JSON to EXTJS grid - extjs

Can someone explain to me why this isnt working? No errors whatsoever.
Ext.onReady(function () {
//Ext.Msg.alert('Status', 'Changes saved successfully.');
Ext.define('Bond', {
extend: 'Ext.data.Model',
fields: ['CUSIP', 'DESCRIPTION', 'COUPONRATE', 'ASKPRICE']
});
var store = new Ext.data.Store({
model: 'Bond',
proxy: {
type: 'ajax',
url: 'http://localhost:3197/Home/GetSecondaryOfferings',
reader: {
type: 'json',
root: 'items',
totalProperty: 'totalCount'
}
}
});
store.load();
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{ header: 'CUSIP', dataIndex: 'CUSIP' },
{ header: 'Description', dataIndex: 'DESCRIPTION', width: 100 },
{ header: 'COUPONRATE', dataIndex: 'COUPONRATE', width: 100 },
{ header: 'ASKPRICE', dataIndex: 'ASKPRICE', width: 100 }
],
renderTo: 'example-grid',
width: 1000,
autoHeight: true,
title: 'JSON SIMPLE 2'
}).render();
});
this is what my JSON object look like:
{"totalCount":3316,"items":[{"CUSIP":"989701AL1","DESCRIPTION":"ZIONS BANCORPORATIONSB NT 5.65000% 05/15/2014","COUPONRATE":" 5.650","ASKPRICE":" 104.450"}]}
The grid just doesnt populate, and I can see the JSON being returned to me from the server.
Thoughts?

The problem is the fact that you use a store with the alax/json type. Bacause you have a cross domain URL, the JSON.
Solution: Make sure the test files are hosted using HTTP from the same domain and all should be well:
var store = new Ext.data.Store({
model: 'Bond',
proxy: {
type: 'ajax',
url: 'SOData.json',
reader: {
type: 'json',
root: 'items',
totalProperty: 'totalCount'
}
}
});

try this way
var URLdata = window.location.protocol + "//" + window.location.host + "/" + "bigdata-dashboard" + "/conf/" +"survey.json" ;
var storedata = new Ext.data.Store({
fields: ['appeId','survId','location','surveyDate','surveyTime','inputUserId'],
proxy: {
type: 'rest',
url:URLdata,
reader: {
type: 'json',
root: 'items',
// totalProperty: 'totalCount'
}
}
});
storedata.load();
// create the grid
var grid = new Ext.grid.GridPanel({
store: storedata,
columns: [
{header: "appeId", width: 60, dataIndex: 'appeId', sortable: true},
{header: "survId", width: 60, dataIndex: 'survId', sortable: true},
{header: "location", width: 60, dataIndex: 'location', sortable: true},
{header: "surveyDate", width: 100, dataIndex: 'surveyDate', sortable: true},
{header: "surveyTime", width: 100, dataIndex: 'surveyTime', sortable: true},
{header: "inputUserId", width:80, dataIndex: 'inputUserId', sortable: true}
],
renderTo:'example-grid',
width:470,
height:200
});

you need to change your service to return jsonp and change the store type to be jsonp, that will fix the issue,
Please let me know if you need more information

Related

Ext JS 4 - Paging toolbar only shows first page

I have a basic gridpanel that is fed from a store with 600 records, but my paging toolbar only shows the first page, regardless of the pageSize config in the store. For example...
When pageSize = 50, the paging toolbar reads:
Page 1 of 1 | Displaying 1 - 50 of 50
When pageSize = 100, the paging toolbar reads:
Page 1 of 1 | Displaying 1 - 100 of 100
Thoughts? Source below.
employee_store.js
var empUrlRoot = 'http://extjsinaction.com/crud.php'
+ '?model=Employee&method=';
//Instantiate employee store
var employeeStore = Ext.create('Ext.data.Store', {
model: 'Employee',
pageSize: 50,
proxy: {
type: 'jsonp',
api: {
create: empUrlRoot + 'CREATE',
read: empUrlRoot + 'READ',
udpate: empUrlRoot + 'UPDATE',
destroy: empUrlRoot + 'DESTROY'
},
reader: {
type: 'json',
metaProperty: 'meta',
root: 'data',
idProperty: 'id',
successProperty: 'meta.success'
},
writer: {
type: 'json',
encode: true,
writeAllFields: true,
root: 'data',
allowSingle: true,
batch: false,
writeRecords: function(request, data){
request.jsonData = data;
return request;
}
}
}
});
gridpanel.js
Ext.onReady(function(){
// gridpanel column configurations
var columns = [
{
xtype: 'templatecolumn',
header: 'ID',
dataIndex: 'id',
sortable: true,
width: 50,
resizable: false,
hidden: true,
// template renders id column blue:
tpl: '<span style="color: #0000FF;">{id}</span>'
},
{
header: 'Last Name',
dataIndex: 'lastName',
sortable: true,
hideable: false,
width: 100
},
{
header: 'First Name',
dataIndex: 'firstName',
sortable: true,
hideable: false,
width: 100
},
{
header: 'Address',
dataIndex: 'street',
sortable: false,
flex: 1,
// template renders column with additional content:
tpl: '{street}<br />{city} {state}, {zip}'
}
];
// Configure the gridpanel paging toolbar
var pagingtoolbar = {
xtype: 'pagingtoolbar',
store: employeeStore,
dock: 'bottom',
displayInfo: true
};
// Create gridpanel
var grid = Ext.create('Ext.grid.Panel', {
xtype: 'grid',
columns: columns,
store: employeeStore,
loadMask: true,
selType: 'rowmodel',
singleSelect: true,
stripeRows: true,
dockedItems: [
pagingtoolbar
]
});
// Configure and create container for gridpanel
Ext.create('Ext.Window', {
height: 350,
width: 550,
border: false,
layout: 'fit',
items: grid //the gridpanel
}).show();
// Load the employeeStore
employeeStore.load();
});
employee_model.js
// Define employee model
Ext.define('Employee', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [
{name: 'id',type: 'int'},
{name: 'departmentId', type: 'int' },
{name:'dateHired', type:'date', format:'Y-m-d'},
{name:'dateFired', type:'date', format:'Y-m-d'},
{name:'dob', type:'date', format: 'Y-m-d'},
'firstName',
'lastName',
'title',
'street',
'city',
'state',
'zip'
]
});
You have to return total record count.
reader: {
root : 'data',
totalProperty: 'total' // this is default, you can change it
}
Next in server response you need:
{
data : ['your data here'],
total : 2000
}

EXTJS 4 sortInfo is not working in JsonStore

Ext.define('RouteSeqModel', {
extend: 'Ext.data.Model',
fields: [{name: '_id', type: 'number'}, {name: 'Route_Seq' , type: 'int'},'Location_Name','Location_ID','Route_ID']
});
var RouteSeqStore = Ext.create('Ext.data.JsonStore', {
model: 'RouteSeqModel',
storeId: 'RouteSeqStore',
autoLoad: false,
sortInfo: { field: "Route_Seq", direction: "ASC" },
proxy: {
type: 'ajax',
url: 'get-routeseq.php',
api: {
create: 'insert-routeseq.php',
update: 'update-routeseq.php',
},
actionMethods: 'POST',
baseParams: {
_id : 0,
},
reader: {
type: 'json',
idProperty: '_id'
},
writer: {
type: 'json',
id: '_id'
}
}
});
Ext.define('MyApp.view.MyGridPanelRouteSeq', {
extend: 'Ext.grid.Panel',
id:'MyGridPanelRouteSeq',
alias: 'widget.mygridpanelrouteseq',
renderTo: Ext.getBody(),
height: window.innerHeight,
width: window.innerWidth/2,
title: 'Route Sequence Setting',
sortableColumns: false,
store: RouteSeqStore,
columns: [
{
xtype: 'gridcolumn',
width: 70,
dataIndex: 'Route_Seq',
text: 'Sequence'
},
{
xtype: 'gridcolumn',
width: 160,
dataIndex: 'Location_Name',
text: 'Location Name'
}]
})
Sequence is read the data from Route_Seq, but the column is still not sorting yet.
i have no idea how come the grid is still not sorting..why?
Where did you get sortInfo from? It's not a valid store config.
You want:
sorters: [{
property: 'Route_Seq',
direction: 'DESC'
}]

ExtJS - EditorGridPanel: error when cell is selected

I'm getting an error when selecting cells in my EditorGridPanel. Here's a snippet of my code:
var bannerGrid = new Ext.grid.EditorGridPanel({
store: bannerStore,
cm: new Ext.grid.ColumnModel({
defaults: {
sortable: true,
menuDisabled: true
},
columns:
{
header: '<img src="img/oo-icon.png" /> <img src="img/network-icon.png" />',
width: 52,
dataIndex: 'inventory',
align: 'center',
renderer: inventoryIcon,
}, {
header: "Name",
dataIndex: 'bannerName',
editor: new Ext.form.TextField({ allowBlank: false }),
width: 300
}, {
header: "Advertiser",
dataIndex: 'advertiser',
editor: advertisersDropdownGrid,
}, {
header: "Art Type",
dataIndex: 'artType',
editor: artTypeDropdownGrid,
}, {
......
Each of the 'editors' are dropdowns that are defined prior to the grid. The weird thing is that the editor that contains the TextField does not throw the same error.
The error I'm getting when selecting a cell is this:
c.getItemCt() is undefined
[Break On This Error] c.getItemCt().removeClass('x-hide-' + c.hideMode);
Again, this only happens on the ComboBox editors!
From further inspection the error is coming from this part of ext itself:
onFieldShow: function(c){
c.getItemCt().removeClass('x-hide-' + c.hideMode);
if (c.isComposite) {
c.doLayout();
}
},
Which seems to be part of the FormLayout section.
Any ideas? I've tried defining the Combo's inline and that did not fix it.
Thanks!
EDIT: Here's how I define my Combo's using classes.
I define my ComboBoxJSON class: (I've blanked out namespaces just for privacy sake)
***.***.***.ComboBoxJSON = Ext.extend(Ext.form.ComboBox, {
url: '',
root: '',
valueField: 'id',
displayField: 'name',
width: 200,
id: '',
fields: [
{ name: 'id', type: 'int'},
{ name: 'name', type: 'string' }
],
initComponent: function () {
var comboStore = new Ext.data.JsonStore({
id: 'JsonStore',
idProperty: 'id',
autoLoad: true,
idProperty: 'id',
root: this.root,
fields: this.fields,
proxy: new Ext.data.ScriptTagProxy({
api: {
read: this.url,
}
})
});
var config = {
store: comboStore,
displayField: this.displayField,
valueField: this.valueField,
mode: 'local',
minChars: 1,
triggerAction: 'all',
typeAhead: true,
lazyRender: true,
value: this.value,
width: this.width,
id: this.id
}
Ext.apply(this, config);
***.***.***.ComboBoxJSON.superclass.initComponent(this);
}
});
Ext.reg("ibwComboJson", ***.***.***.ComboBoxJSON);
I then define my combos before init on the grid, like so: (I've blocked out the URL, but it does return valid JSON)
var advertisersDropdownGrid = new ***.***.***.ComboBoxJSON({
url: '***',
root: 'advertiserList',
id: 'advertisersDropdownGrid'
});
Found the answer to this awhile back, but the solution is pretty absurdly simple.
***.***.***.ComboBoxJSON.superclass.initComponent.call(this);
Forgot the .call portion. :)

ExtJs 4 grid is not populating with data

I am trying to populate Ext JS 4 Grid with json data. Unfortunatelly no matter what i do it's still remains empty. The JS function is below:
function buildGrid() {
Ext.define('Contact', {
extend: 'Ext.data.Model',
fields: ['Id', 'Name', 'State', 'Age']
});
var store = Ext.create('Ext.data.Store', {
model: 'Contact',
proxy: {
type: 'ajax',
url: 'GridData',
reader: {
type: 'json',
record: 'data',
totalProperty: 'total'
}
}
});
var grid = Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{ text: "Name", flex: 1, width: 120, dataIndex: 'Name', sortable: true },
{ text: "State", width: 100, dataIndex: 'State', sortable: true },
{ text: "Age", width: 115, dataIndex: 'Age', sortable: true },
],
height: 210,
width: 600,
split: true,
renderTo: Ext.getBody()
}).show();
}
Ext.onReady(buildGrid);
The server responce is look like:
{"callback":"1307878654818","total":1,"data":[{"Id":"ac2bedf1-bb5c-46e0-ba50-a6628341ca25","Name":"Smith","State":"NU","Age":24}]}
so it looks ok. However the grid view is not showing the date.
Anyone can see what i am doing wrong?
You haven't specified the root property. Try
reader: {
type: 'json',
root: 'data',
totalProperty: 'total'
}

extjs dynamic store?

I'm trying to build an app using tha MVC model and although most things are going well i'm having issues with the new architecture. The problem at hand is that i have created a store using this
Ext.define('MCMS.store.Items', {
extend: 'Ext.data.Store',
model: 'MCMS.model.Item',
autoLoad: {'limit':10},
pageSize:10,
remoteSort:true,
proxy: {
url:'/ajax/moduleLoaded.php',
actionMethods: 'post',
extraParams :{'code': code,'toLoad':'latestContet','return':'json','module':Ext.getDom('module').value,'test':function(){console.log(this)}},
type: 'ajax',
reader: {
type: 'json',
successProperty: 'success',
root: 'items'
}
}
});
This works fine, but i need the module param to be dynamic depending on the view that is using it. Let's say that i have 1 grid like this
Ext.define('MCMS.view.items.List' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.itemsList',
title : lang.items,
store: 'Items',
loadMask: true,
columns: [
{header: "#ID", width: 60, dataIndex: 'id', sortable: true,filter: {type: 'numeric'}},
{header: "Title", width: 250, dataIndex: 'title', id:'title', sortable: true,filter: {type: 'string'}},
{header: "Availability", width: 60, dataIndex: 'active', sortable: true,renderer: function(value, metaData, record, rowIndex, colIndex, store) { if (value == 1) { return '<span style="color:green;">' +lang.yes + '</span>'; } else { return '<span style="color:red;">' + lang.no + '</span>'; } }},
{header: "Category", width: 200, dataIndex: 'category',sortable:false,renderer: function(value, metaData, record, rowIndex, colIndex, store) {
var cat = new Array;
Ext.each(value, function(person, index) {
cat.push('' + this.category + '');
});
return cat.join(' , '); }},
],
selModel: Ext.create('Ext.selection.CheckboxModel'),
columnLines: true,
dockedItems: [ {
xtype: 'toolbar',
items: [{
text:'Add Something',
tooltip:'Add a new row',
iconCls:'add',
itemId:'addItem'
}, '-', {
text:'Options',
tooltip:'Set options',
iconCls:'option'
},'-',{
itemId: 'removeButton',
text:'Remove Something',
tooltip:'Remove the selected item',
iconCls:'remove',
disabled: true
}]
}],
bbar: {xtype: 'itemsPaging' },
features: [filters],
});
and i use this grid in 3 or 4 different occasions but the data needs to change depending on the view that it's using it. All i need is a way to change the module parameter somehow.
Try using this code:
store.getProxy().extraParams.module = newValue;
store.reload();
If I understand you correctly, you need to use grid.reconfigure() to change the makeup of a grid and allow alterations to its store.
You could set the extraParams directly
store.getProxy().extraParams.module = newValue;

Resources