简体   繁体   English

Sencha Touch在列表中的特定项目上更改itemTpl

[英]Sencha Touch change itemTpl on specific items in list

I'd like to be able to add class in the Itemtpl where the Item has it's field "Answered" set to "true" It sound's easy to me, but I don't know where to start.. 我希望能够在Itemtpl中的Item的字段“ Answered”设置为“ true”的地方添加类。这听起来很简单,但是我不知道从哪里开始。

I know I have to check if Answered is true in the tpl, but I don't know how to write in the template.. oO 我知道我必须检查tpl中的Answered是否为true,但是我不知道如何在模板中编写。

        //model
    Ext.define('User', {
        extend: 'Ext.data.Model',
        config: {
            idProperty: 'Name',
            fields: [
                {name: 'Name', type: 'string'},
                {name: 'Address', type: 'string'},
                {name: 'ID', type: 'int'},
                {name: 'WebUrl', type: 'string'},
                {name: 'InfoUrl', type: 'string'},
                {name: 'Answered', type: 'boolean'},
                ]
        }
    });

    //store

    aStore = Ext.create('Ext.data.Store', {
        model: 'User',
        sorters: 'Name',
        grouper: {
            groupFn: function(record) {
                return record.get('Name')[0];
            }
        }
    });

    //full store

    store = Ext.create('Ext.data.Store', {
        model: 'User',
        sorters: 'Name',
        grouper: {
            groupFn: function(record) {
                return record.get('Name')[0];
            }
        },
        proxy: {
            type: 'ajax',
            url: '/Services/RestaurantList.ashx',
            reader: {
                type: 'json',
                rootProperty: 'users'
            }
        },
        listeners:{
            load: function(){
                var all = store.data.all;
                aStore.setData(all.slice(0,30));
            }
        },
        autoLoad: true
    });


    //the list
    list = Ext.create('Ext.List', {
        flex: 8,
        itemTpl: ['<div class="contact">{Name}</div>'],
        store: aStore,
        listeners: {
            itemtap: function(list, index, target, record) {
                mainContainer.setActiveItem(1);
                detailsPanel.setRecord(record);
        },
        plugins: [
            {
                xclass: 'Ext.plugin.PullRefreshFn',
                refreshFn: function(){
                    store.clearData();
                    aStore.clearData();
                    store.clearFilter();
                    aStore.clearFilter();
                    store.load();
                    list.refresh();
                }
            }
        ],
        grouped: true
    });

Have you looked at the docs for XTemplate? 您是否看过XTemplate的文档? http://docs.sencha.com/touch/2.2.1/#!/api/Ext.XTemplate . http://docs.sencha.com/touch/2.2.1/#!/api/Ext.XTemplate In particular, look at the "Conditional processing with basic comparison operators" section. 特别是,请查看“使用基本比较运算符进行条件处理”部分。

If you don't want to use the <tpl if=""> notation, you can also use the ternary operator: 如果您不想使用<tpl if="">表示法,也可以使用三元运算符:

itemTpl: new Ext.XTemplate(
    '<div class="{[values.Answered ? \'answered\' : \'\']}">{Name}</div>'
),
...

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

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