最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - sencha touch change colour of a specific list item - Stack Overflow

programmeradmin1浏览0评论

have a very basic list ponent, 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>'
    )
});

have a very basic list ponent, 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>'
    )
});
Share Improve this question asked Oct 14, 2011 at 4:46 neolaserneolaser 6,90719 gold badges59 silver badges92 bronze badges 1
  • 1 Your code should work, I think, if your data looks like this: [{val: 1, name: 'name1'}...] – ZenMaster Commented Oct 14, 2011 at 5:19
Add a ment  | 

4 Answers 4

Reset to default 3 +100

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 paring. 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

Bees

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'
}

This may be late but you can do it this way

items: [{
    xtype: 'list',
    id: 'patientList',
    store: app.stores.patientList,
    itemTpl: new Ext.XTemplate('<tpl if="overDue14Days &gt; 0"><div class="severeItem"></div></tpl><tpl if="overDue3Days &gt; 0"><div class="mildItem"></div></tpl>', '<div class="listBox">', '<div class="listText">{patientFirstName} {patientLastName}', '<div class="metadata">{numberOfOrders} orders</div>', '</div>', '<div class="listSpacer"></div>', '<div class="deleteItem" id="notMyPatientButton"></div>', '<div class="listArrow"></div>', '</div>'),

and in your css file just add the list style

.patientList.x-list-item
{
    background-color: #ff0000;     
}

Here's what I do:

items: [{
    xtype: 'list',
    id: 'patientList',
    store: app.stores.patientList,
    itemTpl: new Ext.XTemplate('<tpl if="overDue14Days &gt; 0"><div class="severeItem"></div></tpl><tpl if="overDue3Days &gt; 0"><div class="mildItem"></div></tpl>', '<div class="listBox">', '<div class="listText">{patientFirstName} {patientLastName}', '<div class="metadata">{numberOfOrders} orders</div>', '</div>', '<div class="listSpacer"></div>', '<div class="deleteItem" id="notMyPatientButton"></div>', '<div class="listArrow"></div>', '</div>'),

In my css instead of a background color I use a background gradient:

    .severeItem {
    background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(0%,#fccdce), color-stop(10%, #fcc2c4), color-stop(50%,#fdaaac), color-stop(100%, #ff8891));
}

    .mildItem{ 
        background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(0%,#feedba), color-stop(10%, #fcda8b), color-stop(50%,#fdd986), color-stop(100%, #ffe888));
    }
  1. Create your own styles and override sencha touch list-item css
.myList {

  }
.myList .x-list-item {
    position:relative;
    color:#333;
    padding:0em 0em;
    min-height:2.1em;
    display:-webkit-box;
    display:box;
    border-top:1px solid #c8c8c8

}
.myList .x-list-item .brands-row-spon {
    width: 100%;
    background-color: #D8D8D8;
    padding:0.5em 0.8em;
    min-height:2.6em;
}
.myList .x-list-item .brands-row {
    width: 100%;
    min-height:2.6em;
    padding:0.5em 0.8em;
}
  1. Add cls property of sencha touch list item
new Ext.List({
        fullscreen: true,
        id:'xList',
        itemTpl :xListTpl,
        cls:'myList',
        grouped :true,
        store:  new Ext.data.Store({
        model:'yourModel'
        })
       })
  1. Add a condition to change the colour of a specific list item in your list itemTpl
  var  xListTpl = new Ext.XTemplate(
                                '<tpl for=".">',
                                '<tpl if="isSponsored ==&quot;true&quot;"">',
                                '<div class="brands-row-spon">',
                                '</tpl>',
                                '<tpl if="isSponsored !=&quot;true&quot;"">',
                                '<div  class="brands-row">',
                                '</tpl>',
'</tpl>'
                                );
发布评论

评论列表(0)

  1. 暂无评论