Extjs load combobox from store - extjs

i want to load my Combobox in a Panel from Store.
var colors = new Object();
Ext.onReady(function(){
colors = Ext.create('Ext.data.Store', {
fields: ['id', 'name'],
autoLoad: true,
proxy: {
type: 'ajax',
url: 'app/view/main/loadFromDatabase.php',
reader: {
type: 'json',
rootProperty: 'name'
}
},
});
The colors i want load a little bit later in my Panel like this:
{ xtype: 'combo',
fieldLabel: 'Farbe',
name: 'farbe',
store: colors ,
queryMode: 'local',
displayField: 'name',
valueField: 'id' }
The response of my ajax request of loadFromDatabase.php is:
[
{
"id": "1",
"name": "Red"
},
{
"id": "2",
"name": "Blue"
},
{
"id": "3",
"name": "Green"}]
this seems like a valid json.
But when i click on the combobox the box is empty. Whats wrong?

Add these to your combo:
listeners : {
added : function(tis) {
tis.getStore().load();
}
}
and add these to your store:
listeners : {
load : function() {
Ext.getCmp('yourcomboid').setValue(Ext.getCmp('yourcomboid').getValue());
}
}
at last, to avoid second loading, add the following to the combo:
mode : 'local'
full code :
{
xtype : 'combo',
fieldLabel : 'Role Selection',
id : 'role',
hiddenName : 'role',
hiddenValue : 1,
editable : false,
emptyText : 'Please select a role',
typeAhead : true,
triggerAction : 'all',
lazyRender : true,
mode : 'local',
listeners : {
added : function(
tis) {
tis.getStore().load();
}
},
store : new Ext.data.JsonStore(
{
url : 'getRole',
listeners : {
load : function() {
Ext.getCmp('role').setValue(Ext.getCmp('role').getValue());
}
},
fields : ['id','name' ],
root : 'resultList',
}),
displayField : 'name',
valueField : 'id'
}

Your json should look like the below
'items': [{
'name': 'Lisa',
"email": "lisa#simpsons.com",
"phone": "555-111-1224"
}, {
'name': 'Bart',
"email": "bart#simpsons.com",
"phone": "555-222-1234"
}, {
'name': 'Homer',
"email": "home#simpsons.com",
"phone": "555-222-1244"
}]
Then you can specify your store as
var myStore = Ext.create('Ext.data.Store', {
fields:['name','email','phone'],
proxy: {
type: 'ajax',
url: '/yourpath.json',
reader: {
type: 'json',
root: 'items'
}
},
autoLoad: true
});
So you have to make your json as key with array of objects and specify that key in the root property in reader config.There is also no rootProperty property for a normal store.Remove that as well.
Hope this helps you.

i have found an other solution
i donĀ“t know if this solution is better then the other, but it works :)
var colorsFromDB = [];
var colors = Ext.create('Ext.data.Store', {
id: "colors",
fields: ['id', 'name'],
data : colorsFromDB
});
Ext.Ajax.request({
url: "app/view/main/loadFromDatabase.php",
method: 'POST',
success: function( r ) {
var res = Ext.decode(r.responseText);
if (res !== null) {
Ext.each(res.colors, function(obj) {
colorsFromDB.push({
id: obj.id,
name: obj.name
})
});
colors.loadData(colorsFromDB);
}
},
failure: function( r ) {
console.log(r.responseText);
}
});

Related

ExtJS Loading Link List Tree Using Ajax

I have the below code that I am trying to get the list of dates using Ajax and display those on the page as links to elsewhere. So each entry would be a link that when you click would take you elsewhere. Though the treelist is not loading any items...
Data
{
"success": true,
"data": [
"2018-10-08T00:00:00",
"2018-10-05T00:00:00",
"2018-10-04T00:00:00",
"2018-10-03T00:00:00",
]
}
Code
Ext.define('...', {
extend: 'Ext.form.Panel',
xtype: '...',
requires: [
'...'
],
layout: 'border',
items: [{
xtype: 'container',
store: {
proxy: {
type: 'ajax',
url: '...',
useDefaultXhrHeader: true,
withCredentials: true,
reader: {
type: 'json',
rootProperty: 'data'
},
}
}
}]
});
You can display a grid with your data. It can show clickable links.
To do this you need to make a grid with your ajax store and a grid renderer like this:
// Ajax store
Ext.create('Ext.data.Store', {
storeId: 'mystore',
autoLoad: true,
proxy: {
type : 'ajax',
url : '/file.php',
actionMethods: {
read: 'POST'
},
reader : {
type: 'json'
},
extraParams : {
key : 'val'
},
fields : [
{name: 'date', type: 'string'}
]
}
})
// Grid
Ext.create('Ext.grid.Panel', {
renderTo: Ext.getBody(),
scrollable: true,
store: {
type: 'mystore'
},
columns: [
{
text: 'link column',
dataIndex:'link',
renderer: function(value) {
if(value) {
// here you can format your output
return ''+value+'';
}
}
}
]
})
Please look whole example in a Fiddle

How to load the values in combobox from database

How do I populate combo box values instead of hard coding from the database into the store of combo box
{
xtype: 'fieldset',
title: 'Dress Types',
items: [
{
xtype: 'combobox',
displayField: "displayName",
valueField: "displayName",
emptyText: 'Select Type',
store: {
fields: ["id", "displayName"],
data: [
{ "id": "1", "displayName": "Kurtha" },
{ "id": "2", "displayName": "Denim Top" },
{ "id": "3", "displayName": "Western T shirt" },
{ "id": "4", "displayName": "Sleeveless" }
]
},
name: 'dresses',
margin: '15px',
allowBlank: false,
forceSelection: true,
}
]
}
Thanks in advance
There are ways by which you can get this. You need to create one store in your js from your input data and then assign it to the comboBox.
Given example
var combstore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: [{
name: 'value',
mapping: "ITEMID",
type: 'string'
}, {
name: 'name',
mapping: "TITLE",
type: 'string'
}],
proxy: new Ext.data.HttpProxy({
type: 'ajax',
actionMethods: {
read: "POST"
},
url: Your URL,
headers: {
'Accept': 'application/json; charset=utf-8'
},
reader: {
type: 'json',
rootProperty: 'data'
},
autoLoad: true
})
});
Now in your comboBox you can call this combstore to your store.
store :combstore
In the variable combostore we are creating one data store by using Ext.data.Store and placing values in field. Then in proxy method calling url and mapping the values from field.
Read the doc for better understanding Doc
Check the below code.
Ext.create('Ext.form.ComboBox', {
valueField: "displayName",
emptyText: 'Select Type',
store: Ext.create('Ext.data.Store', {
fields: ["id", "displayName"],
proxy: {
url: 'data1.json',
reader: {
type: 'json',
rootProperty: 'data'
}
},
autoLoad: true
}),
name: 'dresses',
margin: '15px',
allowBlank: false,
forceSelection: true,
renderTo: Ext.getBody()
});
I assumed my service returing data like below
{
"data": [{
"id": "1",
"displayName": "Kurtha"
}, {
"id": "2",
"displayName": "Denim Top"
}, {
"id": "3",
"displayName": "Western T shirt"
}, {
"id": "4",
"displayName": "Sleeveless"
}]
}

