ExtJS could not load data to grid from JSON file - javascript

I'm a newbee in ExtJS and I have a problem with loading data to grid from JSON file.
Here's my code:
Model:
Ext.define('EXP.model.EXT.EXTModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'id'},
{name: 'name'},
{name: 'email'}
]
});
Store:
Ext.define('EXP.store.EXT.EXTStore', {
extend: 'Ext.data.Store',
storeId:'EXTStore',
model: 'EXP.model.EXT.EXTModel',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'data/users.json',
reader: {
type: 'json',
root: 'users',
successProperty: 'success'
}
}
});
View:
Ext.define('EXP.view.EXT.ComponentsField' ,{
extend: 'Ext.grid.Panel',
alias: 'widget.componentsfield',
title: 'All Users',
store: 'EXT.EXTStore',
initComponent: function() {
this.columns = [
{header: 'Id', dataIndex: 'id', flex: 1},
{header: 'Name', dataIndex: 'name', flex: 1},
{header: 'Email', dataIndex: 'email', flex: 1}
];
this.callParent(arguments);
}
});
JSON File:
{
"users": [
{"id": 1, "name": "Ed", "email": "ed#sencha.com"},
{"id": 2, "name": "Tommy", "email": "tommy#sencha.com"}
]
}
Do I need something more to display this data? I've red a couple of topics about my issue, but none of them was helpful. :(
I haven't any errors in the console. Grid is displaying, but it have no data in it.

Related

How to update a row on a grid from an alert window using ExtJs?

I'm creating an app that simply shows customers information on a table, and if a user is being clicked then a pop-up window shows up showing user's information in a form (name and email). Within this PopUp I want to be able to update customers name and email and then when clicking on Update button I want the new information to show up on the table right away. As of right now I'm able to populate the table with customers' information as well as binding their information with the Pop-up window. But since I'm still kind of new to ExtJS I'm not really sure how to make the update to show right away on the table after clicking on update button. I would really appreciate any help!. Thanks a lot.
Here's my code that works just fine.
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="https://cdnjs.cat.net/ajax/libs/extjs/6.0.0/classic/theme-triton/resources/theme-triton-all-debug_1.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/ext-all.js"></script>
<script type="text/javascript">
Ext.define('UserModal', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'email', type: 'string'}
]
});
Ext.onReady(function () {
// Ajax call
var usersFromAJAX = Ext.create('Ext.data.Store', {
storeId: 'user',
model: 'UserModal',
autoLoad: 'true',
proxy: {
type: 'ajax',
url: 'example.json',
reader: {
type: 'json',
root: 'customers'
}
}
});
// Setting up the Grid
Ext.create('Ext.grid.Panel', {
store: usersFromAJAX,
id: 'user',
title: 'Employees',
iconCls: 'x-fa fa-users',
listeners: {
itemclick: function (view, index, item, record) {
var window = Ext.create('Ext.window.Window', {
xtype: 'formpanel',
title: 'Update Record',
width: 300,
height: 200,
floating: true,
centered: true,
modal: true,
record: record,
viewModel: {
data: {
employee: index.data// employee's name
}
},
items: [{
xtype: 'textfield',
id: 'firstname',
name: 'firstname',
fieldLabel: 'First Name',
bind: '{employee.name}' // biding employee's name
},
{
xtype: 'textfield',
name: 'firstname',
fieldLabel: 'Email',
bind: '{employee.email}' // biding employee's name
},
{
xtype: 'toolbar',
docked: 'bottom',
style:{
background: "#ACCCE8",
padding:'20px'
},
items: ['->', {
xtype: 'button',
text: 'Update',
iconCls: 'x-fa fa-check',
handler: function () {
console.log("Updating name...");
// Need to add something in here
this.up('window').close();
}
}, {
xtype: 'button',
text: 'Cancel',
iconCls: 'x-fa fa-close',
handler: function () {
// this.up('formpanel').destroy();
this.up('window').close();
//this.up('window').destroy();
}
}]
}]
})
window.show();
}
},
columns: [{
header: 'ID',
dataIndex: 'id',
sortable: false,
hideable: true
}, {
header: 'NAME',
dataIndex: 'name',
}, {
header: 'Email',
dataIndex: 'email',
flex: 1 // will take the whole table
}],
height: 300,
width: 400,
renderTo: Ext.getElementById("myTable")
});
});
</script>
</head>
<body>
<div id="myTable"></div>
</body>
</html>
Here's the JSON that populates my table.
{
"customers": [{
"id": 1,
"name": "Henry Watson",
"email": "henry#email.com"
},
{
"id": 2,
"name": "Lucy",
"email": "lucy#email.com"
},
{
"id": 3,
"name": "Mike Brow",
"email": "Mike#email.com"
},
{
"id": 4,
"name": "Mary Tempa",
"email": "mary#email.com"
},
{
"id": 5,
"name": "Beto Carlx",
"email": "beto#email.com"
}
]
}
Binding is for "live" data, so that means it will update the grid as you type. If you want to defer the changes until you hit a button, you can use methods on the form to do so:
Fiddle
Ext.define('UserModal', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'email']
});
Ext.onReady(function () {
// Setting up the Grid
Ext.create('Ext.grid.Panel', {
store: {
model: 'UserModal',
autoLoad: 'true',
proxy: {
type: 'ajax',
url: 'data1.json',
reader: {
type: 'json',
rootProperty: 'customers'
}
}
},
listeners: {
itemclick: function (view, record) {
var f = Ext.create('Ext.form.Panel', {
xtype: 'formpanel',
title: 'Update Record',
width: 300,
height: 200,
floating: true,
centered: true,
modal: true,
buttons: [{
text: 'Update',
iconCls: 'x-fa fa-check',
handler: function () {
f.updateRecord(record);
f.close();
}
}, {
text: 'Cancel',
iconCls: 'x-fa fa-close',
handler: function () {
f.close();
}
}],
items: [{
xtype: 'textfield',
id: 'firstname',
name: 'name',
fieldLabel: 'First Name'
}, {
xtype: 'textfield',
name: 'email',
fieldLabel: 'Email'
}]
})
f.show();
f.loadRecord(record);
}
},
columns: [{
header: 'ID',
dataIndex: 'id',
sortable: false,
hideable: true
}, {
header: 'NAME',
dataIndex: 'name',
}, {
header: 'Email',
dataIndex: 'email',
flex: 1 // will take the whole table
}],
height: 300,
width: 400,
renderTo: document.body
});
});

