Friday, January 25, 2013

Conditional coloring of the list rows in Sencha Touch 2

I had a tough time doing this. Finally its up. You need to manipulate on the dom.
Just need to add the following listener on your list item.

listeners:{
                activate:function(list,index,item,record){
                var itemClass =Ext.get('id-of-your-list').dom.getElementsByClassName("x-list-item");
                    for (var i = 0; i < itemClass.length; i++) {                       
                        if(this.getStore().getData().items[i].raw.status ===1){ //condition on the record
                            itemClass[i].style.backgroundColor='#f3f3c7'; //change the color of the row
                        }else if(this.getStore().getData().items[i].raw.status ===2){
                            itemClass[i].style.backgroundColor='#c7f3c7'; //change the color of the row
                        }
                    }
                }
            }

1 comment:

Unknown said...

Hi, I did a variation on your code, where I use the "paint"-event instead. And more of the sencha-api, then the more dom.

listeners:{
painted: function(element) {
var itemElements = element.dom.getElementsByClassName("x-list-item");

this.getStore().each(function(item, index){
if(item.get('status') === 2){
itemElements[index].style.backgroundColor = '#dff0d8';
}
}, this);
}
},

REFACTORING

 What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...