-
7 Aug 2009 7:38 AM #1
FieldReplicator. A Field which keeps adding clones of itself until left blank.
FieldReplicator. A Field which keeps adding clones of itself until left blank.
Just like mailto addresses, sometimes you want multiple identical versions of an input field until the user has had enough, and leaves the last one blank.
This is a plugin to any Field which makes it self replicating. Blanking out any Field in the series deletes it (Unless it was the last one!)
Just configure the Field with
Code:plugins: [ Ext.ux.FieldReplicator ],
Code:Ext.ux.FieldReplicator = { init: function(f) { f.replicator = this; f.enableKeyEvents = true; f.on('change', this.onChange, this); f.onKeyDown = f.onKeyDown.createInterceptor(this.onKeyDown); }, // If tabbing out and the change event will be fired, flag that // the change handler must focus the correct sibling field. onKeyDown: function(e) { if ((e.getKey() == Ext.EventObject.TAB) && (String(this.startValue) !== String(this.getValue()))) { if (e.shiftKey) { this.focusPrev = true; } else { this.focusNext = true; } } }, // Handle the field either being changed to blank or from blank. onChange: function(f, n, o) { var c = f.ownerCt, l, ps = f.previousSibling(), ns = f.nextSibling(); if (Ext.isEmpty(n)) { if (!Ext.isEmpty(o)) { // The Field has been blanked, and it is not the only one left, remove it if ((ps && (ps.replicator === this)) || (ns && (ns.replicator === this))) { l = f.findParentBy(function(p) { return !Ext.isDefined(p.ownerCt); }); c.remove(f); l.doLayout(); } } } else { if (Ext.isEmpty(o)) { // Field filled, insert a clone as the next sibling ns = new f.constructor(f.cloneConfig()); c.insert(c.items.indexOf(f) + 1, ns); c.doLayout(); l = f.findParentBy(function(p) { return !Ext.isDefined(p.ownerCt); }); l.doLayout(); } } if (f.focusPrev) { delete f.focusPrev; ps.focus(false, true); } else if (f.focusNext) { delete f.focusNext; ns.focus(false, true); } } };Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
7 Aug 2009 8:15 AM #2
Very nice, I like how you've added this behavior via a plugin.
Aaron Conran
@aconran
Sencha Architect Development Team
-
7 Aug 2009 9:36 AM #3
i like

p.s. would it be a good idea to delete the id from the cloned Field config tp prevent possible conflicts?
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
7 Aug 2009 9:40 AM #4Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
7 Aug 2009 9:45 AM #5
Component.cloneConfig generates a new ID, so all will be well with the original code.
I'll add some extra stuff to the cloneConfig documentation to make it clearer.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
7 Aug 2009 9:47 AM #6
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
7 Aug 2009 9:48 AM #7
spotted one more thing:
Code:if (Ext.isEmpty(n)) { if (!Ext.isEmpty(o)) { // ... ... ... } } else { if (Ext.isEmpty(o)) { // ... ... ... } }
in the interests of efficiency, might i suggest the following modifications:
Code:if (!n && o) { // ... ... ... } else if (!o) { // ... ... ... }Last edited by mystix; 7 Aug 2009 at 9:51 AM. Reason: edit
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
7 Aug 2009 9:54 AM #8
i realise my suggested mod allows empty arrays, but all other bases are covered
(does anyone set arrays into Fields?)
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
7 Aug 2009 10:07 AM #9
The getValue() values are whatever the Field returns, so that could be anything.
I think we should allow people to write Fields which can return any kind of Object.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
7 Aug 2009 10:11 AM #10
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )


Reply With Quote