Extjs Grid with pagination not loading on loadData

I am trying to load data in the extjs grid with paging toolbar using loaddata but it shows empty grid.
The data returned is
{
"data": [{
"id": "user1",
"title": "index0"
}, {
"id": "user2",
"title": "index1"
}, {
"id": "user3",
"title": "index2"
}, {
"id": "user4",
"title": "index3"
}],
"total": 8,
}
My code looks like
initcomponent:function(){
defaultModel:[{
header: 'Date',
dataIndex: 'id',
sortable: true
},
{
header: 'title',
dataIndex: 'title',
sortable: false,
}];
}
this.currStore = Ext.create('Ext.data.Store', {
fields : ['id','title'],
pageSize:4,
autoLoad:false,
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'data',
totalProperty:'total',
}
}
});
this.currStore.load({
params:{
start:0,
limit: 4,
}
});
var config =
{
columns : this.defaultModel,
store : this.currStore,
columnLines : true,
loadMask : true,
autoScroll : true,
tbar : [{
xtype : 'button',
text : 'Refresh',
iconCls : 'btn-refresh',
scope : this,
handler : this.refreshGridData
}],
dockedItems: [{
xtype: 'pagingtoolbar',
store: this.currStore, // same store GridPanel is using
dock: 'bottom',
id:'pagingErrNot1',
pageSize:4,
displayInfo: true,
emptyMsg: "No notification to display",
listeners:{
beforechange : {
scope : this,
fn : function(pagingtoolbar,pageNumber,scope)
{
}
}
}
}],
};
Ext.apply(this,config);
this.callParent(arguments);
}
data is of the form given above and setdata is called after the data is returned from server side.
setData:function(data)
{
this.getStore().loadData(data);
this.getView().refresh();
}
but after loadData, grid is empty and and pagingtoolbar shows disabled.WHat am i doing wrong?

How to create Extjs Models and its association with other models

