How to refresh the store/model every few seconds? - extjs

i have a grid panel (store and models are associated with it) where i am displaying all the data in row wise manner.The column of grid panel consist of action column where if person clicks, it opens a new form in tab style and user fills the data and it got saved in db. i want is, how the store should be refreshed every few second so that this data should also be displayed in the grid panel automatically. How should I do it?

There are a number of ways you could handle this, personally I would just reload the store when the form has closed to reload any updates rather than just reloading constantly.
The code below shows how you could use a TaskRunner in ExtJs to reload the store every x seconds, You could also add a setTimeout in the load event of the store so that every time the store loads you schedule it to load again in x seconds.
Ext.application({
name: 'Fiddle',
launch: function() {
var store = Ext.create('Ext.data.JsonStore', {
storeId: 'simpsonsStore',
fields: ['name', 'email', 'phone'],
autoLoad: true,
proxy: {
type: 'ajax',
url: 'data1.json',
reader: {
type: 'json',
rootProperty: 'characters'
}
},
listeners: {
load: function() {
console.log(this);
}
}
});
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: store,
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
var runner = new Ext.util.TaskRunner(),
task = runner.start({
run: function() {
store.reload();
},
interval: 3000
});
// IMPORTANT: Dont forget to call stop at a logical point.
runner.stop();
}
});
There is also a fiddle containing this code that you can play around with.
If I had to go with the SetTimeout or TaskRunner options for this I would likely initialize them in the render event of the parent panel/container and listen for the beforeDestroy event to stop them running.

Related

State management in ext js?

I am trying to use this following code in my application to retain the current state of my application even after it is refreshed.
Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
But after executing above code, It's not working for me.
Kindly suggest any way to do this as, I am new to ExtJs
Thanks.
You need to define this in the Launch function of your application or init of controller. Its critical to use expires when using state provider.
init : function() {
Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
expires : new Date(Ext.Date.now() + (1000*60*60*24*90)) // 90 days
}));
}
Then based on what components state you are trying to save set its stateful property to true, alternatively giving it a stateId.
Below is example to save state of grid:
Ext.define('MyApp.view.Users', {
extend: 'Ext.grid.Panel',
alias: 'widget.users_grid',
title: 'Users',
stateful:true,
stateId:'users_grid_stid',
store:{
data:[
{
first_name:'Joe',
last_name:'Blogs'
}
]
},
columns: {
items: [
{
xtype: 'gridcolumn',
dataIndex: 'first_name',
text: 'First Name',
width: 200
},
{
xtype: 'gridcolumn',
dataIndex: 'last_name',
text: 'Last Name',
width: 200
}
]
}
});
You can test this by modifying the width of one of the columns on this grid and then that column will be hold the modified width when you next load the page, even after browser restart.
Check F12 Console -> Application Cookies for saved component states.

Extjs getting modified value from textbox rendered inside gridpanel

I'm trying to get value inserted from the interface in a text box rendered inside a GridPanel extjs 3.4, below how is defined the textbox inside the column model:
header: "Rosso",
dataIndex: "contrFilEsRosso",
width: 50,
renderer: function(val, meta,record){
var str0='<p align="left"><input type="text" name="verde" value="' + val + '
return str0;
}
I've modified from the interface the value inside the textbox and i want to send the modified value to the controller. Obviously the store has the value extracted from the backend and is not updated with the new value, so i tried the getView() method of the GridPanel but i haven't been able to get the new value of the textbox.
Thanks a lot.
Any data loaded into an ExtJs grid will be kept in a store, along with the edits.
There are a number of ways you can get the specific value but usually you will just want to send the data back to the server to update as needed.
You can access the store via the getStore() method on the grid and from there you can access any individual records by id or index.
Yo would be best using an EditorGrid and listening to events such as afterEdit
Something like this should work (might need some tweaking, not tested as on mobile)
Fiddle
Ext.application({
name: 'MyApp',
launch: function() {
var store = Ext.create('Ext.data.Store', {
storeId: 'simpsonsStore',
fields: ['name', 'email', 'phone'],
proxy: {
type: 'ajax',
url: 'data1.json',
reader: {
type: 'json',
rootProperty: 'items'
}
},
autoLoad: true
});
Ext.create("Ext.grid.EditorGridPanel", {
title: 'Simpsons',
renderTo: Ext.getBody(),
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
isCellEditable: function(col, row) {
return (col == 0);
},
listeners: {
afterEdit: function(e) {
alert("You changed " + e.field + " from " + e.originalValue + " to " + e.value);
}
},
height: 200,
width: 400,
});
}
});

