Baron
Baron

Reputation: 41

Extjs 6.2.0 modern filefield as button

I am trying to create a filefield as a button with an icon. It seems in the documentation that this is not supported in modern 6.2.0. Is there a way?

Link to Docs: http://docs.sencha.com/extjs/6.2.0/modern/Ext.field.File.html

There also doesn't seem to be a way to change the text in the default button or text accompanying it.

Upvotes: 1

Views: 1600

Answers (2)

Hasan
Hasan

Reputation: 316

there is a good solution for that:

{
        xtype : 'button',
        text : 'add a file',
        handler : 'onAddFile'
},

and then in the controller:

onAddfile: function() {
    var me = this;

    var fileupload = Ext.create('Ext.form.Panel', {
        // renderTo: Ext.getBody(),
        title : 'Upload Panel',
        height : 0,
        width : 0,
        items : [ {
            xtype : 'filefield',
            label : 'any thing',
            accept: 'application/zip',//or mediatypes that you want
            multiple : false,//or true if you need
            controller : me,
            listeners : {
                change : function(field) {
                    me.fileSelected(field);
                }
            }
        } ]
    });
    //this is important to click programmaticallly
    fileupload.element.dom.querySelector('.x-button-el').click();

},

fileSelected: function (field) {
        let files = field.getFiles();
        // now you have the files
}

Thats it...

Upvotes: 1

Mastacheata
Mastacheata

Reputation: 2061

That is by design. The file input's text is browser defined and not meant to be changed by the developer. Usually people work around that by generating a display:hidden file input and a generic button which triggers the file input via JS.

I fear you'll have to divert to similar measures in ExtJS.

For reference here's a discussion on SO about how to change the label of the plain-old HTML file input element: Labeling file upload button

Upvotes: 0

Related Questions