简体   繁体   中英

font-awesome icon in display of combobox extjs 6

I tried several ways to set an icon, in the displayfield, when an item of the combo is selected with not luck, this is the fiddle for anyone to want try to help with this. very much appreciated any light.

fiddle example

The only solution is to transform the input type combo in a div with this:

fieldSubTpl: [
                '<div class="{hiddenDataCls}" role="presentation"></div>',
                '<div id="{id}" type="{type}" style="background-color:white; font-size:1.1em; line-height: 2.1em;" ',
                '<tpl if="size">size="{size}" </tpl>',
                '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
                'class="{fieldCls} {typeCls}" autocomplete="off"></div>',
                '<div id="{cmpId}-triggerWrap" class="{triggerWrapCls}" role="presentation">',
                '{triggerEl}',
                '<div class="{clearCls}" role="presentation"></div>',
                '</div>', {
                    compiled: true,
                    disableFormats: true
                }
            ],

Override the setRawValue method of the combo like this:

 setRawValue: function (value) {
                var me = this;
                me.rawValue = value;

                // Some Field subclasses may not render an inputEl
                if (me.inputEl) {
                    // me.inputEl.dom.value = value;
                    // use innerHTML
                    me.inputEl.dom.innerHTML = value;
                }
                return value;
            },

and style your fake combo div like you want.

Thats because an input on HTML can't have HTML like value inside it.

Keep attenction, the get Value method will return you the HTML inside the div, and maybe you should also override it, but thats the only one method.

You will be able to get the selected value with this method:

Ext.fly(combo.getId()+'-inputEl').dom.innerHTML.replace(/<(.|\n)*?>/gm, '');

If I were you I would like to do something like this:

combo.getMyValue();

So add this property to your combo:

getMyValue:function(){
            var combo=this;
            if(Ext.fly(combo.id+'-inputEl'))
            return Ext.fly(combo.id+'-inputEl').dom.innerHTML.replace(/<(.|\n)*?>/gm, '');
        },

Here is a working fiddle

Perhaps my solution is similar to a hack, but it works in 6.7.0 and is a bit simpler. Tested in Chrome. Theme - Material. For another theme will require minor improvements.

Sencha Fiddle live example

预习

Ext.application({
name: 'Fiddle',

launch: function () {

    var store = new Ext.data.Store({
        fields: [{
            name: 'class',
            convert: function (value, model) {
                if (value && model) {
                    var name = value
                        .replace(/(-o-)|(-o$)/g, '-outlined-')
                        .replace(/-/g, ' ')
                        .slice(3)
                        .trim();
                    model.data.name = name.charAt(0).toUpperCase() + name.slice(1);
                    return value;
                }
            }
        }, {
            name: 'name'
        }],
        data: [{
            class: 'fa-address-book'
        }, {
            class: 'fa-address-book-o'
        }, {
            class: 'fa-address-card'
        }]
    });

    var form = Ext.create('Ext.form.Panel', {
        fullscreen: true,
        referenceHolder: true,
        items: [{
            xtype: 'combobox',
            id: 'iconcombo',
            queryMode: 'local',
            editable: false,
            width: 300,
            valueField: 'class',
            displayField: 'name',
            store: store,
            itemTpl: '<div><i class="fa {class}"></i> {name}</div>',
            afterRender: () => {
                var component = Ext.getCmp('iconcombo');
                var element = document.createElement('div');
                element.className = 'x-input-el';
                element.addEventListener('click', () => component.expand());
                component.inputElement.parent().dom.prepend(element);
                component.inputElement.hide();
                component.addListener(
                    'change', (me, newValue, oldValue) => {
                        component.updateInputValue.call(me, newValue, oldValue);
                    },
                    component
                );
                var method = component.updateInputValue;
                component.updateInputValue = (value, oldValue) => {
                    method.call(component, value, oldValue);
                    var selection = component.getSelection();
                    if (selection) {
                        element.innerHTML =
                            '<div><i class="fa ' + selection.get('class') + '"></i> ' + selection.get('name') + '</div>';
                    }
                };
            }
        }, {
            xtype: 'button',
            text: 'getValue',
            margin: '30 0 0 0',
            handler: function (component) {
                var combo = Ext.getCmp('iconcombo');
                alert(combo.getValue());
            }
        }]
    });
    form.show();

}

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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