Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sencha touch change colour of a specific list item

have a very basic list component, but want to change the row colour of some rows depending on a value/ I have tried setting a tpl but it doesnt seem to work. Any help would be appreciated

Ext.create('Ext.dataview.List', {
    id : 'mylist',
    store: myStore,
    tpl: new Ext.XTemplate(
             '<tpl for=".">'
             '    <tpl if="val == 0"><div style="background-color:red">{name}</div></tpl>',
             '    <tpl if="val == 1"><div>{name}</div></tpl>',
             '</tpl>'
    )
});
like image 446
neolaser Avatar asked Oct 10 '22 18:10

neolaser


1 Answers

Ah, basic mistake, this is what you've got in your code ::

<tpl if="val == 0">

And this is what it should be instead :::

<tpl if="val === 0">

** Notice the three "equal to" signs that you need to add between the two values you're actually comparing. So if you normally wrote

x=y 

Then in a template that would be

x==y  // (you basically add an extra equal) 

So a conditional statement like

x==y  //when you're checking if the values are equal

Becomes

x===y 

EDIT :: Adding the Coding for the entire row to be filled with the background color assigned

NOTE :: Please make a separate XTemplate object, and not inline tpl code. That will allow you to harness the full potential of the XTemplate, including member functions which are incredibly cool !

Trial 1 ::

tpl Code to be added for background color

  '<li class="{[this.listClasses(xindex,xcount)]}">',
          '<b>&nbsp; &nbsp;&nbsp; {nameOfMeeting}</b>',
          '<br>&nbsp; &nbsp;&nbsp;&nbsp;Start Time : {start} &nbsp; &nbsp;&nbsp;  ||  &nbsp; &nbsp;&nbsp;  End Time : {end}',
  '</li>',
   {
        listClasses : function(position, size){
            var classes = [];
            if (position%2===0) {classes.push("even")}
            else                {classes.push("odd")};
            if (position === 1) {classes.push("first")}
            else                {classes.push("last")};
            return classes.join(" ");
        }
    }

//Note : I've added in the helper functions that I'm using to change the background color of the class. My tpl, basically uses alternate color on every list line. So if the first line is green, the second is yellow, the third is green, the fourth is yellow, and so on.

Associated CSS to be added (for the listClasses selected in the li tag)

#meetingsList li.odd { background-color: #ebdde2; }
#meetingsList li.even { background-color: #fdeef4; }
#meetingsList li.odd { border-bottom: 1px solid #999; }
#meetingsList li.even { border-bottom-style: none; }

EDIT Trial 2 :: New CSS to be added

CSS

.testview .x-dataview-item {            border-bottom : 1px solid #cccbcb;        }        
.testview .x-item-selected {           background-color: #006bb6;           background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #50b7ff), color-stop(2%, #0080da), color-stop(100%, #005692) );            background-image: -webkit-linear-gradient(#50b7ff, #0080da 2%, #005692);            
background-image: linear-gradient(#50b7ff, #0080da 2%, #005692);            
color: #fff;;            
text-shadow: rgba(0, 0, 0, 0.5) 0 -0.08em 0;            
border-color: #103656;        }

To add the CSS into Code, add the following to the list object ::

{
 xtype : 'list'
 . . . . 
 cls : 'testview'
}
like image 129
SashaZd Avatar answered Oct 13 '22 10:10

SashaZd