Related
Java application sends to the front the following json data:
{"data":[{"id":1,"name":"","password":"xxxxxxxxxxxxxxxxxxx","roles":[{"id":1,"name":"Administrator"}],"username":"admin"}]}
In the front I have an user model like the following:
Ext.define('App.store.Users', {
extend: 'Ext.data.Store',
fields: [
{name: 'id', type: 'auto'},
{name: 'name', type: 'auto'},
{name: 'password', type: 'auto'},
{name: 'roles', type: 'auto'},
{name: 'username', type: 'auto'},
],
autoLoad: true,
proxy: {
type: 'ajax',
url: '/web/user',
reader: {
type: 'json',
root: 'data'
}
}
});
Edit:
I updated the code and this way the data is loaded.
Also i made a grid so I can show the results.
Ext.define('App.view.Home', {
extend: 'Ext.panel.Panel',
alias: 'widget.home',
title: 'Home',
layout: 'fit',
items: [
{
xtype: 'gridpanel',
store: 'Users',
title: 'Users grid',
columns: [
{text: 'Id', dataIndex: 'id' },
{text: 'Username', dataIndex: 'username', width : 200 },
{text: 'Role', dataIndex: 'roles', width : 200 },
{text: 'Name', dataIndex: 'name', width : 200 },
]
}
]
});
Now the question that remains is that the grid is showing [object Object] how could i be showing the part that i want from that object, as the name of the role
You need to change the reader type to JSON, This code is working for me:
Fiddle
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.define('Users', {
extend: 'Ext.data.Store',
fields: ['id', {
name: 'username',
type: 'string'
}, {
name: 'name',
type: 'string'
}],
autoLoad: true,
proxy: {
type: 'ajax',
url: 'data1.json',
reader: {
type: 'json',
rootProperty: 'data'
}
}
});
Ext.create("Users", {
listeners: {
load: function() {
console.log("Loaded " + this.getCount() + " records");
}
}
});
}
});
I also removed the mappings as I don't think that you need them.
EDIT
In regards to the data showing in a grid, the 'roles' property in the JSON data is an array, that's why it's showing in the grid as object, I've updated the fiddle to show a possible way to get this information, But it's not the recommended method. You should probably look at associations in ExtJs for this.
Reviewing the guide on the Data Package may also help with this.
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.define('Users', {
extend: 'Ext.data.Store',
fields: [{
name: 'id',
type: 'int'
}, {
name: 'username',
type: 'string'
}, {
name: 'name',
type: 'string'
}, {
name: 'roles',
type: 'auto'
}],
autoLoad: true,
proxy: {
type: 'ajax',
url: 'data1.json',
reader: {
type: 'json',
root: 'data'
}
},
listeners: {
load: function(store, records) {
console.log("Loaded " + this.getCount() + " records");
}
}
});
var users = Ext.create("Users");
Ext.define('App.view.Home', {
extend: 'Ext.panel.Panel',
alias: 'widget.home',
title: 'Home',
layout: 'fit',
items: [{
xtype: 'gridpanel',
store: users,
title: 'Users grid',
columns: [{
text: 'Id',
dataIndex: 'id'
}, {
text: 'Username',
dataIndex: 'username',
width: 200
}, {
text: 'Role',
dataIndex: 'roles',
width: 200,
renderer: function(v, metadata) {
return v[0].name;
}
}, {
text: 'Name',
dataIndex: 'name',
width: 200
}]
}]
});
Ext.create('App.view.Home', {
renderTo: Ext.getBody()
});
}
});
first I thought it is a simple problem however I could not solve it anyway.
I have a extjs gridpanel, its store and model. From controller, I can insert new records to store, when I use firebug and debug, I can list all the new records in the store (panel.store.data.items) however in the gridview I cannot make it visible.
Could you please tell me where and what I am missing? Why the records are not listed in the grid?
This is my model
Ext.define('BOM.model.PaketModel', {
extend: 'Ext.data.Model',
fields: [
{ name: 'seriNo', type: 'string' },
{ name: 'tutar', type: 'string' },
]
});
This is store
Ext.define('BOM.store.PaketStore', {
extend: 'Ext.data.Store',
model: 'BOM.model.PaketModel',
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'data',
},
writer: {
type: 'json',
root: 'data',
},
},
});
This is the method I add new rows
addNew: function () {
this.getPaketWindow().returnRowEdit().cancelEdit();
this.getPaketWindow().getStore().insert(0, new BOM.model.PaketModel());
this.getPaketWindow().returnRowEdit().startEdit(0, 0);
}
UPDATE VIEW
Ext.define('BOM.view.PaketCreate', {
extend: 'Ext.grid.Panel',
alias: 'widget.paketcreate',
bodyPadding: 5,
layout: 'fit',
header:false,
initComponent: function () {
this.columns = [
{ text: 'Seri No', flex: 2, sortable: true, dataIndex: 'seriNo', field: {xtype: 'textfield'} },
{ text: 'Tutar', flex: 2, sortable: true, dataIndex: 'tutar', field: {xtype: 'textfield'} }
];
this.dockedItems = [{
xtype: 'toolbar',
items: [{
text: 'Ekle',
id:'addNewCheck',
iconCls: 'icon-add',
},'-',{
id: 'deleteCheck',
text: 'Sil',
iconCls: 'icon-delete',
disabled: true,
}]
}];
this.store = 'BOM.store.PaketStore';
rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
autoCancel: false
});
this.plugins = rowEditing,
this.callParent(arguments);
},
returnRowEdit: function () {
console.log("row editing...");
return rowEditing;
}
});
var rowEditing;
Try:
this.store = Ext.create('BOM.store.PaketStore');
instead of:
this.store = 'BOM.store.PaketStore';
http://jsfiddle.net/qzMb7/1/
It works when I add ".getView()" like
this.getPaketWindow().getView().getStore().insert(0, new BOM.model.PaketModel())
However, I still do not understand. Both reaches the same store when I add records manually I can see them in the store.data but it is visible only if I include .getView() part
I am working with the mvc nested-loading example: http://docs.sencha.com/ext-js/4-1/#!/example/app/nested-loading/nested-loading.html. While trying to build my own project out of this I ran in to a problem trying to render the data in my content view. For some reason the templated data is not loading. I have a feeling it's something to do with xtype: 'component'. I changed it to panel and I seen the border but still no data. I also included the app.jsb3 file and that does not work either. I can see the data listed in the bind method but gets added.
My json data:
[
{id: 1, name: 'This is page one', url: 'http://my.test.com/test.html'},
{id: 2, name: 'This is page two', url: 'http://my.test.com/test2.html'}
]
My Model:
Ext.define('TST.model.Projects', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'url']
});
My Store:
Ext.define('TST.store.Projects', {
extend: 'Ext.data.Store',
model: 'RLA.model.Projects',
autoLoad: true,
proxy: {
type: 'ajax',
url : 'resources/json/projects.json'
}
});
My Controller:
Ext.define('TST.controller.Menu', {
extend: 'Ext.app.Controller',
stores: ['Projects'],
models: ['Projects'],
refs: [
{ref: 'mainLeftMenu', selector: 'mainleftmenu'},
{ref: 'mainContent', selector: 'maincontent'}
],
init: function() {
this.control({
'mainleftmenu': { selectionchange: this.onLeftMenuChange },
});
this.getProjectsStore().on({
scope: this,
load : this.onProjectsStoreLoad
});
},
onProjectsStoreLoad: function(store, records) {
Ext.defer(function() {
if (records.length) {
var record = records[0];
this.getMainLeftMenu().getSelectionModel().select(record);
console.log("iamhere");
}
}, 500, this);
},
/* load the project menu items */
onLaunch: function() {
this.getMainLeftMenu().bindStore(this.getProjectsStore());
},
/* update content on new selection */
onLeftMenuChange: function (view, records) {
if (records.length) {
this.showContent(records[0]);
}
},
showContent: function(record) {
this.getMainContent().bind(record);
}
});
Here is my leftContent view:
Ext.define('TST.view.main.LeftMenu', {
extend: 'Ext.view.View',
alias: 'widget.mainleftmenu',
initComponent: function() {
Ext.apply(this, {
id: 'leftmenu',
dock: 'left',
width: 200,
border: true,
cls: 'leftMenu',
selModel: {
deselectOnContainerClick: false
},
itemSelector: '.items',
tpl: [
'<div class="menuTitle">Projects</div>',
'<tpl for=".">',
'<div class="items">{name}</div>',
'</tpl>'
]
});
this.callParent(arguments);
}
});
Here is my content view:
Ext.define('TST.view.main.Content', {
extend: 'Ext.panel.Panel',
alias: 'widget.maincontent',
initComponent: function() {
Ext.apply(this, {
cls: 'content',
flex: 2,
border: false,
autoScroll: true,
layout: {
type: 'hbox',
align: 'middle',
pack: 'center',
availableSpaceOffset: Ext.getScrollbarSize().width
},
items: [
{
xtype: 'component', // think it has something to do with this?
itemId: 'contentCt',
width: 500,
height: 200,
border: 2,
tpl: [
'<div class="url">{url}</div>'
]
}]
});
this.callParent(arguments);
},
bind: function(record) {
this.child('#contentCt').update(record.getData());
}
});
And firebug shows that record.getData() returns:
object { id=1, name="This is page one", url="http://my.test.com/test.html"}
You will need to post more code, since this works without issue:
Ext.define('TST.view.main.Content', {
extend: 'Ext.panel.Panel',
alias: 'widget.maincontent',
initComponent: function() {
Ext.apply(this, {
cls: 'content',
flex: 2,
border: false,
autoScroll: true,
layout: {
type: 'hbox',
align: 'middle',
pack: 'center',
availableSpaceOffset: Ext.getScrollbarSize().width
},
items: [{
xtype: 'component', // think it has something to do with this?
itemId: 'contentCt',
width: 500,
height: 200,
border: 2,
tpl: ['<div class="url">{url}</div>']
}]
});
this.callParent(arguments);
},
bind: function(data) {
this.child('#contentCt').update(data);
}
});
Ext.onReady(function(){
var p = new TST.view.main.Content({
width: 500,
height: 200,
renderTo: document.body
});
p.bind({
url: 'foo'
});
});
I am new to EXTjs(and to Stackoverflow as well). I was struggling with this issue and at last decided to ask for help.My question is " How to sync combobox and textfield values. i.e how to load different values from 'store' on textfield,while I am changing values on combobox? " My problem is that while I load values on combobox and I select them my textfield stays empty all the time. I tried "Ext.getCmp().setValue();" it works fine with this ,but I think it is not the best option if I'd have 100 textfields. I want combobox and textfield to be synched with store somehow. Any help is appreciated. Situation in pictures are in links below :
pic one
pic two
And my code :
My app.js :
Ext.Loader.setConfig({
enabled: true
});
Ext.application({
name: 'work',
appFolder: 'app',
controllers: ['Work'],
launch: function() {
Ext.create('Ext.container.Viewport', {
//layout: 'fit',
items: [{
xtype: 'rightpanel' // gets it from view class
}
]
});
}
});
My view RightPanel :
Ext.define('work.view.works.RightPanel', {
extend: 'Ext.panel.Panel',
ALIAS: 'widget.rightpanel',
width: 300,
title: 'Building navigation',
animCollapse: true,
collapsible: true,
split: true,
minSize: 400,
maxSize: 400,
margins: '0 5 0 0',
//activeTab:1, tabPosition:'bottom',
initComponent: function() {
this.items = [{
xtype: 'form',
items: [{
xtype: 'combobox',
fieldLabel: 'BuildingID',
queryMode: 'local',
name: 'Bid',
displayField: 'Bid',
valueField: 'Bid',
id: 'Bid',
MODE: 'remote',
store: 'Work'
}, {
xtype: 'textfield',
fieldLabel: 'Address',
name: 'Address',
displayField: 'Address',
valueField: 'Address',
id: 'Address',
store: 'Work'
}]
}];
this.columns = [{
header: 'ID',
dataIndex: 'Bid',
flex: 1
}, {
header: 'Texts',
dataIndex: 'Address',
flex: 1
}];
this.callParent(arguments);
}
});
My store :
Ext.define('work.store.Work', {
extend: 'Ext.data.Store',
model: 'work.model.Work',
storeId: 'workstore',
id: 'workstore',
autoLoad: true,
proxy: {
type: 'ajax',
limitParam: undefined,
startParam: undefined,
paramName: undefined,
pageParam: undefined,
noCache: false,
api: {
read: 'data/showWork.php' // just a .php file that reads values from database and shows them on combobox
},
reader: {
type: 'json',
root: 'data',
successProperty: 'success'
},
writer: {
type: 'json',
root: 'data',
encode: true
}
}
});
My Model class :
Ext.define('work.model.Work', {
extend: 'Ext.data.Model',
//idProperty: 'WorkID',
fields: [{
name: 'Bid',
type: 'int'
}, 'Address']
});
My Controller :
Ext.define('work.controller.Work', {
extend: 'Ext.app.Controller',
stores: ['Work'],
models: ['Work'],
views: [
'works.RightPanel' // name comes from view class
],
init: function() {
console.log('Done');
this.control({
'viewport > rightpanel': {
render: this.test
},
'rightpanel combobox[id=Bid]': {
select: this.change
}
});
},
change: function(buttons) {
var values = this.getStore('Work').collect('Address', 'Address', false);
var win = buttons.up('rightpanel'); // gets the needed widget
var form = win.down('combobox'); // gets the needed form
var value = form.getValue(); // gets the value
console.log("value " + value);
}
});
You want to watch for a change on the combobox, and then action a change based off its new value.
this.items = [{
xtype: 'form',
items: [{
xtype: 'combobox',
fieldLabel: 'BuildingID',
queryMode: 'local',
name: 'Bid',
displayField: 'Bid',
valueField: 'Bid',
id: 'Bid',
mode: 'remote',
store: 'Work'
listeners::{
change:function(cbx, newVal,oldVal,e){
var record = cbx.getStore().find("Bid",newVal); // theres prolly a better way to find this, such as to find the active record of the cbx
Ext.getCmp('Address').setValue(record.getValue("Address"))
}
}
},{
xtype: 'textfield',
fieldLabel: 'Address',
name: 'Address',
displayField: 'Address',
valueField: 'Address',
id: 'Address',
store: 'Work'
}]
I am using Ext.js 4 with MVC. I have a window which pops up, and asks the user for some criteria. That window calls a servlet, which returns the data as JSON, and should then populate a grid. I can see thru Firefly that the JSON object IS be returned. However, the grid is not being populated. The reason is that a subsequent call to the servlet is beign made. This is because I have the URL specified in two places. I know this is wrong, but if either one is omitted I get error messages.
This is app/controller/PSLocators.js
Ext.define('MyApp.controller.PSLocators', {
extend: 'Ext.app.Controller',
stores: [ 'PSLocators' ],
models: [ 'PSLocator' ],
views : [ 'pslocator.List' ]
});
This is app/model/PSLocator.js
Ext.define('MyApp.model.PSLocator',
{
extend: 'Ext.data.Model',
fields:
[
'id',
'name',
'address',
'city',
'state',
'zip',
]
});
This is app/store/PSLocators.js. This has the first of the urls. This is the one that returns no data. I don't think I should have the proxy{} here, but if I delete the proxy { } I get the error message
uncaught exception: [Exception... "'You are using a ServerProxy but have not supplied it with a url.' when calling method: [nsIDOMEventListener::handleEvent]"
nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)"
location: "JS frame :: chrome://firebug/content/net/spy.js :: callPageHandler :: line 812" data: no]"
Ext.define('MyApp.store.PSLocators', {
extend: 'Ext.data.Store',
model: 'MyApp.model.PSLocator',
autoLoad: false, // see also activate() in grid panel
sortOnLoad: false, // sorted by SAS
proxy:
{
type: 'ajax',
url: MyGlobalData.contextPath + '/PSLocator',
reader:
{
type: 'json',
root: 'data', // the name of the array within the JSON dataset
totalProperty: 'results',
successProperty: 'success'
}
}
});
This is the app/view/pslocator/List.js. This has the second of the urls. This url is returning the data correctly as JSON. If I delete the url I get the error message "uncaught exception: No URL specified"
Ext.define('MyApp.view.pslocator.List' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.pslocatorlist',
store : 'PSLocators',
title : 'Store Locator',
id : 'pslocator.List',
autoScroll: true,
height: 400,
columnLines: true,
initComponent: function()
{
this.columns = [
{header: 'ID' , dataIndex: 'id' , flex: .05 , align: 'center' },
{header: 'Name' , dataIndex: 'name' , flex: .20 , align: 'left' },
{header: 'Address' , dataIndex: 'address' , flex: .20 , align: 'left' },
{header: 'City' , dataIndex: 'city' , flex: .10 , align: 'left' },
{header: 'State' , dataIndex: 'state' , flex: .05 , align: 'center' },
{header: 'Zip' , dataIndex: 'zip' , flex: .05 , align: 'center' }
];
this.callParent(arguments);
},
listeners:
{
activate: function()
{
this.un('activate', arguments.callee);
var win = new Ext.Window(
{
id: 'id-pslocator-window',
title: 'Show locations near which store?',
items: [
{
xtype : 'form',
id : 'id-pslocator-form',
bodyPadding: 5,
width : 500,
height : 125,
autoScroll : false,
// The form will submit an AJAX request to this URL when submitted
url: MyGlobalData.contextPath + '/PSLocator',
layout: 'auto',
defaults:
{
anchor: '100%'
},
items: [
{
xtype : 'textfield',
fieldLabel : 'Store number',
name : 'pStoreNumber',
labelWidth : 200,
width : 300, // includes labelWidth
allowBlank : false,
regex : /^([0-9]+)([ ]*)$/,
regexText : 'Must be a single unsigned integer.',
}
],
// Reset and Submit buttons
buttons: [
{
text: 'Reset',
handler: function()
{
this.up('form').getForm().reset();
}
},
{
text: 'Submit',
formBind: true, //only enabled once the form is valid
disabled: true,
handler: function()
{
var form = this.up('form').getForm();
if (form.isValid())
{
form.submit(
{
success: function(form, action)
{
console.log('In success function');
var myGrid = Ext.getCmp('id-pslocator-panel');
console.log('myGrid = ' + myGrid);
var myStore = myGrid.getStore();
console.log('myStore = ' + myStore);
myStore.load(); /* requires store be defined as above */
myGrid.getView().refresh();
var myPopup = Ext.getCmp('id-pslocator-window');
myPopup.destroy();
} // end success function
}); // end form submit
} // end if is valid
} // end handler
} // end submit
] // end buttons
}] // end items
}); // end win
win.show();
// this.store.load();
}
}
}); // Ext.define
Can someone please help, or point me to a working example (reminder: I am attempting the use the MVC architecture.)
You should store the proxy configuration in the model rather than the store in ExtJS4. Also It looks like you are just wanting to limit the search on your stores to a particular store number. You shouldn't need to submit a POST request via the form to do this. You should add a filter to your store so the server can return the correct data.
DISCLAIMER: I've not tested this code out, but it should be enough to get you going.
app/model/PSLocator.js
Ext.define('MyApp.model.PSLocator',
{
extend: 'Ext.data.Model',
fields:
[
'id',
'name',
'address',
'city',
'state',
'zip',
],
proxy:
{
type: 'ajax',
url: MyGlobalData.contextPath + '/PSLocator',
reader:
{
type: 'json',
root: 'data', // the name of the array within the JSON dataset
totalProperty: 'results',
successProperty: 'success'
}
}
});
app/store/PSLocators.js
Ext.define('MyApp.store.PSLocators', {
extend: 'Ext.data.Store',
model: 'MyApp.model.PSLocator',
autoLoad: false, // see also activate() in grid panel
sortOnLoad: false, // sorted by SAS,
remoteFilter: true // Needed so filter changes will go to the server
});
app/view/pslocator/List.js
Ext.define('MyApp.view.pslocator.List' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.pslocatorlist',
store : 'PSLocators',
title : 'Store Locator',
id : 'pslocator.List',
autoScroll: true,
height: 400,
columnLines: true,
initComponent: function()
{
this.columns = [
{header: 'ID' , dataIndex: 'id' , flex: .05 , align: 'center' },
{header: 'Name' , dataIndex: 'name' , flex: .20 , align: 'left' },
{header: 'Address' , dataIndex: 'address' , flex: .20 , align: 'left' },
{header: 'City' , dataIndex: 'city' , flex: .10 , align: 'left' },
{header: 'State' , dataIndex: 'state' , flex: .05 , align: 'center' },
{header: 'Zip' , dataIndex: 'zip' , flex: .05 , align: 'center' }
];
this.callParent(arguments);
}
}); // Ext.define
app/view/pslocator/Window.js
Ext.define('MyApp.view.pslocator.Window', {
extend: 'Ext.Window',
alias: 'widget.storeselector',
id: 'id-pslocator-window',
title: 'Show locations near which store?',
items: [
{
xtype : 'form',
id : 'id-pslocator-form',
bodyPadding: 5,
width : 500,
height : 125,
autoScroll : false,
// The form will submit an AJAX request to this URL when submitted
url: MyGlobalData.contextPath + '/PSLocator',
layout: 'auto',
defaults:
{
anchor: '100%'
},
items: [
{
xtype : 'textfield',
fieldLabel : 'Store number',
name : 'pStoreNumber',
labelWidth : 200,
width : 300, // includes labelWidth
allowBlank : false,
regex : /^([0-9]+)([ ]*)$/,
regexText : 'Must be a single unsigned integer.',
}
],
// Reset and Submit buttons
buttons: [
{
text: 'Reset',
handler: function()
{
this.up('form').getForm().reset();
}
},
{
text: 'Submit',
formBind: true, //only enabled once the form is valid
disabled: true,
handler: function()
{
var form = this.up('form').getForm();
if (form.isValid())
{
this.fireEvent('storeselected', form.getValues().pStoreNumber);
this.destroy();
} // end if is valid
} // end handler
} // end submit
] // end buttons
}] // end items
});
app/controller/PSLocators.js
Ext.define('MyApp.controller.PSLocators', {
extend: 'Ext.app.Controller',
stores: [ 'PSLocators' ],
models: [ 'PSLocator' ],
views : [ 'pslocator.List' ],
init: function() {
this.control({
'pslocatorlist': {
activate: this.showStoreSelector
},
'storeselector': {
storeselected: this.updatePSLocatorStore
}
);
},
showStoreSelector: function()
{
var win = Ext.create('MyApp.view.pslocator.Window');
win.show();
},
updatePSLocatorStore: function(storeId) {
this.getPSLocationsStore().filter('id', storeId);
this.getPSLocationsStore().load(); // I can't remember if this step is needed.
}
});
I think thats about as best I can get it from reading over the code. It should give you an idea of how you can use the MVC technique to your advantage and hopefully get you on the right path.