调整Ext.form.ComboBox的大小以适应其内容

时间:2010-04-27 18:07:13

标签: combobox extjs resize autosize

在Ext论坛上有很少的解决方案,但我无法让它们中的任何一个工作。我似乎错过了一些小事。

我需要调整组合框的大小以适应其首次创建时的内容。当内容发生变化时,我不需要担心调整大小。

是否有使用Extjs 3.2的工作示例?

当前代码:

var store = new Ext.data.ArrayStore({
    fields: ['view', 'value', 'defaultselect'],
    data: Ext.configdata.dataCP
});

comboCPU = new Ext.form.ComboBox({
    tpl: '<tpl for="."><div class="x-combo-list-item"><b>{view}</b><br /></div></tpl>',
    store: store,
    displayField: 'view',
    width: 600,
    typeAhead: true,
    forceSelection: true,
    mode: 'local',
    triggerAction: 'all',
    editable: false,
    emptyText: 'empty text',
    selectOnFocus: true,
    listeners: { select: AdjustPrice, change: AdjustPrice, beforeselect: function (combo, record, index) { return ('false' == record.data.disabled); } },
    applyTo: 'confelement'
});

我也尝试删除宽度:600并用minListWidth:600替换它,但结果跟随并没有解决问题。 alt text http://img28.imageshack.us/img28/7665/4272010105134am.png

4 个答案:

答案 0 :(得分:1)

尝试以下方法:

  1. 确定包含最多字符的列表框选项
  2. 创建一个div并使用最多字符设置选项
  3. 将此div添加到正文
  4. 获取div的clientWidth
  5. 以下代码适用于ExtJs 3.2!

    myStore = new Ext.data.Store({
     ...
    listeners : {
                load: function() {
                    var maxValue = "";
                    var charLen = 0, maxCharLen = 0;
                    var maxListWidth = 300;
    
                    /**
                     * This is a work-around since the 'listWidth' property
                     * does not accept any values that would simulate auto-resize
                     * in reference to the category with the most characters.
                     */
                    this.data.each(function(item, index, totalItems ) {
                        var nameValue = item.data['name']; // 'name' is the field name
    
                        if(nameValue == null || nameValue == ''){
                            // do nothing
                        }else {
                            charLen = nameValue.length;
                            if(charLen > maxCharLen){
                                maxCharLen = charLen;
                                maxValue = nameValue;
                            }
                        }
                    });
    
                    if(maxValue != ""){
                        var divEl = document.createElement('div');
                        divEl.id = 'tempEl';
                        divEl.style.display = "inline";
                        divEl.className = "x-combo-list";
                        divEl.innerHTML = maxValue;
    
                        document.body.appendChild(divEl);
    
                        // check if appended
                        divEl = document.getElementById('tempEl');
                        if(divEl) {
                            var divWidth = divEl.clientWidth;
                            if(divWidth == 0 ) {
                                divEl.style.display = "inline-block";
                                divWidth = divEl.clientWidth;
                            }
    
                            // the allocated width for the scrollbar at side of the list box
                            var scrollbarWidth = 30;
    
                            // make sure that column width is not greater than
                            if((divWidth + scrollbarWidth) > maxListWidth) {
                                maxListWidth = divWidth + scrollbarWidth;
                                myCombo.listWidth = maxListWidth; 
                            }
                            document.body.removeChild(divEl);
                        }
                    }
                }
    });
    
    var myCombo = new fm.ComboBox({
     ...
    });
    

答案 1 :(得分:0)

试 autoWidth:true

并删除宽度参数

答案 2 :(得分:0)

width: 600是正确的,因此您必须处理其他问题,这与您发布的内容并不明显。您可以尝试删除applyTo配置,而只是将renderTo: Ext.getBody()放入,以查看它是否与如何应用于您的元素有关。你确定没有其他代码可能会影响宽度吗?

答案 3 :(得分:0)

找到here

// throw this stuff in a closure to prevent
// polluting the global namespace
(function(){

    var originalOnLoad = Ext.form.ComboBox.prototype.onLoad;
    Ext.form.ComboBox.prototype.onLoad = function(){
        var padding = 8;
        var ret = originalOnLoad.apply(this,arguments);
        var max = Math.max(this.minListWidth || 0, this.el.getWidth());
        var fw = false;
        Ext.each(this.view.getNodes(), function(node){
            if(!fw){ fw = Ext.fly(node).getFrameWidth('lr'); }
            if(node.scrollWidth){ max = Math.max(max,node.scrollWidth+padding); }
        });
        if( max > 0 && max-fw != this.list.getWidth(true) ){
            this.list.setWidth(max);
            this.innerList.setWidth(max - this.list.getFrameWidth('lr'));
        }
        return ret;
    };

})();