Extjs 4 showing JSON data (on demand) to grid? - json

After URL has been reached, how to show this data to grid, autoLoad:true, only loads firstly defined URL, but how to "dynamically" show loaded JSON to grid?, Reload the data with newly called JSON?
buttons: [{
text: 'Load1',
handler:function(){
myStore.getProxy().url = 'app/kontaktGrid1.json';
myStore.load();
grid.getView().refresh();
}},{
text: 'Load2',
handler:function(){
myStore.getProxy().url = 'app/kontaktGrid2.json';
myStore.load();
grid.getView().refresh();
}}]
Ext.define('app.gridStore', {
extend: 'Ext.data.Model',
fields: [
'name', 'email', 'phone'
]
});
var myStore =Ext.create('Ext.data.Store', {
model: 'app.gridStore',
proxy: {
type: 'ajax',
url : '',
reader:{
type:'json'
}
},
autoLoad:false
});
--Grid in Border Layout Center--
items:[{
xtype:"grid",
id:"kontaktGrid",
store: myStore,
border: false,
columns: [
{header: 'name',sortable : false, dataIndex: 'name'},
{header: 'email',sortable : false, dataIndex: 'email'},
{header: 'phone',sortable : false, dataIndex: 'phone'}
]
}]
This isn't working, only loading from server no dispalying data.

first, why did you load your json like that ? even it's working... this is the simple way :
text: 'Load1',
handler:function(){
myStore.load({
scope : this,
url : 'app/kontaktGrid1.json'
});
//myStore.getProxy().url = 'app/kontaktGrid1.json';
//myStore.load();
//grid.getView().refresh();
}
from docs, the definition of method load is
Loads data into the Store via the configured proxy..
second, your probem is only loading from server no dispalying data..
it's mean no error with json, store, and the model...
i think your problem is in the grid panel..
try show us how did you create the grid
make sure your grid column is refer(dataIndex) to your json
var grid = Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{text : 'name', sortable : false, dataIndex:"name"},
{text : 'email', sortable : false, dataIndex:"email"},
{text : 'phone', sortable : false, dataIndex:"phone"}
],
//.....

Related

Extjs 5 XTemplate with JSON store data

I need help with my xtemplate and store with proxy. No matter what I am trying, I am stuck in this problem. The xtemplate is only showing data from a store without using a proxy.
Working store:
Ext.create('Ext.data.Store', {
storeId : 'viewStore',
model : 'dataview_model',
data : [
{statistic: '213213', description: 'Hallo'},
{statistic: '534345', description: 'Alloh'},
]
});
Working Xtemplate and data config
xtype: 'component',
cls: 'kpi-tiles',
id: 'statisticsBoxes',
height: 100,
tpl: [
'<div class="kpi-meta">',
'<tpl for=".">',
'<span>',
'<div class="statsDiv">{statistic}</div> {description}',
'</span>',
'</tpl>',
'</div>'
],
data: [{
description: Ext.getStore('statisticsStore').getAt(0).data.statistic,
statistic: Ext.getStore('viewStore').getAt(0).data.statistic
},{
description: Ext.getStore('viewStore').getAt(1).data.description,
statistic: Ext.getStore('viewStore').getAt(1).data.statistic
}],
But when I am changing the data for the template so it will load data from the Statistics store, the following error occurs in the console.log: Uncaught TypeError: Cannot read property 'getAt' of undefined.
Data config:
data: [{
description: 'the description',
statistic: Ext.getStore('statisticsStore').getAt(0).data.statistic
}],
Statistics store:
Ext.define('ExecDashboard.store.Statistics', {
extend: 'Ext.data.Store',
alias: 'store.statistics',
storeId: 'statisticsStore',
model: 'ExecDashboard.model.Statistics',
proxy: {
type: 'ajax',
url: '/statistics',
reader: 'json'
}
});
Produces the following JSON:
[{"statistic":"1"}, {"statistic":"2"}]
The store is loaded in the viewModel:
stores: {
Statistics: {
type: 'statistics',
autoLoad: true
}
}
I think the problem is that the store is not loaded at that moment. But I don't know how to solve this problem. I know that 'Ext.getStore('statisticsStore').getAt(0).data.statistic' works in the console.log when the store is loaded.
Use an Ext.view.View, that's exactly what the class is for:
xtype: 'dataview',
cls: 'kpi-tiles kpi-meta',
id: 'statisticsBoxes',
height: 100,
itemSelector: '.statsDiv',
tpl: [
'<tpl for=".">',
'<span>',
'<div class="statsDiv">{statistic}</div> {description}',
'</span>'
'</tpl>'
],
store: 'statisticsStore'
You probably need to set autoLoad: true on your store.
An event listener on the store in my viewModel solves the problem.
Statistics: {
type: 'statistics',
autoLoad: true,
listeners: {
load: function(){
var data = [{"description": "New", "statistic" : this.data.items[0].data.statistic}];
Ext.getCmp('statisticsBoxes').update(data);
}
}
}
When the store is loaded, the event will be fired which updates the Xtemplate with new data

Extjs 5 add filters to grid dynamically on reconfigure

