views:

3136

answers:

4

Hi

I have a problem when apply an Ext.ComboBox over an existing html select item, even if the existing content makes the html select about 20px (by it's content non static width is set), the Ext.ComboBox will resize to a sort of default, large, width value. There's a way to auto resize the Ext.ComboBox based on the existing items and no using the default width?

Even if I know which best tool Ext is, this issue will let my colleagues to discard Extjs.

Thanks in advance

A: 

I'm pretty sure you can get ExtJs to render whatever html items you need to, in the way you want them to be rendered.

here's some code from the Examples/Form/Combos.js file:

var converted = new Ext.form.ComboBox({
    typeAhead: true,
    triggerAction: 'all',
    transform:'state',
    width:20, //<-- set this config value!
    forceSelection:true
});

in the code that you're using to transform the combo, just specify a width for the ExtJs combo.

Joshua
As you say, Extjs just render whatever html items I need to, but my problem is that I don't want to set a fixed width, but let the extjs combo to autoresize as the html combo does. Imagine when you have an html select without a fixed width, and you have a value of "20", then the htaml select will resize to the with to contains the "20" value.. in other words, the html select will resize to the maximum width item in the list! I need this behavior
Hoghweed
+1  A: 

You can't technically make a combo "auto width" -- Ext actually converts the <select> into a regular <input> behind the scenes, and <input> elements have to have a width/size specified. However, you can trick Ext into sizing the combo based on the existing <select> which should give you the same end result. Here's an example from the Ext combo demo page, where I have modified the width config value:

var converted = new Ext.form.ComboBox({
    typeAhead: true,
    triggerAction: 'all',
    transform:'state',
    width: Ext.fly('state').getWidth(),
    forceSelection:true
});

The obvious caveat would be that if you subsequently modify the list after it's rendered, the combo will not resize itself automatically and you'd have to figure out a way to resize it yourself.

bmoeskau
Thanks for your suggestion, but I cannot understand why it's not possible to autosize the ext combo based on the normal behavior of the html select which autosize it taking the maximum element lenght as the width. I know the behavior of the Ext combo which is built upon an input element... The last thing, the Ext.fly().getWidth() what is it for?
Hoghweed
Because it is not a select -- it's that simple. The select has built in behavior from the browser that inputs do not need. Since Ext uses an input (and a fancy div structure for the dropdown menu) there is no concept of "auto width" -- it simply does not apply. So you have to approximate it. Did you try the code above?? The width setting sets the generated input to be the same width as the original select element.
bmoeskau
To clarify one point that you may have missed -- this code is from the existing combo demo page. 'state' is the id of an existing combo on that page -- you would replace that with the id of your own select that is being converted.
bmoeskau
So I take it that you tried the example and it did not work for you?
bmoeskau
I'm sorry but I cannot still try the code you suggested, my question was not about the 'state' which I understood it was the "wrapped" combo id, but about the Ext.fly function... I cannot try now but I think it will be good for me. When I have answers sure I will give the "right" answer point.
Hoghweed
Ext.fly is the same as Ext.get, it just uses a flyweight reference (see the docs). If this confuses you, just use Ext.get instead.
bmoeskau
A: 

In addition to what bmoeskau suggests, you can use an xtemplate for your combo's items. This will give you the ability to change the look of the item. You can wrap text, add images, etc.

A: 

add a listener to the afterrender event and set the width if the list (the div that drops down ) to auto e.g.

afterrender: function(combo){                   
                combo.list.setSize('auto', 0);
                combo.innerList.setSize('auto', 0);
            }

The reason I am using afterrender and not render is because if you set lazyInit to false it will set the list width, so in afterrender you override that setWidth

Will