0

我很惭愧地说我今天大部分时间都花在这上面。我正在尝试根据数据存储的内容构建一个表单。我正在使用 Ext.js 和 MVC 架构。这是我所拥有的:

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 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");

        this.callParent(arguments);

        console.log("--> leaving dynamicform init");
    }


});  // Ext.define

我得到的错误(我一整天都有很多)是“my.items.push 不是一个函数”。

提前感谢您提供的任何帮助!

4

1 回答 1

1

items预计是一个数组而不是 MixedCollection。如果我记得不错,稍后会更改为 MixedCollection。因此,要解决您的问题更改:

var items = new Ext.util.MixedCollection();
// ...
items.add(widget);

var items = [];
// ...
items.push(widget);

考虑将items表单定义为空数组,然后initComponent使用Ext.apply

var me = this,
    items = genItems(myitems);
Ext.apply(me, {
    items:items
});
于 2012-10-28T11:27:00.530 回答