Unable to Get JSON to Read Properly Without Root and Cross Domain

I'm only asking this because I've seemingly tried everything I can find, and it has to be easier than I'm making it.
I'm working with Sencha Touch (EXTJS knowledge is still helpful). When I console.log the store, it shows no items/data. I am getting a good response from the store's load call, but it's not making it into the store. I appreciate the help.
Model:
Ext.define('NQT.model.NENEventsModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'regionId', type: 'int', mapping: 'regionId'},
{name: 'regionName', type: 'string', mapping: 'regionName'},
{name: 'state', type: 'string', mapping: 'state'},
{name: 'switchId', type: 'int', mapping: 'switchId'},
{name: 'switchName', type: 'string', mapping: 'switchName'}
]
});
Store:
Ext.define('NQT.store.NENEventsStore',
{
extend: 'Ext.data.Store',
storeId: 'NENEventsStore',
config: {
model: 'NQT.model.NENEventsModel',
proxy: {
type: 'jsonp',
url: 'http://this_is_not_the_url.com:8080/rest/json',
method: 'GET',
pageParam: false,
startParam: false,
limitParam: false,
noCache: false,
reader: {
type: 'json',
rootProperty: ''
}
}
}
});
Part of view:
var eventsStore = Ext.create('NQT.store.NENEventsStore');
{
xtype: 'grid',
titleBar: false,
store: eventsStore,
columns: [
{text: 'regionId', dataIndex: 'regionId', width: 150},
{text: 'regionName', dataIndex: 'regionName', width: 150},
{text: 'state', dataIndex: 'state', width: 150},
{text: 'switchId', dataIndex: 'switchId', width: 150},
{text: 'switchName', dataIndex: 'switchName', width: 150}
]
},
{
xtype: 'button',
docked: 'bottom',
text: 'Reload Grid',
handler: function () {
console.log(eventsStore);
eventsStore.load();
}
}
JSON Sample:
[{"regionId":1,"regionName":"NY Metro","state":"NJ","switchId":167,"switchName":"Jersey City 1"},
{"regionId":1,"regionName":"NY Metro","state":"NY","switchId":2029,"switchName":"Farmingdale 1"},
{"regionId":4,"regionName":"New England","state":"CT","switchId":203,"switchName":"Wallingford 1"}]
As Evan Trimboli pointed out, my response was not as good as I thought it was; it was missing the Ext.data.JsonP.callback that should have preceded it. I created a web service for the URL so I could make the call properly without being a security risk.
Part of the service:
<?php
$app->group('/secret', function () use ($app) {
$app->get('/getData', function () use ($app) {
$callback = $app->request()->get('callback');
$events = file_get_contents("http://example_url.com:8080/this/is/the/path");
if(!empty($callback)){
$app->contentType('application/javascript');
echo sprintf("%s(%s)", $callback, $events);
} else {
echo $events;
}
});
});
?>

