Skip to content

Instantly share code, notes, and snippets.

@emayk
Created October 1, 2013 04:48
Show Gist options
  • Select an option

  • Save emayk/6773980 to your computer and use it in GitHub Desktop.

Select an option

Save emayk/6773980 to your computer and use it in GitHub Desktop.
extjs, create form dynamic generate
/**
*
* Source From SO
* http://stackoverflow.com/questions/13095695/extjs-4-0-mvc-add-components-to-a-form-based-on-store
*
* Create Form Dynamics
**/
var myitems = Ext.create('Ext.data.Store', {
fields: ['name', 'prompt', 'type'],
data :
[
{"name":"name", "prompt":"Your name" , "type":"textfield"},
{"name":"addr", "prompt":"Your street address", "type":"textfield"},
{"name":"city", "prompt":"Your city" , "type":"textfield"}
]
});
function genwidget(name, prompt, type)
{
console.log("In genwidget with name=" + name + ", prompt=" + prompt + ", type=" + type + ".");
var widget = null;
if (type == "textfield")
{
// widget = Ext.create('Ext.form.field.Text',
widget = Ext.create('Ext.form.TextField',
{
xtype : 'textfield',
name : name,
fieldLabel: prompt,
labelWidth: 150,
width : 400, // includes labelWidth
allowBlank: false
});
}
else
{
// will code others later
console.log("Unrecongized type [" + type + '] in function mywidget');
}
return widget;
};
function genItems(myitems)
{
console.log("Begin genItems.");
// var items = new Ext.util.MixedCollection();
var items = [];
var howMany = myitems.count();
console.log("There are " + howMany + " items.");
for (var i = 0; i < myitems.count(); i++)
{
var name = myitems.getAt(i).get('name');
var prompt = myitems.getAt(i).get('prompt');
var type = myitems.getAt(i).get('type');
var widget = genwidget(name, prompt, type) ;
items.add(widget);
console.log("items.toString(): " + items.toString());
}
return items;
};
Ext.define('MyApp.view.dynamicform.Form' ,{
extend: 'Ext.form.Panel',
alias : 'widget.dynamicformform',
// no store
title: 'Dynamic Form',
id: 'dynamicform.Form',
bodyPadding: 5,
autoScroll: true,
layout: 'auto',
defaults:
{
anchor: '100%'
},
dockedItems: [ ],
items: genItems(myitems),
// Reset and Submit buttons
buttons: [
{
text: 'Reset',
handler: function()
{
this.up('form').getForm().reset();
console.log('Reset not coded yet');
}
},
{
text: 'Submit',
formBind: true, //only enabled once the form is valid
disabled: true,
handler: function()
{
var form = this.up('form').getForm();
if (form.isValid())
{
form.submit({
success: function(form, action)
{
console.log('Success not coded yet');
}
});
}
}
} // end submit
], // end buttons
initComponent: function()
{
console.log("--> in dynamicform init");
var me = this,
items = genItems(myitems);
Ext.apply(me, {
items:items
});
this.callParent(arguments);
console.log("--> leaving dynamicform init");
}
}); // Ext.define
@emayk
Copy link
Copy Markdown
Author

emayk commented Oct 1, 2013

how to create? :-)

@pramila37
Copy link
Copy Markdown

using this code, m getting error for items.add(widget) as "Object doesn't support this property or method". Please help.

@vijay30487
Copy link
Copy Markdown

I am getting the same error which pramila37 was getting. @pramila37: did you get any solution?

@vijay30487
Copy link
Copy Markdown

I got the solution : I have used items.push(widget) and it works for me.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment