grid-jsondata not working in extjs3.4 - json

I am trying to use Jsonstore. I am not able to display the data in the grid, lot of empty rows are being displayed.
I want to display the data in testString variable in the grid.
Help me with this?
var testString = new String();
testString = Ext.util.JSON.encode(data);
this is the value in testString
//value Of testString: //[{"dc1":"abcd","dc2":"efg","dc3":"abc","dc4":"abc"}];
Store and grid
var dcStore = new Ext.data.JsonStore({
autoLoad: true,
fields: ['dc1','dc2','dc3','dc4'],
data : testString
});
this.dcGrid = new Ext.grid.GridPanel({
title: 'View',
store: dcStore,
columns:[
{header: 'dc1',dataIndex: 'dc1'},
{header: 'dc2', dataIndex: 'dc2'},
{header: 'dc3', dataIndex: 'dc3'},
{header: 'dc4',dataIndex: 'dc4'}
]
});
If i assign the value to testString directly like this, data will be displayed in grid
var testString = [{"dc1":"abcd","dc2":"efg","dc3":"abc","dc4":"abc"}];
Fiddle Update:
Ext.application({
name : 'Fiddle',
launch : function() {
var testData = {"data":[{"dc1":"re","dc2":"we","dc3":"qw","dc4":"qwe"}]};
var testDataJson = Ext.util.JSON.decode(testData);
var testDataString = Ext.util.JSON.encode(testDataJson);
var dcStore = new Ext.data.JsonStore({
fields: ['dc1','dc2','dc3','dc4'],
data : testDataString
});
var dcGrid = new Ext.grid.GridPanel({
renderTo: Ext.getBody(),
title: 'View',
store: dcStore,
columns:[
{header: 'dc1',dataIndex: 'dc1'},
{header: 'dc2', dataIndex: 'dc2'},
{header: 'dc3', dataIndex: 'dc3'},
{header: 'dc4',dataIndex: 'dc4'}
]
});
}
});

JsonStores are usually used to pull in data from a remote source in JSON format, This could be from an AJAX request or directly to a JSON file, Then the reader parses this information into the store. If you already have the data locally then you are probably better using an ArrayStore. Also your local data is likely to be in JSON format already as its a JS object.
Also your grid was not configured correctly and only shown the first row for me, I've used the example code for a GridPanel from the API documentation here.
I can't give a direct fix for your issue above without knowing what is in the data variable in this line of code testString = Ext.util.JSON.encode(data); so I've put an example of using both an array store and a remote JSON store below for your reference which should help.
// app.js
Ext.onReady(function(){
Ext.BLANK_IMAGE_URL = '/js/ext-3.4.0/resources/images/default/s.gif';
var arrayData = [
["abc", "abc", "abc", "abc"],
["abc1", "abc1", "abc1", "abc1"],
["abc2", "abc2", "abc2", "abc2"],
["abc3", "abc3", "abc3", "abc3"],
["abc4", "abc4", "abc4", "abc4"]
];
var arrayStore = new Ext.data.ArrayStore({
// store configs
autoDestroy: true,
storeId: 'myStore',
fields: ['dc1', 'dc2', 'dc3', 'dc4'],
data: arrayData
});
var grid = new Ext.grid.GridPanel({
renderTo: Ext.getBody(),
store: arrayStore,
colModel: new Ext.grid.ColumnModel({
defaults: {
width: 120,
sortable: true
},
columns: [
{id: 'dc1', header: 'dc1', width: 200, sortable: true, dataIndex: 'dc1'},
{header: 'dc2', dataIndex: 'dc2'},
{header: 'dc3', dataIndex: 'dc2'},
{header: 'dc4', dataIndex: 'dc2'}
]
}),
viewConfig: {
forceFit: true
},
sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
width: 600,
height: 300,
frame: true,
title: 'Framed with Row Selection and Horizontal Scrolling',
iconCls: 'icon-grid'
});
var jsonStore = new Ext.data.JsonStore({
// store configs
autoDestroy: true,
autoLoad: true,
url: 'data/data.json',
storeId: 'myStore',
// reader configs
root: 'rows',
idProperty: 'dc1',
fields: ['dc1', 'dc2', 'dc3', 'dc4'],
});
var grid2 = new Ext.grid.GridPanel({
renderTo: Ext.getBody(),
store: jsonStore,
colModel: new Ext.grid.ColumnModel({
defaults: {
width: 120,
sortable: true
},
columns: [
{id: 'dc1', header: 'dc1', width: 200, sortable: true, dataIndex: 'dc1'},
{header: 'dc2', dataIndex: 'dc2'},
{header: 'dc3', dataIndex: 'dc2'},
{header: 'dc4', dataIndex: 'dc2'}
]
}),
viewConfig: {
forceFit: true
},
sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
width: 600,
height: 300,
frame: true,
title: 'Framed with Row Selection and Horizontal Scrolling',
iconCls: 'icon-grid'
});
});
// data.json
{
"rows": [{
"dc1": "abc",
"dc2": "abc",
"dc3": "abc",
"dc4": "abc"
}, {
"dc1": "test1",
"dc2": "abc",
"dc3": "abc",
"dc4": "abc"
}, {
"dc1": "test 2",
"dc2": "abc",
"dc3": "abc",
"dc4": "abc"
}]
}

