最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Toolbar items addedinserted into Ext.ToolBar aren't showing on the page - Stack Overflow

programmeradmin0浏览0评论

I have a custom ponent that extends a panel. The panel has a top tool bar with a basic config. I want to be able to add items to the panel dynamically after it has been created.

Those items should always e at the end of the main set of menu items and before the filler that pushes the logout button to the far right, so I wrote the insertMenuItem function to grab the position of the "shunt" (the tbfill) item and insert the new toolbar item at this location. If it doesn't find the "shunt", because its been overridden, it just adds to the end of the toolbar.

This seems to work just great. If I look at the "live" contentPanel I can see the inserted menu item. The problem e with getting it to display. Now I suspect that this is a scoping issue, I'm just not sure what, where, when, how.... :|

I've tried this.doLayout(), topToolbar.doLayout() in the function I've got a doLayout() on the contentPanel object after calling my function, but none of them seem to help.

HELP! ;-D

Below is my extended Panel class. Thanks in advance for your help

Stephen


SOM.ux.contentPanel = Ext.extend(Ext.Panel, {
     autoScroll:true
    ,initComponent:function() {
        var config = {
             plugins:['dispatcher']
            ,contentEl : Ext.get('som-body')
            ,tbar:{
                     itemId:'contenttoolbar'
                    ,items:[
                {
                    xtype : 'button',
                    text: 'Dashboard',
                    handler: function(){
                        document.location.href = '/'
                    }
                },{itemId:'shunt',xtype: 'tbfill'},{
                    xtype : 'button',
                    text: 'Logout',
                    handler: function(){
                        document.location.href = '/admin.login.doLogout'
                    }
                },{
                        xtype : 'tbbutton',
                        text: 'Message',
                        scope: this,
                        handler: function(){
                            this.publish('SOM.ux.Dashboard',{action:'doHelloWorld',params:{name:'Stephen'}});
                        }
                    }
                ]
            }

        }; // end of config
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        SOM.ux.contentPanel.superclass.initComponent.apply(this, arguments);
    }
    ,afterRender: function(){
        this.subscribe('SOM.ux.Toolbar');
        SOM.ux.contentPanel.superclass.afterRender.call(this);
    }
    ,onDispatchMessage: function(subject,message) {
        if (message.action) {
            this[message.action].call(this,message.params);
        }       
        console.log('contentpanel doDispatch',subject,message);
    }
    ,insertMenuItem: function(itemObj){
        var topToolbar = this.getTopToolbar();
        var aItems = topToolbar.items;
        var insertPos = aItems.indexOfKey('shunt');
        if (insertPos) {
            console.log('using insert');
            topToolbar.insert(insertPos,itemObj);
        } else {
            console.log('using add');
            topToolbar.add(itemObj);
        }
        this.getTopToolbar().doLayout();
    }

I have a custom ponent that extends a panel. The panel has a top tool bar with a basic config. I want to be able to add items to the panel dynamically after it has been created.

Those items should always e at the end of the main set of menu items and before the filler that pushes the logout button to the far right, so I wrote the insertMenuItem function to grab the position of the "shunt" (the tbfill) item and insert the new toolbar item at this location. If it doesn't find the "shunt", because its been overridden, it just adds to the end of the toolbar.

This seems to work just great. If I look at the "live" contentPanel I can see the inserted menu item. The problem e with getting it to display. Now I suspect that this is a scoping issue, I'm just not sure what, where, when, how.... :|

I've tried this.doLayout(), topToolbar.doLayout() in the function I've got a doLayout() on the contentPanel object after calling my function, but none of them seem to help.

HELP! ;-D

Below is my extended Panel class. Thanks in advance for your help

Stephen


SOM.ux.contentPanel = Ext.extend(Ext.Panel, {
     autoScroll:true
    ,initComponent:function() {
        var config = {
             plugins:['dispatcher']
            ,contentEl : Ext.get('som-body')
            ,tbar:{
                     itemId:'contenttoolbar'
                    ,items:[
                {
                    xtype : 'button',
                    text: 'Dashboard',
                    handler: function(){
                        document.location.href = '/'
                    }
                },{itemId:'shunt',xtype: 'tbfill'},{
                    xtype : 'button',
                    text: 'Logout',
                    handler: function(){
                        document.location.href = '/admin.login.doLogout'
                    }
                },{
                        xtype : 'tbbutton',
                        text: 'Message',
                        scope: this,
                        handler: function(){
                            this.publish('SOM.ux.Dashboard',{action:'doHelloWorld',params:{name:'Stephen'}});
                        }
                    }
                ]
            }

        }; // end of config
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        SOM.ux.contentPanel.superclass.initComponent.apply(this, arguments);
    }
    ,afterRender: function(){
        this.subscribe('SOM.ux.Toolbar');
        SOM.ux.contentPanel.superclass.afterRender.call(this);
    }
    ,onDispatchMessage: function(subject,message) {
        if (message.action) {
            this[message.action].call(this,message.params);
        }       
        console.log('contentpanel doDispatch',subject,message);
    }
    ,insertMenuItem: function(itemObj){
        var topToolbar = this.getTopToolbar();
        var aItems = topToolbar.items;
        var insertPos = aItems.indexOfKey('shunt');
        if (insertPos) {
            console.log('using insert');
            topToolbar.insert(insertPos,itemObj);
        } else {
            console.log('using add');
            topToolbar.add(itemObj);
        }
        this.getTopToolbar().doLayout();
    }

Share Improve this question edited Jan 22, 2010 at 1:47 Abie 10.8k6 gold badges34 silver badges39 bronze badges asked Jan 21, 2010 at 18:24 Stephen MorettiStephen Moretti 2,4381 gold badge18 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

The following works:

var tb = new Ext.Toolbar({
    items : [{text : 'wtf'}]
});

new Ext.Window({
    width : 300,
    height : 100,
    tbar : tb
}).show();

(function() {
    tb.add({text:'new btn'});
    tb.doLayout()
}).defer(1500);

To add to jonathan's ment, only call doLayout after it's rendered.

Make sure you are calling insertMenuItem after the toolbar has rendered. Is itemObj (the param) an Ext.Toolbar.Item config?

发布评论

评论列表(0)

  1. 暂无评论