Displaying the number of rows in a grid - Extjs - list

So I'm trying to display the number of rows (records) returned from a grid. Here's the code:
Ext.define('AM.view.user.List' ,{
extend: 'Ext.grid.Panel',
alias: 'widget.userlist',
title: '<center>Data Management</center>',
store: 'Users',
dockedItems: [{
xtype: 'toolbar',
dock: 'bottom',
items: [
{ xtype: 'tbtext', text: 'Number of Records\:' + ***code that will return number of records*** },
{ xtype: 'tbfill' },
{ text: 'Print' },
{ text: 'Export' }
]
}],
...
I'm not sure how to use the getCount() method to return the number of rows from this grid (or store?). Any ideas?
Heres: my store:
Ext.define('AM.store.Users', {
extend: 'Ext.data.Store',
model: 'AM.model.User',
fields: ['field1', 'field2', 'field3'],
data: [
{field1: 'Data 1', field2: 'Data 2', field3: 'Data 3'},
{field1: 'Data 1', field2: 'Data 2', field3: 'Data 3'},
{field1: 'Data 1', field2: 'Data 2', field3: 'Data 3'}
]
});

You won't be able to do this dynamically, where it updates on its own, so you will have to use a placeholder and update the panel when the store is loaded.
{ xtype: 'tbtext', itemId: 'numRecords' }
Then:
listeners: {
render: function(store) {
store.on('load', function(records) {
var count = records.length; //or store.getTotalCount(), if that's what you want
grid.down('#numRecords').setText('Number of Records: ' + count);
});
}
}

You can try to add a paging toolbar and use the properties displayInfo & displayMsg.
Try this code :
bbar : {
xtype : 'pagingtoolbar',
displayInfo: true,
store:'your store',
displayMsg : 'Total rows {2}'// defaultvalue = 'Displaying {0} - {1} of {2}'
}

Assuming your store is loaded before the grid is rendered, this will probably work:
Ext.data.StoreManager.lookup("Users").getCount();
If the store loads dynamically, you will need to attach an event to the store's load event to update your grid, comment if the above code does not work and I can probably help you.

As it has already been said, you have to wait for the store to be loaded before using getCount. Except in the case you'd really use a memory store like the one in your example, but I doubt you'd want to display a dynamic number of record for that use case...
So, you have to listen for the load event of the store and update your text item then. The load event will fire each time the store is loaded, reloaded, etc., which may occur multiple times if your grid is paged or allow for filtering, etc. That means that our number of records will be kept in sync with the actual content of the store. Good.
Now, how to install that listener? One very common place for putting that kind of treatment is in the initComponent method of your component.
Here's the code. See the comments for a crash course in overriding initComponent (see another answer for a lecture on the topic).
Ext.define('AM.view.user.List' ,{
extend: 'Ext.grid.Panel',
alias: 'widget.userlist',
title: '<center>ECRIS-MetaData Management</center>',
store: 'Users',
dockedItems: [{
xtype: 'toolbar',
dock: 'bottom',
items: [
// Give an itemId to this component to make it easy to
// reference later.
{ xtype: 'tbtext', text: 'Loading...', itemId: 'recordNumberItem' },
{ xtype: 'tbfill' },
{ text: 'Print' },
{ text: 'Export' }
]
}],
initComponent: function() {
// We're overriding an existing method, so that's very important to call
// the parent method, or the component will break in awful sufferings
this.callParent(arguments);
// I'm putting the code *after* callParent, so that the store is available
var store = this.getStore(),
// Using ComponentQuery to retrieve the text item
textItem = this.down('#recordNumberItem');
// Using `mon` instead of `on` for better memory management (the listener
// will be removed from the store automatically when the component is
// destroyed).
this.mon(store, 'load', function() {
// We're left with the easy part...
textItem.setText("Number of records: " + store.getCount());
});
}
// ...
});

Related

sencha horizontal scroll with a list

I am trying follow the list-horizontal example given by Sencha.
I am getting a message saying
( The base layout for a DataView must always be a Fit Layout )
In the example it is not a fit layout. Could someone please tell me what I am missing here.
Thanks
Jim
Ext.define( 'Styles.view.Beginning', {
extend: 'Ext.List',
xtype: 'beginninglist',
config: {
title:'Beginning',
iconCls: 'star',
layout:{
type:'vbox',
pack: 'center'
},
items:[
{
//give it an xtype of list for the list component
xtype: 'dataview',
height: 250,
scrollable: 'horizontal',
directionLock: true,
inline: {
wrap: false
},
//set the itemtpl to show the fields for the store
itemTpl: '{name} {image}',
//bind the store to this list
store: 'Beginning'
}
]
}
});
First of all, you need to learn Sencha layouting. You can refere here. You were creating a List and then trying to put Dataview inside that. So if you need Dataview, directly extend Dataview like below:
Ext.define('Styles.view.Beginning', {
extend: 'Ext.dataview.DataView',
xtype: 'beginninglist',
config: {
inline: {
wrap: false
},
scrollable: {
direction: 'horizontal'
},
//set the itemtpl to show the fields for the store
itemTpl: '{name} {image}',
//bind the store to this list
store: 'Beginning'
}
});

Sencha Touch 2.3..1 DataView is not loading Store details loaded later

I want to show some details on to Sencha Touch 2.3.1 DataView.
Actual Data to be loaded will come later to the DataView object creation.
I am not using Store proxy, but I have my own existing HTTP Request API for getting the JSON data. So once I get the data, I am creating a DataModel and adding to Store.
When I debug the code, I see it is adding and Store is populated with Data.
Now I am getting the DataView object with Ext.getCmp("") and setting the Store.
When I see the DataView object, I see the store (_store) containing the data (_data) in it.
But still the DataView is not populated with the datails.
Please help me in this context
My DataModel:
Ext.define('AlbumsListDM', {
extend: 'Ext.data.Model',
config: {
fields: [
"user",
"title",
"subtitle",
"image"
]
}
});
My Store:
Ext.define('AlbumsListStore', {
extend: 'Ext.data.Store',
config: {
model: 'AlbumsListDM',
autoLoad: true
}
});
My DataView:
Ext.define(APP_NAME+'.view.AlbumsList', {
extend: 'Ext.DataView',
id: 'viewAlbumsListID',
requires: ['AlbumsListDM'],
config: {
width:'100%',
height:'100%',
layout: 'fit',
items: [{
xtype: 'dataview',
scrollable: true,
inline: true,
cls: 'dataview-inline',
itemTpl: new Ext.XTemplate('<div class="img" style="background-image: url({image});">{title}</div>')
}]
}
});
My Construction of store and updating DataView:
Ext.create('AlbumsListStore', { id: 'AlbumsListStoreID' });
var albumsList = myJsonData;
var albumsListStore = Ext.getStore('AlbumsListStoreID');
var count = albumsList.length;
for(var index = 0; index < count; index++) {
albumsListStore.add(Ext.create('AlbumsListDM', albumsList[index]));
}
var viewAlbumsList = Ext.getCmp("viewAlbumsListID");
viewAlbumsList.setStore(albumsListStore);
Thanks
Siva

Sencha list to display store

First off: There are a bunch of questions about this exact thing all over the place. I have spent the better part of a day reading through them all and clearly I am still failing to understand this.
I am getting my store data from a httprequest (rather than standard ajax call) and this is working and adding my data to the store. But whatever I try, this data will not populate the list. Currently my code looks like:
Model:
Ext.define('estarCamera.model.Event', {
extend: 'Ext.data.Model',
config: {
fields: [
'Id',
'Title',
'Content',
'Image',
'Location',
'Latitude',
'Longitude',
'Radius',
'Starts',
'Expires',
'Prestart'
]
}
});
Store:
Ext.define('estarCamera.store.Events', {
extend: 'Ext.data.Store',
config: {
model: 'estarCamera.model.Event',
storeId: 'EventStore'
}
});
Data is populating the store:
var jsonResponse = JSON.parse(xhr.responseText);
if(jsonResponse.status == "Success"){
//Success
var eventsJsn = JSON.parse(jsonResponse.message);
$.each(eventsJsn, function(){
$.each(this, function(k,v){
//Events root element
$.each(this, function(k,v){
//Each 'Event' element
var eStore = Ext.getStore('EventStore');
eStore.add({
Id: this.ID,
Title: decodeURIComponent(this.Title),
Content: decodeURIComponent(this.Content),
Image: this.Image,
Location: this.Location,
Latitude: this.Latitude,
Longitude: this.Longitude,
Radius: this.Radius,
Starts: this.Starts,
Expires: this.Expires,
Prestart: this.Prestart
});
eStore.sync();
})
});
});
Ideally this will then populate:
Ext.define('estarCamera.view.Events', {
extend: 'Ext.Panel',
xtype: 'events',
requires: [
'estarCamera.store.Events',
'Ext.form.FieldSet',
'Ext.List'
],
config: {
title:'Events',
iconCls: 'star',
layout: 'vbox',
items:[
{
docked: 'top',
xtype: 'toolbar',
title: 'Active Events'
},
{
xtype: 'container',
layout: 'fit',
flex: 10,
items:[{
xtype:'list',
title: 'Events',
width: '100%',
height: '100%',
store: 'Events',
styleHtmlContent: true,
itemTpl: new Ext.XTemplate(
'<div class="outerEvent">',
'<h1>title{Title}</h1>',
'<p>{Content}</p>',
'</div>'
)
}]
}]
}
});
Does anyone know why this is happening?
OK, feeling a bit fooling now .. eventually (after an embarrassingly long time) found that this was because my list was trying to reference 'Events' which is the name of my store and how I thought it was supposed to work. Changed this to 'EventStore' (the storeid of my store) and it worked perfectly.

Sencha add dynamically tap listener to record list

I have an Ext.data.Store and a Ext.Panel with a list.
I add record dynamically with this command:
myStore.add({txt: r});
I would like to add a listener that when I click on a list record, it shows me the record data in a message box.
How can I do it?
Ext.data.store
var myStore = Ext.create('Ext.data.Store', {
storeId: 'MyStore',
fields: ['txt']
}); // create()
Ext.Panel
listpanel = new Ext.Panel({
layout: 'fit', // important to make layout as 'fit'
items: [
{
xtype: 'titlebar',
id: 'myTitle',
docked: 'top',
title: 'Before Change title'
},
{
//Definition of the list
xtype: 'list',
itemTpl: '{txt}',
store: myStore,
}]
});
You need to use itemtap event of the Ext.List component.
E.g
....
....
xtype: 'list',
itemTpl: '{txt}',
store: myStore,
listeners : {
itemtap : function(item, num, record, ev) {
var myTxt = item.getStore().getAt(num).get('txt');
Ext.Msg.alert('Message','Tapped record : '+myTxt);
}
}
....
....

How to select a card in a carousel by menu item in a docked menu list in Sencha touch?

I cannot figure out how I can retrieve a given Data item in a store (id-number) to send it to the "setActiveItem" method in a listener:
So I have a store - model:
Ext.regModel('PictureItem', {
fields: ['id', 'titel', 'url']
});
var pictureItems = new Ext.data.Store({
model: 'PictureItem',
data: [
{id:1, titel:'page 1', url:'http://placekitten.com/1024/768'},
{id:2, titel:'page 2', url:'http://placekitten.com/1024/768'},
{id:3, titel:'page 3', url:'http://placekitten.com/1024/768'},
]
});
Here is my menuList called "leftList":
var leftList = new Ext.List({
dock: 'left',
id:'list1',
width: 135,
overlay: true,
itemTpl: '{titel}',
singleSelect: true,
defaults: {
cls: 'pic'
},
store: pictureItems,
listeners:{
selectionchange: function (model, records) {
if (records[0]) {
Ext.getCmp('karte').setActiveItem(!!!Here the number of the selected Item
or respondend "id" in the data store!!!);
}
}
}
});
and the carousel....
var carousel = new Ext.Carousel({
id: 'karte',
defaults: {
cls: 'card'
},
items: [{
scroll: 'vertical',
title: 'Tab 1',
html: '<img class="orientation" alt="" src="img_winkel/titel_v.jpg">'
},
If I call
Ext.getCmp('karte').setActiveItem(2);
it works with the called card - but how can I get the number from the id of the selected item in the menu List /store????
By the way: what does mean:
if (records[0]) {
why [0]?
I FOUND THE ANSWER FOR MYSELF - IT'S EASY:
Ext.getCmp('karte').setActiveItem(records[0].get('id'), {type: 'slide', direction: 'left'});
The secret to get the record-entry is ".get()":
records[0].get('arrayfield')
So now I can change the activeItem in the carousel easely...