1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    15
    Vote Rating
    1
    blackswanny is on a distinguished road

      1  

    Default List items with custom icons

    List items with custom icons


    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
    Code:
                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++) {
                    data.push({
                                            /*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.
    Code:
        initConfig: function(config) {
            this.callParent(arguments);
            var iconsCls = this.getIconsCls();
            var iconsBaseCls = this.getIconsBaseCls();
            iconsBaseCls = iconsBaseCls ? iconsBaseCls : '';
            var itemTpl = new Ext.XTemplate(
                '<div class="' + iconsBaseCls +' {' + iconsCls + '}">' ,
                {' + ColumnName + '}</div>' 
            );
            this.setItemTpl(itemTpl);
        },
    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.
    Code:
    .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;
    }

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,642
    Vote Rating
    899
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Thanks for sharing how you accomplished something!
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

Thread Participants: 1