Extjs 5 Progress bar during initComponent

I have a tab panel, and I perform a server request in it's initComponent method to add tabs according to the result. This process can take a long time, and I would like to display a progress bar during the begining of initComponent, till the end (before the callParent call method for example).
Is it possible to achieve this behaviour ?
Thanks a lot !
You can use a LoadMask on any pretty much any component, here is an example, there is also a fiddle:
Ext.application({
name: 'Fiddle',
launch: function() {
var store = Ext.create('Ext.data.JsonStore', {
storeId: 'simpsonsStore',
fields: ['name', 'email', 'phone'],
autoLoad: true,
proxy: {
type: 'ajax',
url: 'data1.json',
reader: {
type: 'json',
rootProperty: 'characters'
}
},
listeners: {
load: function() {
console.log(this);
}
}
});
var grid = Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: store,
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
var myMask = new Ext.LoadMask(grid, {msg:"Please wait..."});
myMask.show();
var timeout = setTimeout(function() {
myMask.hide();
}, 5000);
}
});
I've added a setTimeout to replicate a long loading request, you do not need to use this.
You do not need to rely on "callParent" method. You have two options:
Make your server request synchronously. (by setting async:false for your ext.ajax call or for the related operation) It causes your code to be waited till the response comes.it blocks UI interaction but makes you sure having response immediately in line after server call
Another way is calling this.setLoading(true) before calling ajax call and setting this.setLoading(false) in your ajax/operation callback method. Note that this should refer to your main component. So it is bettor to save it in a variable at initComponent first line. For example by a line like var me= this;. After that refer to current main component bymeinstead ofthis`
var progressBar = Ext.create('Ext.ProgressBar');
progressBar.wait({
interval: 1000,
duration: 10000,
increment: 10,
text: 'Adding tabs...',
scope: this,
fn: function() {
var tabPanel = Ext.create('Ext.tab.Panel');
me.add(tabPanel);
tabPanel.add({
xtype: 'panel',
title: 'Main tab'
});
tabPanel.add({
xtype: 'panel',
title: 'Chunk'
});
tabPanel.add({
xtype: 'panel',
title: 'My girl'
});
progressBar.hide();
}
});
Fiddle

Extjs grid cant get store buffered working

I have a large dataset (over 80k records). So I am trying to implement buffer but I couldn't get it work. If I include buffered configuration I am getting error object doesn't support this property. When I remove it, works ok but of course it takes forever to load the data.
Here is my code
Ext.Loader.setConfig({
enabled: true,
disableCaching: false
});
Ext.require(['Ext.data.*', 'Ext.grid.*',
'Ext.grid.plugin.BufferedRenderer', 'Ext.ux.grid.FiltersFeature']);
Ext.define('Borrower', {
extend: 'Ext.data.Model',
fields: [{
name: 'Name',
type: 'string'
}, {
name: 'Num',
type:
'string'
}]
});
Ext.onReady(function () {
var store = Ext.create('Ext.data.Store', {
autoLoad: false,
model: 'Borrower',
pageSize: 100,
buffered: true, // getting error object doestn support this property in extjs-all-debug.js
proxy: {
type: 'rest',
url: 'borrBuff.xsp/byName',
reader: {
type: 'json',
root: 'items'
}
}
});
var filters = {
ftype: 'filters',
encode: false,
local: true
};
var grid = Ext.create('Ext.grid.Panel', {
id: 'testgrid',
store: store,
features: [filters],
plugins: 'bufferedrenderer',
columns: [{
text: 'Borr Name',
dataIndex: 'Name',
filterable: true
}, {
text: 'Number',
dataIndex: 'Num'
}]
});
})
Opening the grid in a window on button click
var grid = Ext.getCmp('testgrid');
var win = Ext.create('Ext.Window', {
title: 'Grid Filters Example',
height: 400,
width: 700,
modal: true,
layout: 'fit',
items: grid
}).show();
grid.getStore().load();
Just couldnt figure out what I am doing wrong. Appreciate any help in fixing this issue.
Is there any reason to fetch all the data from server?
I'd recommend using data paging with all the pros, speed being the first one (and some cons too). If you choose to use an infinite grid, it will work well. In fact the basic idea behind infinite grid is fetching data in chunks (data paging in other words).
Filtering works without any problems with this scenario, see sample. It's usually handled by server, which is built to do this kind of tasks.

load data from grid row into (pop up) form for editing

I read in Ext JS in Action ( by J. Garcia) that if we have an instance of Ext.data.Record, we can use the form's loadRecord method to set the form's values. However, he does not give a working example of this (in the example that he uses data is loaded into a form through a file called data.php). I have searched many forums and found the following entry helpful as it gave me an idea on how to solve my problem by using form's loadRecord method:
load data from grid to form
Now the code for my store and grid is as follows:
var userstore = Ext.create('Ext.data.Store', {
storeId: 'viewUsersStore',
model: 'Configs',
autoLoad: true,
proxy: {
type: 'ajax',
url: '/user/getuserviewdata.castle',
reader: {
type: 'json',
root: 'users'
},
listeners: {
exception: function (proxy, response, operation, eOpts) {
Ext.MessageBox.alert("Error", "Session has timed-out. Please re-login and try again.");
}
}
}
});
var grid = Ext.create('Ext.grid.Panel', {
id: 'viewUsersGrid',
title: 'List of all users',
store: Ext.data.StoreManager.lookup('viewUsersStore'),
columns: [
{ header: 'Username', dataIndex: 'username' },
{ header: 'Full Name', dataIndex: 'fullName' },
{ header: 'Company', dataIndex: 'companyName' },
{ header: 'Latest Time Login', dataIndex: 'lastLogin' },
{ header: 'Current Status', dataIndex: 'status' },
{ header: 'Edit',
menuDisabled: true,
sortable: false,
xtype: 'actioncolumn',
width: 50,
items: [{
icon: '../../../Content/ext/img/icons/fam/user_edit.png',
tooltip: 'Edit user',
handler: function (grid, rowIndex, colIndex) {
var rec = userstore.getAt(rowIndex);
alert("Edit " + rec.get('username')+ "?");
EditUser(rec.get('id'));
}
}]
},
]
});
function EditUser(id) {
//I think I need to have this code here - I don't think it's complete/correct though
var formpanel = Ext.getCmp('CreateUserForm');
formpanel.getForm().loadRecord(rec);
}
'CreateUserForm' is the ID of a form that already exists and which should appear when user clicks on Edit icon. That pop-up form should then automatically be populated with the correct data from the grid row.
However my code is not working. I get an error at the line 'formpanel.getForm().loadRecord(rec)' - it says 'Microsoft JScript runtime error: 'undefined' is null or not an object'.
Any tips on how to solve this?
Rec is undefined in your EditUser function. You give an ID as parameter to your EditUser, you need to give the record to it as a parameter instead.
//The call
EditUser(rec);
function EditUser(rec) {
var formpanel = Ext.getCmp('CreateUserForm');
formpanel.getForm().loadRecord(rec);
}

Resources