Hello Everyone,
I am new to EXTJS and want to grasp Extjs models and its complexity as per the json response. For Example I have a sample Json response for which I want to create a model
This is my app.js file
Ext.application({
requires: ['Ext.container.Viewport'],
name: 'AM',
appFolder: 'app',
launch: function() {
console.log('I am in app');
var store = Ext.create('AM.store.ExampleStore');
store.load();
console.log('After Loading the records');
}
});
This is json sample
[
{
"page": "0",
"data": [
{
"firstname": "puneet",
"id": "28170",
"lastname": "srivastava",
"area": "bangalore",
"profession": "Engineer"
},
{
"firstname": "v Durga",
"id": "28171",
"lastname": "Mallikrjuna",
"area": "bangalore",
"profession": "Extjs developer"
}
]
}
]
I created models as per my understanding. Model belongs to data part of json
Ext.define('AM.model.Example', {
extend: 'Ext.data.Model',
fields: ["firstname", "id", "lastname", "area", "profession"],
belongsTo : 'AM.model.Page'
});
Ext.define('AM.model.Page', {
extend: 'Ext.data.Model',
fields: ["page", "data"],
hasMany:{ model:"AM.model.Example", name: "example", associationKey : "data" }
});
Ext.define('AM.store.ExampleStore', {
extend: 'Ext.data.Store',
model : "AM.model.Page",
proxy: {
type: 'ajax',
url: 'example.json',
reader: {
type: 'json',
root: 'spaces'
}
},
listeners : {
'load' : function(store, records, successful) {
console.log('Loading the records');
console.log(store.data);
}
}
})
The data which I am getting after logging the store that contains only {page : 0} and the data part is missing.
Just want to know Where I am making the mistake or Any thing I am not including in the store or models.
Thanks
The Model should be:
Ext.define('AM.model.Example', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstname', type: 'string'},
{name: 'id', type: 'int'},
...
});
in your case, root should be data. Root means master node of your Json object.
Ext.define('AM.store.ExampleStore', {
extend: 'Ext.data.Store',
model : "AM.model.Page",
proxy: {
type: 'ajax',
url: 'example.json',
reader: {
type: 'json',
root: 'data'
}
},
listeners : {
'load' : function(store, records, successful) {
console.log('Loading the records');
console.log(store.data);
}
}
})

Howto obtain child item data with hasMany relation in extjs4 grid selectionchange event

I have a storage with models:
Ext.define('App.Supplier.Store', {
extend : 'Ext.data.Store',
constructor : function(config) {
Ext.regModel('Supplier', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'irn', type: 'string'}
],
hasMany : {model: 'SupplierContact', name: 'contacts', associationKey: 'contacts'}
});
Ext.regModel('SupplierContact', {
fields: [
{name: 'id', type: 'int'},
{name: 'email', type: 'string'},
{name: 'phone', type: 'string'},
{name: 'name', type: 'string'}
],
belongsTo: 'Supplier'
});
config = config || {};
config.model = 'Supplier';
config.proxy = {
type : 'ajax',
url : '/supplier/search/process',
reader : {
type : 'json',
root : 'data',
totalProperty : 'totalCount',
successProperty: 'success'
}
};
config.pageSize = 10;
config.remoteSort = true;
config.simpleSortMode = true;
// call the superclass's constructor
App.Supplier.Store.superclass.constructor.call(this, config);
}
});
I have a valid json from url and this code works fine:
var store = new App.Supplier.Store({storeId: 'supplierStore'});
store.load({
callback: function() {
var supplier = store.first();
console.log("Order ID: " + supplier.getId() + ", which contains items:");
supplier.contacts().each(function(contact) {
alert(contact.data.phone);
});
}
});
My grid:
Ext.define('App.Supplier.Grid', {
extend : 'Ext.grid.GridPanel',
alias : 'widget.supplierGrid',
cls : 'supplier-grid',
iconCls: 'icon-grid',
collapsible: true,
animCollapse: false,
title: 'Expander Rows in a Collapsible Grid',
height: 300,
buttonAlign:'center',
headers : [
{text : 'Id', dataIndex : 'id', width : 20},
{text : 'Name', dataIndex : 'name', flex : 4 },
{text : 'IRN', dataIndex : 'irn', flex : 3}
],
initComponent : function() {
this.store = new App.Supplier.Store({storeId: 'supplierStore'});
this.store.load();
this.callParent(arguments);
this.on('selectionchange', this.onRowSelect, this);
},
onRowSelect: function(sm, rs) {
if (rs.length) {
alert(sm.contacts); // undefined
alert(rm.contacts); // undefined
var info = this.getComponent('infoPanel');
info.updateDetail(rs[0].data);
}
}
});
How to get contacts in onRowSelect for selected row ?
PS: json from server:
{"totalCount":100,
"success":true,
"data":[{
"id":2,
"name":"department 0",
"irn":"123490345907346123-0",
"contacts":[{
"id":3,
"phone":"+7907 123 12 23",
"email":"test#localhost",
"name":"hh"
}, {
"id":4,
"phone":"+7832 123 12 23",
"email":"test2#localhost",
"name":"gtftyf"
}]
}]}
Can you provide your json as well? I think your json is not correct so that, ExtJS loads the relationships as well. In order to load the relationships as well, you will need to provide the contacts details in the returned json as well..
You should have something like this:
sucess: true,
totalCount: 10,
data: [{
id: 142,
name: 'xyz',
irn: 'test',
contacts: [{
id: 130,
email: 'xyz#site.com',
phone: 123445,
name: 'Supplier XYZ'
},{
id: 131,
email: 'test#site.com',
phone: 123445,
name: 'Supplier XYZ'
}]
}, ...
]
Update:
Json is correct! The problem lies with the way you access your data. If you look at the signature of selectionchange event, you will notice that the first is DataView and second is an array of selected records. So, in your case the rs is an array of the selected rows. You should be able to access it as rs[0].contacts.
Another way to access the selected records will be to use the DataView object. You can use the getSelectedRecords method to get the array of the selected records.

Resources