"Utilizing a Font Awesome icon within the dropdown list of a combobox in Ext JS

I have attempted multiple methods to add an icon in the displayfield when a combo box item is selected without success. Here is the fiddle link for anyone who would like to help me with this issue. Your assistance is greatly appreciated.

View the example on Fiddle

Answer №1

If you want to transform the input type combo into a div, follow these steps:

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
            }
        ],

To override the setRawValue method of the combo, use this code:

 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;
        },

You can style your fake combo div as desired.

This approach is necessary because an input on HTML cannot have HTML content inside it.

Be aware that the getValue method will return the HTML inside the div. Consider overriding it if needed, but this is the only required method.

To get the selected value, use the following method:

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

Consider creating a method like this for easier access:

combo.getMyValue();

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

Answer №2

My solution may seem like a hack, but it does work in version 6.7.0 and is simpler to implement. It has been tested on Chrome using the Material theme. Minor adjustments may be needed for other themes.

Check out the live example on Sencha Fiddle

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();

}

});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to include overflow-hidden in the image displayed by the tailblock component?

I have implemented the following Tailblock component, which is built on tailwind.css. My goal is to achieve a hover effect where the image scales up within its original dimensions. I want the image to zoom in without changing its height and width. I atte ...

Let's discuss how to include the scrollTop option

I am new to coding and I need help adding a scrollTop margin of +100px to my code. The page already has some top margin but I can't seem to locate it. I'm also having trouble finding where the margin-top is being set in the JavaScript file. /*** ...

Ways to remove scroll bars from a textarea in JavaFX

Is there a way to disable the scrollbars on a TextArea element? I managed to remove the horizontal scrollbar by using: TextArea.setWrapText(true); However, I am struggling to disable the vertical scrollbar - all I can do is hide it. My goal is to hav ...

Continuous horizontal columns

Is there a way to create horizontal columns with inline-blocks, like the method described here, without having vertical gaps between items on the second line due to different heights? I want to eliminate the vertical gaps between the tiles using only CSS. ...

Adjusting and arranging multiple thumbnail images within a container of specific width and height

I need a user-friendly method to achieve the following. The thumbnails will not be cropped, but their container will maintain a consistent height and width. The goal is for larger images to scale down responsively within the container, while smaller image ...

Achieving equal height for the englobing/parent div and the inner divs

To achieve a standard left, middle, and right column layout, it is necessary for me to enclose various inner divs of different heights within a surrounding/parent div. It is crucial that the parent div adjusts its height to match the tallest inner div, whi ...

Troubleshooting code: JavaScript not functioning properly with CSS

I am attempting to create a vertical header using JavaScript, CSS, and HTML. However, I am facing an issue with the header height not dynamically adjusting. I believe there might be an error in how I am calling JSS. Code : <style> table, tr, td, t ...

What could be the reason for the absence of margin on the top and bottom

.separator { border: 1px solid #000000; margin: 10px; } <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>Separator</title> </head> <body> &l ...

Increasing the size of the .ui-btn-left component

Seeking advice on a Javascript query that I can't seem to resolve online. As a beginner in Javascript, I'm struggling with what seems like a simple issue. My dilemma lies in enlarging the close button on a jQuery page. .ui-dialog .ui-header .ui- ...

Incorporating a hamburger symbol into an established menu for easy navigation

I have come across a tutorial on creating a navigation menu with a logo positioned to the left. However, I now wish to incorporate a hamburger icon for mobile devices and I am unsure about the process. Despite my efforts to find a suitable tutorial onlin ...

The background image fails to display on the button

Why isn't the background image for my button appearing when set using a CSS rule? I have a button with the class "todo" and although other parts of the rule work, the image itself does not show up even after trying "background: url" and "background-im ...

Menu stack with content displayed to the right on larger screens

Given the content div's position (up middle), what method can I use to stack the divs in this manner? Is it possible to accomplish with only CSS or is jQuery necessary to move the div content out on larger screens? ...

Screen proportions altered - Background image disappearing | Unusual occurrences |

If you look at my site everything seems to be ok, ... untill you minimize the website. A horizontal scrollbar appears and when you use that scrollbar, you will see that my image has vanished. My Website I made that image responsive...so i have no idea wh ...

How can I keep a fixed position element from shifting to the left when resizing the window?

Is there a solution to prevent the logo element from shifting to the left when resizing the window, despite its fixed position? #logo { top:20px; left:50%; margin-left:-177.6px; width:355.2px; height:148.8px; ...

Need to know how to retrieve the li element in a ul that does not have an index of 2? I am aware of how to obtain the index greater than or less

I'm looking to hide all the li elements except for the one with a specific index. I've written some code to achieve this, but I'm wondering if there's a simpler way using jQuery. While jQuery provides methods like eq, gt, and lt, there ...

A complete guide on utilizing *ngFor in Angular to display data rows

I am facing an issue with using *ngFor to create new "rows" for adding new categories dynamically. Although there are no errors displayed when I run the program, the intended functionality is not achieved. I have tried making some modifications but it see ...

How can I update a CSS class value by utilizing AngularJS variables?

I am currently facing an issue with making my CSS values dynamic. The code I have tried is not functioning as expected. <style> .panel-primary { background-color:{{myColorPanel}} } </style> I came across a similar query on Ho ...

``There seems to be a problem with the radio buttons where the selection disappears when

I'm facing an issue with a radio button group I created. It's functioning properly, but the selection disappears when clicked outside of the radio button. Can someone assist me in resolving this problem? Here is the link to the jsfiddle for refer ...

How can you stop text in a textarea from spilling over into the padding?

When I add padding to a textarea element and type more than four lines of content, the content overflows into the padding. Is there a way to prevent this? I have looked at the suggestions in this thread and this one (specifically for google-chrome), but t ...

Unable to change Bootstrap variables within a Next.js project

I'm having trouble changing the primary color in Bootstrap within my NextJS project. I've tried different methods but nothing seems to work as expected. Here is the code snippet from my "globals.scss" file that I imported in "_app.js": $primary: ...