【问题标题】:sencha touch change colour of a specific list itemsencha touch 更改特定列表项的颜色
【发布时间】:2011-12-07 11:12:41
【问题描述】:

有一个非常基本的列表组件,但想根据值更改某些行的行颜色/我尝试设置一个 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>'
    )
});

【问题讨论】:

  • 如果您的数据如下所示,我认为您的代码应该可以工作:[{val: 1, name: 'name1'}...]

标签: javascript list sencha-touch


【解决方案1】:
  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. 添加煎茶触摸列表项的cls属性
new Ext.List({
        fullscreen: true,
        id:'xList',
        itemTpl :xListTpl,
        cls:'myList',
        grouped :true,
        store:  new Ext.data.Store({
        model:'yourModel'
        })
       })
  1. 添加条件以更改列表项Tpl 中特定列表项的颜色
  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>'
                                );

【讨论】:

    【解决方案2】:

    这可能会晚,但你可以这样做

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

    【讨论】:

      【解决方案3】:

      啊,基本错误,这就是你的代码中的内容::

      <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 
      

      EDIT ::为整行添加编码以填充分配的背景颜色

      注意:请创建一个单独的 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; }
      

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

      【讨论】:

      【解决方案4】:

      这是我的工作:

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

      【讨论】:

      • 感谢您的示例!这会为整个列表行着色吗?我尝试了类似的东西,它只着色了中间区域(包含文本的 div)
      • stan229 - 你是怎么做到的?当我这样做时,我得到了与@neolaser 相同的结果 - 只有中间区域是彩色的
      • @neolaser :: 请选择与您的查询匹配的答案并投票以关闭此线程。
      • @SashaZd:为什么?如果我理解正确,问题还没有解决 - 不是整行都是彩色的......
      • 据我所知,整行尚未着色。希望有人能找到解决方案...
      猜你喜欢
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2014-07-08
      相关资源
      最近更新 更多