View Full Version : List items with custom icons

23 Sep 2012, 11:21 PM
You can set custom icon to every item in List (Ext.dataview.List) or Dataview according to its type. I have used sprite PNG-image with custom icons. We will take advantages of CSS and templates.
For this you need to do next steps
1) to your items in Data, array of records, which you pass to the List, you should add iconCls field, where you should put custom CSS class containing right icon for each item. And set all this info to your list via store and model

Ext.define('ListModel', {
extend: 'Ext.data.Model',
config: {
fields: [/*your fields*/,
{name: 'iconCls', type: 'string'}
for (var i = 0, ln = records.length; i < ln; i++) {
/*set fields values*/,
iconCls: /*custom CSS class for icon*/

var store = Ext.create('Ext.data.Store', {
model: 'ListModel',
data: data
list = Ext.create('Ext.Dataview.List', {
store: store,
iconsCls: 'iconCls',
iconsBaseCls: 'x-list-icons'

2) In List you should pre-config your Template for item (ColumnName - is name of display field)
Base CSS class is used in context of sprite image implementation.

initConfig: function(config) {
var iconsCls = this.getIconsCls();
var iconsBaseCls = this.getIconsBaseCls();
iconsBaseCls = iconsBaseCls ? iconsBaseCls : '';
var itemTpl = new Ext.XTemplate(
'<div class="' + iconsBaseCls +' {' + iconsCls + '}">' ,
{' + ColumnName + '}</div>'

And there you are. Every item will set its own CSS class according to its iconCls field. I show some CSS example, which fits the code above.

.x-list .x-list-item .x-list-icons {
padding-left: 32px !important;
.x-list .x-list-item .x-list-icons::before {
background-image: url('../images/icons.png');
background-repeat: no-repeat;
width: 32px;
height: 32px;
content: "";
position: absolute;
left: 0;
.x-list-icons.x-icon1::before {
background-position: 0px 0px;
.x-list-icons.x-icon2::before {
background-position: -36px 0px;
.x-list-icons.x-icon3::before {
background-position: -72px 0px;

26 Sep 2012, 5:51 AM
Thanks for sharing how you accomplished something!