ExtJS Grid renders Empty Cells

Model
Ext.define('XXX.User', {
extend: 'Ext.data.Model',
fields: [
'name',
'email',
'nick',
'mobile',
{ name: 'create_time', type: 'date', dateFormat: 'Y-m-d H:i:s' }
]
});
Ajax Response
{
"error": "",
"errno": 0,
"success": true,
"message": "Operation performed successfully",
"data": [{
"nick": "muquaddim1",
"name": "Muquaddim One",
"id": "141",
"mobile": "01710000***",
"email": "muquaddim+1#example.com",
"create_time": "2012-02-26 14:58:29"
}]
}
Store
var user_store = Ext.create('Ext.data.Store', {
// destroy the store if the grid is destroyed
autoDestroy: true,
autoLoad: true,
model: 'XXX.User',
sotreId: 'user-store',
proxy: {
type: 'ajax',
url : 'proxy/user'
},
sorters: [{
property: 'create_time',
direction: 'ASC'
}]
});
Grid
var user_grid = Ext.create('Ext.grid.Panel', {
title: 'List of Users',
store: user_store,
columns: [{
header: 'Nick',
dataIndex: 'nick',
editor: {
allowBlank: false
}
}, {
header: 'Name',
dataIndex: 'name',
flex: 1,
editor: {
allowBlank: false
}
}, {
header: 'Email',
dataIndex: 'email',
width: 160,
editor: {
allowBlank: false,
vtype: 'email'
}
}, {
header: 'Mobile',
dataIndex: 'mobile',
width: 100,
editor: {
allowBlank: false
}
}, {
xtype: 'datecolumn',
header: 'Join Date',
dataIndex: 'create_time',
width: 90,
editor: {
xtype: 'datefield',
allowBlank: false,
format: 'Y-m-d H:i:s',
maxValue: Ext.Date.format(new Date(), 'Y-m-d H:i:s')
}
}]
});
Panel
var users = Ext.create('Ext.panel.Panel', {
title: 'Users',
id: 'user_panel',
items:[user_grid]
});
I am using ExtJS 4.0.7. I modified the code found in Example. Example code works fine. But it does not work. What am I missing here?
Since your data is nested in your response you need to configure a reader in your store's proxy. Try setting up your proxy like this:
proxy: {
type: 'ajax',
url : 'proxy/user'
reader: {
type: 'json',
root: 'data'
}
}

EXTJS grid + JSP data is not loaded

