1. #1
    Sencha - Community Support Team grgur's Avatar
    Join Date
    Aug 2007
    Location
    Split, Croatia
    Posts
    133
    Vote Rating
    14
    grgur will become famous soon enough grgur will become famous soon enough

      0  

    Default Replace component function

    Replace component function


    In case you need to replace a component with new one, here's the addition to AbstractContainer that will allow such action:

    Code:
    Ext.override(Ext.AbstractComponent, {
        replace: function(newCmp) {
            var 	oldCmp = this, 
    		parent = oldCmp.up(), 	//find the parent component
    		index;
    				
    	//find the exact position of the component that needs to be replaced
            parent.items.each(function(c,i) {if (c==oldCmp) index=i;});
    		
    	//remove the sucker
    	parent.remove(oldCmp, true);
    		
    	//add the new one
            newCmp = parent.insert(index, newCmp);
            
    	//refresh layout
            parent.doLayout();
    
    	//return the newly created component
            return newCmp;
        }
    });
    All you need to do is find the component you want to replace and - replace it:
    Code:
    Ext.getCmp('iNeedToBeReplaced').replace({xtype: 'panel',html:'I just replaced that old component'});
    Last edited by grgur; 26 May 2011 at 9:36 PM. Reason: returned cmp
    Grgur Grisogono
    Ext JS in Action SE co-author
    Exercising awesomeness at Modus Create - Official Sencha Partner

    Get in touch for Sencha Touch Training
    @ggrgur