最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - set combo box width to auto fit it's longest field option text in ExtJS - Stack Overflow

matteradmin4PV0评论

How to set bo box width to auto fit it's longest field option text ?

Say, I have bo box field having width 300 ..But if from that bo there is a field option having some long text - bigger than 300px.

I want to make my bo box width auto fit to that long field option to see entire selected field option in Combo without any cutting.

Actually, I forgot there is config which do same thing for bo in extjs.

I think in ExtJS 4, bo width was getting set to auto fit for longest field option.

I don't want to do it by dynamically adding code to auto resize it.

How to set bo box width to auto fit it's longest field option text ?

Say, I have bo box field having width 300 ..But if from that bo there is a field option having some long text - bigger than 300px.

I want to make my bo box width auto fit to that long field option to see entire selected field option in Combo without any cutting.

Actually, I forgot there is config which do same thing for bo in extjs.

I think in ExtJS 4, bo width was getting set to auto fit for longest field option.

I don't want to do it by dynamically adding code to auto resize it.

Share Improve this question asked Dec 14, 2015 at 10:49 Abhijit MukeAbhijit Muke 1,2143 gold badges17 silver badges41 bronze badges 1
  • Questions seeking code help must include the shortest code necessary to reproduce it in the question itself. See How to create a Minimal, Complete, and Verifiable example – Paulie_D Commented Dec 14, 2015 at 11:11
Add a ment  | 

2 Answers 2

Reset to default 4

No need to add any manual code..Don't give width ..

grow : true,
growToLongestValue : true

Work for me in ExtJS 6 too..

Ext.create('Ext.form.ComboBox', {
       fieldLabel : 'Field',
       store : ['a', 'b', 'c', 'Long long long long long long long long'],
       grow : true,
       growToLongestValue : true,
       renderTo : Ext.getBody()
});

Check Sencha Fiddle https://fiddle.sencha./#fiddle/12jc

You would have to do it manually; it is not available in the Ext code. Something like this:

listeners: {
    afterrender: function(box) {
        var width = 0,
           metrics = new Ext.util.TextMetrics();
        box.getStore().each(function(rec) {
            var recWidth = textMetrics.getWidth(rec.get(box.displayField));
            width = Math.max(width,recWidth);
        });
        box.setWidth(width);
    }
}

Without any testing or warranty, but you should get an idea how to solve it.

A detailed example how to use TextMetrics can be found in the docs.

Post a comment

comment list (0)

  1. No comments so far