如何在选项卡的标题中放置组合框?

时间:2013-09-03 10:02:25

标签: extjs combobox extjs3 tabpanel

是否可以在标签的标题中显示组合框(更好的是,如果它将是extjs组合)?

我创建了an example on jsfiddle但是存在这样的问题:
1.无法显示组合选项列表(鼠标单击不起作用)
2.标签的高度不正确(我想这可以通过将高度应用于组合来修复)。

示例代码:

new Ext.TabPanel({
    renderTo: 'tab-with-combo',
    activeTab: 0,
    items:[
        {
            title: 'First tab ' +
                '<select>' +
                    '<option>Option 1</option>' +
                    '<option>Option 2</option>' +
                '</select>'},
        {title: 'Second tab'}
    ]
});

2 个答案:

答案 0 :(得分:3)

回答你的问题:

  1. Tabpanel组件preventDefault点击标签处的事件,因此我们需要修改onStripMouseDown方法。

  2. 如果我们在title属性中定义一些内容,那么extjs会将它放在具有特殊样式的span.x-tab-strip-text html元素中。所以我们需要在选项卡中将此元素后面的组合框附加。

  3. 代码:

    new Ext.TabPanel({
        renderTo : 'tab-with-combo',
        activeTab : 0,
        items : [{
                title : 'First tab ',
                listeners : {
                    afterrender : function (panel) {
                        //the needed tabEl
                        var tabEl = panel.findParentByType('tabpanel').getTabEl(panel);
                        var tabStripInner = Ext.fly(tabEl).child('span.x-tab-strip-inner', true);
                        //we need that for have the combobox in the same line as the text
                        Ext.fly(tabStripInner).child('span.x-tab-strip-text', true).style.float = 'left';
                        //create the combobox html element
                        var combo = document.createElement("select");
                        var opt1 = document.createElement("option");
                        opt1.innerHTML = 'Option 1';
                        var opt2 = document.createElement("option");
                        opt2.innerHTML = 'Option 2';
                        combo.appendChild(opt1);
                        combo.appendChild(opt2);
                        combo.style.float = 'left';                 
                        //add the combobox to the tab
                        tabStripInner.appendChild(combo);
                    }
                }
            }, {
                title : 'Second tab'
            }
        ],
    
        //this is the tab's click handler
        onStripMouseDown : function (e) {
            if (e.button !== 0) {
                return;
            }
            var t = this.findTargets(e);
            if (t.close) {
                //preventDefault needed to move here
                e.preventDefault();
                if (t.item.fireEvent('beforeclose', t.item) !== false) {
                    t.item.fireEvent('close', t.item);
                    this.remove(t.item);
                }
                return;
            }
            if (t.item && t.item != this.activeTab) {
                this.setActiveTab(t.item);
            }
        },
    });

答案 1 :(得分:0)

感谢@ Alexander.Berg的回答,这个工作正常(例如here):

var comboId = Ext.id();
new Ext.TabPanel({
    cls: 'tab-panel-with-combo',
    renderTo: 'tab-with-combo',
    activeTab: 0,
    items:[
        {
            title: '<div class="tab-title" style="float: left; padding-right: 5px;">First tab with a long name</div> ' +
                   '<div style="float: right;" id="' + comboId + '" ></div>',
            closable: true,
            html: 'Content of the first tab'
        },
        {
            title: '<div class="tab-title">Second tab</div>',
            closable: true,
            html: 'Content of the second tab'
        }
    ],
    listeners: {
        afterrender: function() {
            var store = new Ext.data.ArrayStore({
                fields: ['abbr', 'case_name'],
                data : [
                    ['one',   'Case #1'],
                    ['two',   'Case #2'],
                    ['three', 'Case #3']
                ]
            });

            new Ext.form.ComboBox({
                store: store,
                displayField:'case_name',
                editable: false,
                typeAhead: false,
                selectOnFocus:false,
                mode: 'local',
                forceSelection: true,
                triggerAction: 'all',
                emptyText:'Select a case...',
                renderTo: comboId
            });
        }
    }
});