-
22 Jan 2010 12:04 AM #1
add dynamic fields on the fly
add dynamic fields on the fly
Im a bit stuck on adding fields dynamically to a formpanel using a href link. Any suggestions on how this is done. here is my code.
Code:var test = new Ext.FormPanel({ labelWidth: 120, frame:true, autoWidth:true, items: [{ xtype:'fieldset', title: '', autoHeight:true, defaults: {width: 110}, defaultType: 'textfield', collapsible:true, items :[{ fieldLabel: 'Top level menu', name: 'top_menu', allowBlank:false },{ fieldLabel: 'Description', name: 'top_description', xtype:'textarea' },{ fieldLabel: 'Add +', xtype:'box', autoEl: { tag: 'a', href: 'javascript:test()', // need this to add the two fields again below the current ones onclick html: 'click me' } } ] }], buttons: [{ text: 'Save', handler:function(){ } },{ text: 'Cancel' }] });
-
22 Jan 2010 12:38 AM #2
Hi!
I think the best way to render components dinamically is adding them into a panel.
First of all, you have to define a panel without items.
After that,Code:{ xtype: 'panel', id: 'yourID', layout: 'form' }
I hope it helpsCode:var aTextField = new .....; Ext.getCmp('yourID').add(aTextField); Ext.getCmp('yourID').doLayout();
.
-
22 Jan 2010 12:41 AM #3
thanks for your reply. I will give that a try and see how it works out.
-
22 Jan 2010 12:44 AM #4
He already has a Panel!
Use a Button with a handler.
Code:var test = new Ext.FormPanel({ labelWidth: 120, frame:true, autoWidth:true, items: [{ xtype:'fieldset', ref: 'fs', title: '', autoHeight:true, defaults: {width: 110}, defaultType: 'textfield', collapsible:true, items :[{ fieldLabel: 'Top level menu', name: 'top_menu', allowBlank:false },{ fieldLabel: 'Description', name: 'top_description', xtype:'textarea' },{ xtype:'button', text: 'Add field', handler: function() { var p = test.fs.items.items.length - 1; test.fs.insert(p, { xtype: 'textfield', fieldLabel: 'Added field' }); test.fs.doLayout(); } }] }], buttons: [{ text: 'Save', handler:function(){ } },{ text: 'Cancel' }] });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
-
22 Jan 2010 12:57 AM #5
Yes, it's true. That's the better way
.
-
22 Jan 2010 2:12 AM #6
Thanks animal that works well. Would that ref:fs add both the fields instead of just the textfield and not the textarea?
-
22 Jan 2010 2:14 AM #7
Err, what? Plain English please.
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
-
22 Jan 2010 2:21 AM #8
that came out wrong.
never mind i got it thanks guys for your help.
-
22 Jan 2010 2:50 AM #9
how would you go about removing the textarea and text fields that have been added. I have added a new button called remove but this seems to remove everyting thats on the form including the remove button itself.
Code:var fsf = new Ext.FormPanel({ labelWidth: 120, frame:true, autoWidth:true, items: [{ xtype:'fieldset', ref: 'fs', title: '', autoHeight:true, defaults: {width: 250}, defaultType: 'textfield', collapsible:true, items :[{ fieldLabel: 'Top level menu', name: 'top_menu', allowBlank:false },{ fieldLabel: 'Description', name: 'top_description', xtype:'textarea' },{ xtype:'button', text: 'Add field', handler: function() { var p = fsf.fs.items.items.length - 1; fsf.fs.insert(p, { xtype: 'textarea', fieldLabel: '', value:'Description goes here' }); fsf.fs.insert(p, { xtype: 'textfield', fieldLabel: '', value:'Title goes here' }); fsf.fs.doLayout(); } },{ xtype:'button', text: 'Remove field', handler: function() { var p = fsf.fs.items.items.length - 1; fsf.fs.remove(p); fsf.fs.doLayout(); } }] }], buttons: [{ text: 'Save', handler:function(){ } },{ text: 'Cancel' }] });
-
22 Jan 2010 3:52 AM #10
What do you WANT to remove?
You are asking it to remove whatever is the last item there.
Which is the Button. So that's a once-only operation!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


Reply With Quote