-
14 Mar 2009 12:02 PM #1
Dynamic form fields : cloning fields and fieldsets on the fly
Dynamic form fields : cloning fields and fieldsets on the fly
Ever wanted a form to be a bit more dynamic.
Like being able to add a field or fieldset more than once.
This is how you do it :
All type of fields are supported :
Code:{ fieldLabel: 'First Name', name: 'first', labelSeparator : '', width: 230, allowBlank:false, dynamic:true, maxOccurs:5, listeners : { 'maxoccurs' : {fn: function(field) { Ext.example.msg('maxoccurs', 'Implement behaviour'); }}} }
fieldsets are supported :
Even nested fields of fieldsets are supported!Code:{ xtype : 'fieldset', dynamic : true, maxOccurs:5, listeners : { 'maxoccurs' : {fn: function(field) {Ext.example.msg('maxoccurs', 'Implement behaviour');}, items :... .. }
Note the current amount of clones can also be set programmatically.
This will add/remove clones untill the specified amount is reached.
Additional features :Code:fieldSet.clones(2); person.getForm().findField('state').clones(1); person.doLayout();
Especially for large and dynamic forms the property nameSpace is introduced.
The property nameSpace set on a field or fieldset will bound the component to a named collection
and therefore allow a more fine grained control.
This will allow you to populate and extract values of fields, fieldsets, dynamic fields and dynamic fieldsets of a given nameSpace :Code:{ fieldLabel: 'First Name', name: 'first', nameSpace:'person' labelSeparator : '', width: 230, allowBlank:false, dynamic:true, maxOccurs:5, listeners : { 'maxoccurs' : {fn: function(field) { Ext.example.msg('maxoccurs', 'Implement behaviour'); }}} }
Code:person.populate(Ext.decode('{"state":["Netherlands","Delaware"]}'),'location','field'); person.populate(Ext.decode('[{"first":["Adriaan","Cornelis"],"last":"Zaanen"},{"first":["Bill"],"last":"Joy"}]'),'person','fieldset'); person.populate(Ext.decode('{"birthDate":"03/12/2009"}'),'person','field');To get this up and running just unzip the attached demo in your examples folder of extjs.Code:var location = person.extract('location','field'); var fsperson = person.extract('person','fieldset'); var fperson = person.extract('person','field');
And that's it. See the attached image for a sneak preview of the attached demo.
Have fun!
14032009 : initial, introduced dynamic field by overriding Ext.form.Field
15032009 : introduced dynamic fieldset by overriding Ext.form.FieldSet
16032009 : allowed programmer to set amount of field and fieldset clones
20032009 : introduced namespaces
20032009 : introduced extracting and populating of namespaced fields and fieldsets
22032009 : documented features
23032009 : allowed programmer to retrieve current set of clones of a given field or fieldset
-
15 Mar 2009 8:38 AM #2
-
16 Mar 2009 12:01 PM #3
How i can add a fieldset more than once on load form?
-
16 Mar 2009 1:15 PM #4
Thanks for your feedback. Both fields and fieldsets can be cloned.
You can set the current amount of clones by :
Download the attached demo to acquire the latest version of the code.Code:fieldSet.clones(2); person.getForm().findField('state').clones(1); person.doLayout();
-
17 Mar 2009 6:49 AM #5
Thanx for plugin. I have problem with cloning FormPanel - i think that in your example it does not work.
-
17 Mar 2009 6:57 AM #6
cloning fields and fieldsets are supported and tested on both IE and FF
cloning a panel is not supported
-
17 Mar 2009 7:07 AM #7
Thx for fast reply. My bad ... but i have another problem. In your example tabs don't render on start (in my ext) - i must resize browser window, and than it's ok. Now, I change your tabs to:
Code:... fieldLabel: 'First Name', name: 'resource/first', id:'test' ...
Code:var tabs = new Ext.TabPanel({ width:450, activeTab: 0, deferredRender:false, layoutOnTabChange:true, defaults:{autoHeight:true,hideMode:'offsets'}, items: [ {title:'Person',id: 'person' ,items: [person]}, {title:'Company',id: 'company' ,items: [company]} ] });but the problem still exists - i must resize window and than cloning is ok. Any ideas?Code:Ext.getCmp("test").clones(4); Ext.getCmp("test").doLayout();
-
17 Mar 2009 7:10 AM #8
Ok - i found solution. Do layout must be done of course on tabs.
-
17 Mar 2009 7:25 AM #9
Last req: a use your plugin on form with a lot of fields - now i haven't any idea how i can auto cloning and load variables with:
Please, help me with this. I think that this possibility will be best for this plugin and make me (and people) happyCode:Ext.getCmp('mainForm').getForm().load({url:'form/get_customers',params:{id:id}});
-
20 Mar 2009 1:33 PM #10
Especially for large and dynamic forms the property nameSpace is introduced.
The property nameSpace set on a field or fieldset will bound the component to a named collection,
as part of the total set of fields and fieldsets of the form, and therefore introduce a more fine grained control.
This will allow you to populate and extract values of fields, fieldsets, dynamic fields and dynamic fieldsets of a given nameSpace :Code:{ fieldLabel: 'First Name', name: 'first', nameSpace:'person' labelSeparator : '', width: 230, allowBlank:false, dynamic:true, maxOccurs:5, listeners : { 'maxoccurs' : {fn: function(field) { Ext.example.msg('maxoccurs', 'Implement behaviour'); }}} }
Code:person.populate(Ext.decode('{"state":["Netherlands","Delaware"]}'),'location','field'); person.populate(Ext.decode('[{"first":["Adriaan","Cornelis"],"last":"Zaanen"},{"first":["Bill"],"last":"Joy"}]'),'person','fieldset'); person.populate(Ext.decode('{"birthDate":"03/12/2009"}'),'person','field');Code:var location = person.extract('location','field'); var fsperson = person.extract('person','fieldset'); var fperson = person.extract('person','field');


Reply With Quote