I tried to code EXTJS Grid using jsp. I modify the EXTJS example to get data from jsp page, but the data is not loaded.
Could you please help ?
grid js
<script type="text/javascript">
Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', './js/ux/');
Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.util.*',
'Ext.toolbar.Paging',
'Ext.ux.PreviewPlugin',
'Ext.ModelManager',
'Ext.tip.QuickTipManager'
]);
Ext.onReady(function(){
Ext.tip.QuickTipManager.init();
var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: 'data.jsp'
}),
reader: new Ext.data.JsonReader({
root: 'topics',
totalProperty: 'totalCount',
id: 'threadid'
},
[
{name: 'title'},
{name: 'postid'},
{name: 'username'},
{name: 'lastpost'},
{name: 'excerpt'},
{name: 'userid'},
{name: 'dateline'},
{name: 'forumtitle'},
{name: 'forumid'},
{name: 'replycount'},
{name: 'lastposter'}
]),
baseParams: {
abc: 123
}
});
var pluginExpanded = true;
var grid = Ext.create('Ext.grid.Panel', {
width: 700,
height: 500,
title: 'ExtJS.com - Browse Forums',
store: store,
disableSelection: true,
loadMask: true,
// grid columns
columns:[{
id: 'topic',
text: "Topic",
dataIndex: 'title',
flex: 1,
sortable: false
},{
text: "Author",
dataIndex: 'username',
width: 100,
hidden: true,
sortable: true
},{
text: "Replies",
dataIndex: 'replycount',
width: 70,
align: 'right',
sortable: true
},{
id: 'last',
text: "Last Post",
dataIndex: 'lastpost',
width: 150,
sortable: true
}],
// paging bar on the bottom
bbar: Ext.create('Ext.PagingToolbar', {
store: store,
displayInfo: true,
displayMsg: 'Displaying topics {0} - {1} of {2}',
emptyMsg: "No topics to display"
}),
renderTo: 'topic-grid'
});
// trigger the data store load
store.loadPage(1);
});
</script>
jsp, data.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%
String data = "{\"totalCount\":\"1\",\"topics\":[{\"title\":\"XTemplate with in EditorGridPanel\",\"threadid\":\"133690\",\"username\":\"kpr#emco\",\"userid\":\"272497\",\"dateline\":\"1305604761\",\"postid\":\"602876\",\"forumtitle\":\"Ext 3.x: Help\",\"forumid\":\"40\",\"replycount\":\"2\",\"lastpost\":\"1305857807\",\"lastposter\":\"kpr#emco\",\"excerpt\":\"Hi\"}]}";
out.println(data);
System.out.println(data);
%>
None of your json column titles seem to match up with your ExtJS model.
The model needs to have the same column names as the data so that it knows where to put the data.
UPDATE
I'm confounded by your store data model implementation. I've never seen it implemented as the second argument for a new JsonReader.
But because you are using baseParam config I assume that you are using ExtJS 3.x not 4.x and I am admittedly not very familiar with 3.x so that may be a legal data model implementation.
In ExtJS 4.x the data model is usually implemented as a separate object than the store, something like this:
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'title', type: 'string'},
{name: 'postid', type: 'int'},
{name: 'username', type: 'string'},
{name: 'lastpost', type: 'int'},
{name: 'excerpt', type: 'string'},
{name: 'userid', type: 'int'},
{name: 'dateline', type: 'int'},
{name: 'forumtitle', type: 'string'},
{name: 'forumid', type: 'int'},
{name: 'replycount', type: 'int'},
{name: 'lastposter', type: 'string'}
]
});
var store = Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : 'data.jsp',
reader: {
type: 'json',
root: 'topics',
totalProperty: 'totalCount',
idProperty: 'threadid'
}
}
});
I am not sure how this works in other versions of ExtJS if you are actually using 3.x, you will have to check your own ExtJS documentation. Also as suggested, you should be seeing an error message in your browser's error console. That will tell you exactly what the problem is.

ExtJS 4 tree panel items visible in Firefox but not in Chromium

I was creating Tree Panel similar to TreeGrid example with drag'n'drop. The only problem is that items are correctly shown in tree panel in Firefox browser whereas in Chromium tree grid is empty. How's that possible?
JSON data sent to server:
{"text":".","children": [
{
"id":null,
"name":"en",
"visible":false,
"expanded":true,
"leaf":false,
"children":{
"id":5,
"name":"/",
"visible":false,
"expanded":true,
"leaf":true,
"children":[]
}
}]
}
Model
Ext.define('Example.model.WebTreeItem', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [
{name: 'id', type: 'int', defaultValue: 0},
{name: 'visible', type: 'boolean' },
{name: 'name', type: 'string' }
]
});
Store
Ext.define('Example.store.WebTreeItems', {
extend: 'Ext.data.TreeStore',
model: 'Example.model.WebTreeItem',
autoLoad: true,
proxy: {
type: 'ajax',
api: {
read : 'getlist.json'
},
reader: {
type: 'json'
}
}
});
View
Ext.define('Example.view.webitem.Tree', {
extend: 'Ext.tree.Panel',
alias : 'widget.webtreeitem',
title : 'Web items',
store: 'WebTreeItems',
rootVisible: false,
multiSelect: true,
singleExpand: false,
collapsible: true,
selModel: Ext.create('Ext.selection.CheckboxModel'),
height: 800,
renderTo: 'webstructure-tree',
columns: [{
xtype: 'treecolumn',
text: 'Name',
flex: 2,
sortable: true,
dataIndex: 'name'
},{
xtype: 'booleancolumn',
text: 'Visible',
flex: 1,
dataIndex: 'visible',
sortable: false
}],
viewConfig: {
plugins: {
ptype: 'treeviewdragdrop'
}
}]
});
Dependencies are loaded automatically using
Ext.Loader.setConfig({enabled:true});
Ext.application({
...
});
Any suggestion will be highly appreciated.
Well I thought that I was sending aforementioned JSON, but in fact I was sending something like this (quoted response with escaped quotes) and Chromium couldn't read it correctly
"{\"text\":\".\",\"children\": [
{
\"id\":null,
\"name\":\"en\",
\"visible\":false,
\"expanded\":true,
\"leaf\":false,
\"children\":{
\"id\":5,
\"name\":\"/\",
\"visible\":false,
\"expanded\":true,
\"leaf\":true,
\"children\":[]
}
}]
}"

Categories

Resources