-
7 Jul 2010 9:24 AM #1
How to replace placeholder components
How to replace placeholder components
While we are waiting for Ext Designer to incorporate the ability to use custom components, you may find this override useful. It replaces a component (a place holder component for example) with another...
FOR EXTJS3...
FOR EXTJS4 (Thanks JoelRSimpson)...PHP Code:/**
* Replaces a component with another, preserving the original config
* @param {Component} newComponent the new component to replace this one with
* @return {Component} this the replaced component
*/
Ext.override(Ext.Component, {
replaceWith : function(newComponent){
var ctnr = this.ownerCt;
// if the refOwner is a form or if newComponent.ownerForm config is set to the form
var form = newComponent.ownerForm ? newComponent.ownerForm : this.refOwner.form;
var i = ctnr.items.indexOf(this);
// apply initialConfig
Ext.applyIf(newComponent, this.initialConfig);
ctnr.remove(this, true);
var added = ctnr.insert(i, newComponent);
// if the component is in a form
if (form){
// remove the old component from the form
form.remove(this);
// add the new component to the form
form.add(added);
}
}
});
To use it, design your form in Ext Designer and add placeholder components where you want to include custom components. Give them an autoRef and export the project.PHP Code:/**
* Replaces a component with another, preserving the original config
* @param {Component} newComponent the new component to replace this one with
* @return {Component} this the replaced component
*/
Ext.override(Ext.Component, {
replaceWith : function(newComponent){
var ctnr = this.ownerCt;
// if the Owner container is a form or if newComponent.ownerForm config is set to the form
var form = newComponent.ownerForm ? newComponent.ownerForm : this.ownerCt.form;
var i = ctnr.items.indexOf(this);
// apply initialConfig
Ext.applyIf(newComponent, this.initialConfig);
ctnr.remove(this, true);
var added = ctnr.insert(i, newComponent);
// if the component is in a form
if (form){
// remove the old component from the form
form.remove(this);
// add the new component to the form
form.add(added);
}
}
});
Paste the above override into a JavaScript file and load it in your page along with any custom component files needed.
In the subclass initComponent method you can then do something like this...
In the example above my placeholder component had an autoRef of "tagsField" and I am replacing it with the rather groovy SuperBoxSelect custom component.PHP Code:this.tagsField.replaceWith({
xtype:'superboxselect',
mode: 'local',
displayField: 'tag',
displayFieldTpl: '{tag}',
valueField: 'tag',
queryDelay: 0,
triggerAction: 'all'
});
Note: thanks to Otávio's post below, the original config is preserved so you only need to specify the config needed by your new component.Last edited by froamer; 28 Sep 2011 at 9:50 PM. Reason: Added ExtJS 4 Support thanks to JoelRSimpson
-
8 Jul 2010 2:52 AM #2
Great job guy,
It's very useful. I have a suggestion on replaceWith method:
Cheers, and thank you for sharing.PHP Code:replaceWith : function(newComponent){
var ctnr = this.ownerCt;
// if the refOwner is a form or if newComponent.ownerForm config is seted get the form
var form = newComponent.ownerForm ? newComponent.ownerForm : this.refOwner.form;
var i = ctnr.items.indexOf(this);
// apply initialConfig, thus not need rewrite config on "this.tagsField.replaceWith({xtype: 'superboxselect'})"
Ext.applyIf(newComponent, this.initialConfig);
ctnr.remove(this, true);
var added = ctnr.insert(i, newComponent);
// if the component is in a form
if (form){
// remove the old component to the form reference
form.remove(this);
// add the new component to the form reference to setValue work fine.
form.add(added);
}
}
Otávio Augusto Rodrigues Fernandes
Net On - Soluções Tecnológicas Ltda
Desenvolvimento e Consultoria em ExtJS
www.neton.com.br
otavio@neton.com.br
(31) 3075-7868
-
8 Jul 2010 6:36 AM #3
Hi Otávio,
Many thanks for taking this to the next logical step, preservation of the original config is a great improvement. I have tested this in my code and it works well. I have adjusted my original post so people reading this will get your tweaks.
-
8 Jul 2010 12:00 PM #4Sencha - Desktop Packager Dev Team
- Join Date
- Mar 2007
- Location
- Baltimore, MD.
- Posts
- 1,745
- Vote Rating
- 6
Very nice froamer and Otávio. A great workaround for UX components!
-
27 Jul 2010 11:26 AM #5
-
27 Jul 2010 5:04 PM #6
very nice Override.
Wemerson Januario
Skype: wemerson.januario
Email: wemerson.januario@gmail.com
Fone: 62 84101145 - Goiânia-GO- Brazil
Consulting and Training Ext JS
Projects: (Nubes ERP)
-
12 Sep 2010 2:59 AM #7
Inside a FormPanel this technique only seems to work if the component that replaces the placeholder has a clearInvalid() method. This is the case for all Field components and its subcomponents.
I tried to replace the placeholder with an Ext.Container that contains other field subcomponents and I´ve got the error "f.clearInvalid is not a function" for the container component. Also the replacement with a ReCaptcha component (see http://www.sencha.com/forum/showthre...HA-Form-Plugin) didn´t work. I had to add the clearInvalid() method to the ReCaptcha component.
If I replace the placeholder with a composite field everything works fine.
:-((
-
13 Sep 2010 7:29 PM #8Sencha - Desktop Packager Dev Team
- Join Date
- Mar 2007
- Location
- Baltimore, MD.
- Posts
- 1,745
- Vote Rating
- 6
I think the FormPanel will register all Field child items within itself when it's initializing. It uses that enumeration of Fields for rolling through validations, etc. You can put a placeholder of a Container and should be able to replace it with another Container no problem...if you use a Field placeholder, you're best off replacing it with another Field component.
-
27 Nov 2010 6:40 AM #9
Issue creating multiple Instances of a a component
Issue creating multiple Instances of a a component
Hi,
I am having an issue with using the superselectbox ux in a component that was built using the Ext Designer.
I have used the replaceWith override and a textfield placeholder. I "link" my form component into my "Edit existing item" page and I have a window that uses another "linked" form for my "New" page. Pretty straight forward.
The edit page gets rendered first. When I click the New button to show the New window, an additional control seem to be displayed on the edit form and 2 new labels are displayed on the new form but no control. This may be a bit confusing so I have included a link to some screenshots to help explain. (I tried linking them directly in the post but I couldn't them to show up)
The first screenshot is when the first page loads and the second is when the window is shown.
http://picasaweb.google.com/lesliefa...entScreenshots
Thanks for you help!
-
29 Nov 2010 9:32 AM #10Sencha - Desktop Packager Dev Team
- Join Date
- Mar 2007
- Location
- Baltimore, MD.
- Posts
- 1,745
- Vote Rating
- 6
Hi fargs,
So you are using the same form inside of two different containers, correct? Are you using the same "instance" or creating a new instance of the form per Window? Doesn't it sound like the Window is using a different instance of the form. If you provide some code samples, I could probably pinpoint the issue.
Thanks,
Jarred
Similar Threads
-
[CLOSED][3.0.3] Corrupt placeholder-bar
By defcon1 in forum Ext 3.x: BugsReplies: 6Last Post: 17 Nov 2009, 2:11 AM -
Image placeholder...
By jollyca in forum Ext 2.x: Help & DiscussionReplies: 0Last Post: 28 Aug 2008, 6:44 AM -
[Ext 2.0] Best/Supported way to replace() components
By JEBriggs in forum Ext 2.x: Help & DiscussionReplies: 5Last Post: 21 Feb 2008, 10:38 AM -
[Ext 2.0] Best/Supported way to replace() components
By JEBriggs in forum Ext 1.x: Help & DiscussionReplies: 0Last Post: 23 Aug 2007, 12:30 PM -
How to customize the placeholder
By joey in forum Ext 1.x: Help & DiscussionReplies: 4Last Post: 14 Jul 2007, 4:57 AM


Reply With Quote