I have a grid that is generated dynamically
I build the columns array and store, then call reconfigure on my grid
Next I create a filter array. Now filters have to be applied to the columns in my grid.
But the filters are not reflected in my grid. How can ths be achieved?
myView.js
Ext.define('myView.view.myView', {
extend: 'Ext.grid.Panel',
plugins: 'gridfilters',
store: 'myStore.store.myStore',
pageSize:10,
dockedItems: [{
xtype: 'pagingtoolbar',
store: 'WhiskerPlot.store.settingStore', // same store GridPanel is using
dock: 'bottom',
displayInfo: true
}],
filters :[]
});
myController.js
var colFilters = [];
colFilters.push({
type : 'list',
dataIndex : dummyDataIndex,
options : [small,large]
});
var filters = {
ftype : 'filters',
filters : colFilters
};
var gridview = Ext.ComponentQuery.query('myView')[0];
gridview.reconfigure(store,columnarr);
gridview.filters.addFilters(filters);
Add the filters to the column array itself.
Copied from: Filters Doc
var grid = Ext.create('Ext.grid.Panel', {
store: {
url: 'path/to/data'
},
plugins: 'gridfilters',
columns: [{
dataIndex: 'id',
text: 'Id',
filter: 'number'
}, {
dataIndex: 'name'
text: 'Name',
filter: {
type: 'string',
value: 'Ben'
}
}, {
...
}]
});

extJs - populate grid panel with array of strings

I receive an array of strings from the server.
How can I populate my grid panel if the data is not in key-value format?
Here is the response:
{"result":true,"data":["dep1","dep2","dep3"],"totalCount":3}
Here is my grid panel
xtype: 'gridpanel',
flex: 1,
itemId: 'departmentsGridPanel',
title: '',
store: new Ext.data.ArrayStore({
autoLoad: true,
fields: [
'department'
],
proxy: {
type: 'ajax',
url: 'FilteringDataServlet?filterColumn=avdeling',
reader: {
type: 'json',
root: 'data'
}
}
}),
columns: [{
text: 'Avdeling',
flex: 1,
dataIndex: 'department'
}],
Then you should use Ext.data.reader.Array with mapping parameter.
Employee = Ext.define('Employee', {
extend: 'Ext.data.Model',
fields: [
{name: 'name', mapping: 0}, // "mapping" only needed if an "id" field is present which
{name: 'occupation', mapping: 1} // precludes using the ordinal position as the index.
]
});
I have solved this by adding load listener to my store and modifying the data there
listeners: {
load: function(store, records, success, opts) {
store.each(function(record) {
record.set('department', record.raw);
});
}
}

Trouble loading JSON data into a ExtJS datastore

I've tried every combination I can think of in terms of how to configure my ExtJS datastore to read my incoming JSON data. I'm getting this JSON data in:
[{ "data_type": {"attribute1" : "value1",
"attribute2" : "value2",
"attribute3" : "value3"
}
},
{ "data_type": {"attribute1": "value4",
"attribute2" : "value5",
"attribute3" : "value6"
}
}
]
I don't want to parse the JSON and reformat it in order to make ExtJS happy because it seems redundant. What I want to end up with is a datastore which would allow me to do:
Ext.create('Ext.container.Container',
{
id: 'junk',
renderTo: 'slider',
width: 960,
height:600,
layout: 'vbox',
items: [
{
xtype: 'grid',
title: 'foobar',
height: 400,
width: 700,
store: my_store,
viewConfig: { emptyText: 'No data' },
columns: [
{
text: 'column1',
dataIndex: 'attribute1'
},{
text: 'column2',
dataIndex: 'attribute2'
},{
text: 'column3',
dataIndex: 'attribute3'
}
]
}
]
}
I know ExtJS knows how to parse this JSON, because I can do:
var foo = Ext.decode(data);
var good_data = foo[0].data_type.attribute1
And that returns 'value1' as I would expect. Can anyone help me understand the magical incantation to get a datamodel and store to do that?
Thanks!
First of all you should create model:
Ext.define('SomeModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'attribute1'},
{name: 'attribute2'},
{name: 'attribute3'}
]
});
Then you can configure store to support your data format by setting record property to data_type:
var store = Ext.create('Ext.data.Store', {
autoLoad: true,
data : data,
model: 'SomeModel',
proxy: {
type: 'memory',
reader: {
type: 'json',
record: 'data_type'
}
}
});
Working sample: http://jsfiddle.net/lolo/WfXK6/1/

Get the clicked JSON element's value with ExtJS

I have a JSON store:
var jsonstore = new Ext.data.ArrayStore({
fields: ['bla', 'blubb'],
data: [ ['bla', 'blubb'],
['blabla', 'blublu'],
['blass', 'hallo'],
['bam', 'guckt'] ]
});
and an extjs listview:
....
,{
xtype: 'listview',
name: 'abrufliste',
store: jsonstore,
id:"ladebereich",
multiSelect: false,
emptyText: 'nix da',
reserveScrollOffset: true,
columns: [ { header: 'Name',
width: .5,
dataIndex: 'NAME' }
....
and a click event:
Ext.ComponentMgr.get('ladebereich').on("click",function (sthis,index,node,e ){
alert("node: "+node.childNodes[0].childNodes[0].innerHTML);});
I want to get the clicked node's value.
I do get the value with
node.childNodes[0].childNodes[0].innerHTML
, however thats a crappy solution.
I want to get the clicked Element from my jsonstore,
any suggestions?
Another way is to add a listener to your listview to respond to any click events on the listview:
,{
xtype: 'listview',
name: 'abrufliste',
store: jsonstore,
id:"ladebereich",
multiSelect: false,
emptyText: 'nix da',
reserveScrollOffset: true,
listeners:
{
click: function(object, selectedIndex, node, event) {
// Get the name corresponding to the selected row.
var rowName = object.store.getAt(selectedIndex).get("Name");
}
},
columns: [ { header: 'Name',
width: .5,
dataIndex: 'NAME' }
it works with
Ext.ComponentMgr.get('ladebereich').on("click",function (sthis,index,node,e ){
var rec = jsonstore.getAt(index);
alert(rec.get("NAME"));
});