Related

Unable to show JSON data into Grid Panel. Shows only one blank row in Grid

Unable to show JSON data into Grid. I got blank grid but you can see sequence no "1" and blank row, though number 1 is auto generated, it is not a JSON data.
code
Ext.onReady(function(){
// PRESSURE GRID - PRESSURE TAB //
var proxy=new Ext.data.HttpProxy( {url:'',method: 'POST'} );
Ext.define('pressureModel', {
extend: 'Ext.data.Model',
fields: ['month', 'KOD', 'timePeriod', 'beachBank', 'manMade', 'charterBoat', 'privateRentalBoat']
});
var pressureGridStore=new Ext.data.Store({
id: "pressureG",
model: 'pressureModel',
proxy: proxy,
reader:new Ext.data.JsonReader({
type : 'json',
root: 'pressureFi',
totalProperty: 'pressureResultLength'
},[{name:'month'},{name:'KOD'},{name:'timePeriod'},{name:'beachBank'},{name:'manMade'},{name:'charterBoat'},{name:'privateRentalBoat'}]
)
});
// Generic fields array to use in both store defs.
var pressureFields = [
{name: 'month', mapping: 'month', type: 'string'},
{name: 'KOD', mapping: 'KOD', type: 'string'},
{name: 'timePeriod', mapping: 'timePeriod', type: 'string'},
{name: 'beachBank', mapping: 'beachBank', type: 'string'},
{name: 'manMade', mapping: 'manMade', type: 'string'},
{name: 'charterBoat', mapping: 'charterBoat', type: 'string'},
{name: 'privateRentalBoat', mapping: 'privateRentalBoat', type: 'string'}
];
var pressureGrid = new Ext.grid.GridPanel({
id : "pressure-grid",
ddGroup : 'gridDDGroup',
store : pressureGridStore,
columns: [new Ext.grid.RowNumberer(),
{
text: 'Month',
width: 70,
dataIndex: 'month'
},{
text: 'Kind of Day',
width: 85,
dataIndex: 'KOD'
},{
text: 'Time Period',
width: 95,
dataIndex: 'month'
},{
text: 'Beach/Bank',
width: 65,
dataIndex: 'beachBank'
},{
text: 'Man/Made',
width: 65,
dataIndex: 'manMade'
},{
text: 'Charter Boat',
width: 75,
dataIndex: 'charterBoat'
},{
text: 'Private/Rental Boat',
width: 105,
dataIndex: 'privateRentalBoat'
}],
enableDragDrop : true,
stripeRows : true,
autoExpandColumn : 'name',
width : 624,
height : 325
});
function handleActivate(tab){ alert("in handle ");
pressureGridStore.proxy.url='siteUtil.jsp';
pressureGridStore.load({params:
{'method':'getSitePressureInfo'}
});
}
tabPanelObject = {
getTabPanel: function(siteId) {
var infoPanel = new Ext.tab.Panel({
id: 'tabPan',
xtype: 'tabpanel',
title: 'Site Information',
height: 1000,
width: '50.4%',
items:[
{
title: 'Pressure',
id: 'pressureTab',
listeners: {activate: handleActivate},
items:[
{
xtype: "panel",
width : '100%',
height : 300,
layout: 'fit',
items: [
pressureGrid
] }
]}
]
});
return infoPanel;
}
}
});
Json Response is as follow
{"pressureResultLength":"96","pressureFi":[{"charterBoat":9,"timePeriod":"0200- 0800","KOD":"WEEKDAY","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9}, {"charterBoat":0,"timePeriod":"0800-1400","KOD":"WEEKDAY","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9},{"charterBoat":0,"timePeriod":"1400-2000","KOD":"WEEKDAY","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"2000-0200","KOD":"WEEKDAY","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"0200-0800","KOD":"WEEKEND","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9},{"charterBoat":0,"timePeriod":"0800-1400","KOD":"WEEKEND","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9},{"charterBoat":0,"timePeriod":"1400-2000","KOD":"WEEKEND","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"2000-0200","KOD":"WEEKEND","beachBank":9,"month":"JAN","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"0200-0800","KOD":"WEEKDAY","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":0,"timePeriod":"0800-1400","KOD":"WEEKDAY","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":0,"timePeriod":"1400-2000","KOD":"WEEKDAY","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"2000-0200","KOD":"WEEKDAY","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"0200-0800","KOD":"WEEKEND","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":0,"timePeriod":"0800-1400","KOD":"WEEKEND","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":0,"timePeriod":"1400-2000","KOD":"WEEKEND","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"2000-0200","KOD":"WEEKEND","beachBank":9,"month":"FEB","privateRentalBoat":9,"manMade":9},{"charterBoat":9,"timePeriod":"0200-0800","KOD":"WEEKDAY","beachBank":9,"month":"MAR","privateRentalBoat":9,"manMade":9}]}
I checked the FireBug Console. It returns the response as above but it is of type(actionmethod) 'GET'
When I did
pressureGridStore.on({
'load':{
fn: function(store, records, options){
//store is loaded, now you can work with it's records, etc.
console.info('store load, arguments:', arguments);
console.info('Store count = ', store.getCount());
},
scope:this
}
});
I got Store Count = 1.
-Ankit
the problem is that your components aren't defined correctly. for instance, the reader config in extjs4 doesn't belong to store, it belongs to proxy see http://dev.sencha.com/deploy/ext-4.0.2a/docs/#/api/Ext.data.proxy.Ajax so for the proxy you should have
var proxy=new Ext.data.HttpProxy( {
url:'',
reader:new Ext.data.JsonReader({
type : 'json',
root: 'pressureFi',
totalProperty: 'pressureResultLength'
})
})
I think with this modification it should work

store is undefined

I have this problem on ExtJS. I created a grid that display on a window, it will get json data from an external server side script. It is rougly working but the firebug keeps telling me that store is undefined everything I click on the columns in the grid. Can you please help me solve this. Here is my code:
var fields = [{
name: "Name",
type: "text"
}, {
name: "NSId",
type: "number"
}, {
name: "Id",
type: "number"
}, {
name: "Version",
type: "number"
}];
var columns = [{
header: 'Name',
id: 'Name',
width: 160,
sortable: true,
dataIndex: 'Name'
}, {
header: 'NSId',
id: 'NSId',
width: 45,
sortable: true,
hidden: true,
dataIndex: 'NSId'
}, {
header: 'Id',
id: 'Id',
width: 30,
sortable: true,
hidden: true,
dataIndex: 'Id'
}, {
header: 'Version',
id: 'Version',
width: 50,
sortable: true,
hidden: true,
dataIndex: 'Version'
}];
var searchAndPickUrl = OcsConfig.url.refto;
var pickerUrl = '?picker=' + config.picker;
var createRefTo = searchAndPickUrl + pickerUrl;
var jsonstore = {
xtype: 'jsonstore',
autoDestroy: true,
fields: fields,
root: 'candidates',
idProperty: 'Name',
url: createRefTo,
autoLoad: true
};
var cmpGrid = null;
var clickCell = function (trigger) {
var record = cmpGrid.getSelectionModel().getSelected();
var selValues = record.data['name'] + ',' + record.data['nsid'] + ',' + record.data['id'] + ',' + record.data['version'];
Ext.Msg.alert('Name', selValues);
};
var refToGrid = {
id: Ext.id(),
xtype: 'grid',
store: jsonstore,
columns: columns,
stripeRows: true,
defaults: {
anchor: '100%'
},
autoExpandColumn: 'Name',
height: 350,
width: 600,
title: '',
stateful: true,
stateId: 'grid',
listener: {
rowdblclick: clickCell
}
};
var refToWin = function refToInputVal(trigger) {
var values = config.options || [];
var cmpWin = null;
var genId = Ext.id();
var win = null;
if (cmpWin === null) {
win = {
xtype: 'window',
title: config.caption || '',
modal: true,
items: [{
xtype: 'form',
id: genId,
padding: 5,
defaults: {
anchor: '100%'
},
items: [refToGrid]
}],
bbar: [{
xtype: 'button',
text: 'OK',
handler: function () {
returnValue(trigger, cmpGrid);
cmpWin.hide();
}
}, {
xtype: 'button',
text: 'Cancel',
handler: function () {
cmpWin.hide();
}
}],
Thanks.
Change your store declaration...
var jsonstore = new Ext.data.JsonStore({
autoDestroy: true,
fields: fields,
root: 'candidates',
idProperty: 'Name',
url: createRefTo,
autoLoad: true
});

ExtJS Grid doesn't load data

I'm trying to display data in a grid by using JsonStore.
The grid gets rendered, but it doesn't display the data.
The JSON data returned by API.ashx:
[{"Account":{"Username":"root","Password":null,"Enabled":true,"Id":1},"Text":"Hallo Welt!","Id":1},{"Account":{"Username":"root","Password":null,"Enabled":true,"Id":1},"Text":"hihihi","Id":3}]
My code:
Ext.onReady(function () {
var store = new Ext.data.JsonStore({
url: 'API.ashx?type=notes&action=getAll',
root: 'Note',
autoload: true,
fields: ['Text', { name: 'Id', type: 'int'}]
});
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{
id: 'Note',
header: 'Id',
width: 25,
sortable: true,
dataIndex: 'Id'
},
{
header: 'Text',
width: 160,
sortable: true,
dataIndex: 'Text'
}
],
stripeRows: true,
autoExpandColumn: 'Note',
height: 350,
width: 600,
title: 'Notes',
stateful: true,
stateId: 'grid'
});
store.load();
grid.render('grid-example');
});
I just fixed it myself. I had to remove the option "root" and now it works fine.

Grid loaded from JSON file its works in IE but not able to load in fireFox?

I am using ExtJS3.3.1 and try to load a Grid from file which contains a JSON Object.
It will work fine in IE loads grid with data but not works in Firefox.
here is simple code It doesn't give any error and warnning.
var store = new Ext.data.Store({
url: 'http://localhost/Vaishali/src/survey.html',
reader: new Ext.data.JsonReader({
root: 'data',
id: 'appeId',
totalProperty: 'total'
}, [{
name: 'appeId',
mapping: 'appeId'
}, {
name: 'survId',
mapping: 'survId'
}, {
name: 'location',
mapping: 'location'
}, {
name: 'surveyDate',
mapping: 'surveyDate'
}, {
name: 'surveyTime',
mapping: 'surveyTime'
}, {
name: 'inputUserId',
mapping: 'inputUserId'
}])
});
store.load();
var grid = new Ext.grid.GridPanel({
store: store,
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
}],
width: 540,
height: 200
});
so can u please tell me why this happens.
oops
when I added the event onload and check the exception in mozilla firefox it will show exception in that status code show 0 and text "connection Failure'.
but in IE it will give proper connection and result.
I'd suspect you're not loading the page from http://localhost/ as well, so you're running afoul of the same origin policy. IE's more forgiving than it strictly speaking should be.

ExtJS Grid JSON Store Proxy Error

I am working on displaying a ExtJS Grid. I got everything correct except the part about proxy with in the Json store. When I try to display it throws an error:
this.proxy is undefined
Ext.data.Store=function(A){this.data=n...ta=C;this.resumeEvents();return B}});
I figured out that i need to do something in the store but not sure what. the variable list data has the Json Array string. Please any help is really appreciated. thanks
var listData = <%= aItems %> ;
var AIGrid;
Ext.onReady(function () {
var AIRecord = Ext.data.Record.create([{
name: "ID",
type: "int",
mapping: "ID"
}, {
name: "WSTITLE",
type: "string",
mapping: "WSTITLE"
}, {
name: "REQ_ATTUID",
type: "string",
mapping: "REQ_ATTUID"
}, {
name: "DESCRIPTION",
type: "string",
mapping: "DESCRIPTION"
}, {
name: "RES_ATTUID",
type: "string",
mapping: "RES_ATTUID"
}, {
name: "RESOLUTION",
type: "string",
mapping: "RESOLUTION"
}, {
name: "START_TIME",
type: "string",
mapping: "START_TIME"
}, {
name: "END_TIME",
type: "string",
mapping: "END_TIME"
}]);
var AIreader = new Ext.data.JsonReader({
root: "root",
id: "ID"
}, AIRecord);
var AIstore = new Ext.data.Store({
nocache: true,
data: listData,
reader: AIreader
});
var AIcol = new Ext.grid.ColumnModel([{
header: 'ID',
readOnly: true,
dataIndex: 'ID',
width: 30
}, {
header: 'Work Step',
dataIndex: 'WSTITLE',
width: 200,
readOnly: true
}, {
header: 'Requester',
dataIndex: 'REQ_ATTUID',
width: 80,
readOnly: true
}, {
header: 'Description',
dataIndex: 'DESCRIPTION',
width: 300,
readOnly: true
}, {
header: 'Resolver',
dataIndex: 'RES_ATTUID',
width: 80,
readOnly: true
}, {
header: 'Resolution',
dataIndex: 'RESOLUTION',
width: 300,
readOnly: true
}, {
header: 'Start',
dataIndex: 'START_TIME',
width: 100,
readOnly: true
}, {
header: 'End',
dataIndex: 'END_TIME',
width: 100,
readOnly: true
}]);
AIcol.defaultSortable = true;
AIGrid = new Ext.grid.GridPanel({
store: AIstore,
renderTo: 'listgrid',
cm: AIcol,
enableColLock: true,
title: 'Open Action Items',
width: 900,
height: 500,
selModel: new Ext.grid.RowSelectionModel({
singleSelect: true
})
});
AIGrid.store.load();
});
I got the answer. with a little extra research over the internets. I used MemoryProxy to load the local data. that solved the problem. Yay!!