繁体   English   中英

sencha touch改变特定列表项的颜色

[英]sencha touch change colour of a specific list item

有一个非常基本的列表组件,但想要改变一些行的行颜色取决于一个值/我尝试设置一个tpl但它似乎没有工作。 任何帮助,将不胜感激

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

啊,基本错误,这就是你在代码中得到的::

<tpl if="val == 0">

而这应该是它应该是:::

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

**注意您需要在实际比较的两个值之间添加三个“等于”符号。 所以,如果你通常写

x=y 

然后在一个模板中

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

所以像条件语句一样

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

x===y 

编辑::为要填充的整个行添加编码,并指定背景颜色

注意::请创建一个单独的XTemplate对象,而不是内联tpl代码。 这将使您充分发挥XTemplate的潜力,包括非常酷的成员函数!

试验1 ::

tpl为背景颜色添加的代码

  '<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(" ");
        }
    }

//注意:我在助手函数中添加了我用来改变类的背景颜色。 我的tpl,基本上在每个列表行使用替代颜色。 因此,如果第一行是绿色,第二行是黄色,第三行是绿色,第四行是黄色,依此类推。

要添加的关联CSS(对于在li标签中选择的listClasses)

#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; }

编辑试用2 ::要添加的新CSS

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

要将CSS添加到代码中,请将以下内容添加到列表对象::

{
 xtype : 'list'
 . . . . 
 cls : 'testview'
}

这可能会迟到,但你可以这样做

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>'),

并在您的css文件中添加列表样式

.patientList.x-list-item
{
    background-color: #ff0000;     
}
  1. 创建自己的样式并覆盖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. 添加sencha触摸列表项的cls属性
new Ext.List({
        fullscreen: true,
        id:'xList',
        itemTpl :xListTpl,
        cls:'myList',
        grouped :true,
        store:  new Ext.data.Store({
        model:'yourModel'
        })
       })
  1. 添加条件以更改列表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>'
                                );

这是我做的:

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>'),

在我的CSS而不是背景颜色我使用背景渐变:

    .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));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM