Monthly Archives: March 2014
Sencha Touch List Expand and Collapse All Items
First add events of the buttons to your controller.
btnExpandAll: {
tap: ‘expandAll’
},
btnCollapseAll: {
tap: ‘collapseAll’
}
Now add above two functions in controller.
expandAll: function(){
var list = this.getItemsList();
var items = list.getViewItems();
var itemsLn = items.length;
for (i = 0; i < itemsLn; i++) {
item = items[i];
Ext.Animator.run({
element: item.element,
duration: 500,
easing: ‘ease-in’,
preserveEndState: true,
from: {
height: 60
},
to: {
height: 190
}
});
}
}
collapseAll: function(){
var list = this.getItemsList();
var items = list.getViewItems();
var itemsLn = items.length;
for (i = 0; i < itemsLn; i++) {
item = items[i];
Ext.Animator.run({
element: item.element,
duration: 500,
easing: ‘ease-in’,
preserveEndState: true,
from: {
height: 190
},
to: {
height: 60
}
});
}
}
You can set your own from and to height according to your requirement.
Sencha Touch List, Different background colors for Group Header
Here I added background color in each items of the list and then grouped it based in certain field. For example.
Ext.define(‘MyApp.model.MyModel’, {
extend: ‘Ext.data.Model’,
config:{
fields: [
{ name:’id’, type:’int’},
{ name:’group_background_color’, type:’string’},
{ name:’item_name’, type:’auto’},
{ name: ‘group’, type: ‘string’}
]
}
})
Then we have following Store definition.
Ext.define(‘MyApp.store.MyStore’, {
extend: ‘Ext.data.Store’,
config: {
model: ‘MyApp.model.MyModel’,
autoLoad: true,
grouper: {
groupFn: function (item) {
return item.get(‘group’);
}
},
proxy: {
type:’ajax’,
actionMethods:{
read: ‘POST’
},
url:’items.json’,
reader:{
type:’json’,
rootProperty:’items’
}
}
}
});
Now we have our list where we will show this data.
{
xtype: ‘list’,
id:’myList’,
itemId:’myList’,
scrollable: true,
grouped: true,
itemTpl:'<div>{item_name}</div>’ ,
store: ‘MyStore’
}
This will create list with groups and group header. If you use default Sencha Theme the group header will have blue as background color. We have to change it to our colors dynamically. For that we will update on list refresh. List refresh event is fired when list is visible first time and there is some change in data in store or list store is loaded. Add refresh event in controller.
control:{
‘#myList’:{
refresh: ‘refreshListHeaders’
}
}
And here is the function.
refreshListHeaders: function(list){
if(list.groups){
var groupElements = document.getElementsByClassName(‘x-list-header’);
for(var i=0;i <
groupElements.length i++){
var firstChild = list.groups[i].children[0]; var backGroundColor = firstChild.data.banner_background_color;
groupElements[i+1].style.setProperty(‘background-color’, backGroundColor, ‘!important’);
}
}
}
So our logic is very simple. We get all the elements with class name “x-list-header” . That gives us all the header elements. Now for each group we are getting background color from the first item of the group and set it as background color to group header.