我认为最好在itemTpl
使用XTemplate
实例时执行这样的计算。此外,出于性能原因,可能最好预先计算类名并将其放入模型中。然后,您只需在模板中使用这个新属性作为{className}
.
这是我在内部使用类计算的示例应用程序itemTpl
:
// ST 2.2.1 application
Ext.application({
name: 'Test',
launch: function() {
var view,
i = 0, N = 100,
data = [],
style;
// fill sample array with random data
while (++i < N + 1) {
data.push({
title: 'Title #' + i,
date: new Date(new Date().getTime() - Math.random() * 1000 * 60 * 60 * 24 * 365)
});
}
// append season classes and other styles to the head of html page
Ext.getHead().createChild({
tag: 'style',
type: 'text/css',
html: ['.x-list .x-list-item.x-list-item-tpl .x-innerhtml {padding: 0;} ',
'.container {padding: 12px 15px; margin-right: 15px;} ',
'.spring {background: url(http://icons.iconarchive.com/icons/flameia/xrabbit/128/Folder-Spring-icon.png) right -4px no-repeat; background-size: 60px;} ',
'.summer {background: url(http://icons.iconarchive.com/icons/flameia/xrabbit/128/Folder-Summer-icon.png) right -4px no-repeat; background-size: 60px;} ',
'.autumn {background: url(http://icons.iconarchive.com/icons/flameia/xrabbit/128/Folder-Autumn-icon.png) right -4px no-repeat; background-size: 60px;} ',
'.winter {background: url(http://icons.iconarchive.com/icons/flameia/xrabbit/128/Folder-Winter-icon.png) right -4px no-repeat; background-size: 60px;} ',
'.date {font-size: 0.6em; color: #777;}'].join('')
});
// construct list view
view = Ext.create('Ext.List', {
items: [{
docked: 'top',
xtype: 'toolbar',
title: 'Test'
}],
fullscreen: true,
infinite: false,// play with this and look at dom and console output
itemHeight: 60,
itemTpl: new Ext.XTemplate(
'<div class="container {date:this.getSeason()}">',
'<div>{title}</div>',
'<div class="date">{date:date("jS F Y")}</div>',
'</div>', {
getSeason: function(date) {
var m = date.getMonth(),
season;
if (m < 2 || m > 10) {
season = 'winter';
} else if (m > 1 && m < 5) {
season = 'spring';
} else if (m > 4 && m < 8) {
season = 'summer';
} else {
season = 'autumn';
}
console.log(season);
return season;
}
}
),
data: data
});
Ext.Viewport.add(view);
}
});