如何在extjs中从get form设置textfield的值

时间:2014-10-17 02:06:03

标签: extjs4 textfield setvalue

我有表格

xtype: 'form',
border: false,
padding: '5 0 0 0',
bodyPadding: 5,
items: [{
	xtype: 'textfield',
	fieldLabel: 'Theme Name',
	name: 'themeName',
	itemId: 'themeName',
	allowBlank: false
},{
	xtype: 'displayfield',
	fieldLabel: 'Text',
	itemId: 'themeText',
        name: 'Text',
	value: '<div class="theme-text-upload"></div>'
}]

然后我有功能将值设置为textfield。

setValueToTextField: function(){
  var field = form.getForm();  
  for (var i = 0; i < field.items.length; i++) {						
	var name = field.items[i].name;
	var fieldData = data[name];
        if(field.items[i].xtype === 'textfield'){
            field.items[i].name.setValue(fieldData);
        }
  };
}

但是我无法为textfield设置值。它显示错误消息,即setValue()不是函数。

1 个答案:

答案 0 :(得分:0)

您的代码未提供有关您要实现的目标的完整信息。我想知道你正试图设置textfield的值并将displayfield与textfield值绑定。

以下是 fiddle

Ext.require([
    'Ext.form.*',
    'Ext.tip.*'
]);

Ext.onReady(function() {
    var parentPanel = Ext.create('Ext.form.Panel', {
        renderTo: Ext.getBody(),
        items: [{
            xtype: 'form',
            id: 'sampleform',
            border: false,
            padding: '5 0 0 0',
            bodyPadding: 5,
            items: [{
                xtype: 'textfield',
                fieldLabel: 'Theme Name',
                name: 'themeName',
                itemId: 'themeName',
                allowBlank: false,
                listeners: {
                    'change': function(textField, newVal, oldVal, opt) {
                        var displayField = Ext.ComponentQuery.query('[name=Text]');
                        displayField[0].setValue(newVal);
                    }
                }
            }, {
                xtype: 'displayfield',
                fieldLabel: 'Text',
                itemId: 'themeText',
                name: 'Text',
                value: '<div class="theme-text-upload"></div>'
            }, {
                xtype: 'button',
                text: 'populate Text Field',
                handler: function() {
                    var form = this.up('form');
                    var field = form.getForm();
                    var textField = Ext.ComponentQuery.query('[name=themeName]');
                    textField[0].setValue("updated value");
                }
            }]
        }]
    });
});