PDA

View Full Version : how to use setValue()?



poplau
30 Aug 2007, 8:09 PM
var mytitle,mycontent;
var simple = new Ext.form.Form({
labelWidth: 75//, // label settings here cascade unless overridden
//url:'save-form.php'
});
simple.add(
mytitle = new Ext.form.TextField({
fieldLabel: 'title',
name: 'first',
width:200,value:'',
allowBlank:false
}),
mycontent = new Ext.form.TextArea({
fieldLabel: 'content',
name: 'address',
grow: false,
width:225,
height:100,
preventScrollbars:true,
value: ''
})
);


when i use
"Ext.get('first').setValue('hello');"
it's say'对象不支持此属性或方法';
if i use
"mytitle.setValue('hello');",it's ok?
could you tell me how to use setValue();

thank

mystix
30 Aug 2007, 11:01 PM
you're trying to Ext.get() a HTMLElement with an id of 'first', but your dynamically generated form field with the name 'first' does not have an id.

you'll need to give your TextField an id via the id config.


...
...
mytitle = new Ext.form.TextField({
fieldLabel: 'title',
id: 'myTextFieldId',
name: 'first',
width:200,value:'',
allowBlank:false
}),
...
...

after which you'll be able to do


// set the value attribute of the TextField's underlying <input> field
// note: form must already be rendered
Ext.get('myTextFieldId').setValue('myTextFieldValue');



[notes]
Ext.get() returns an Ext.Element, which has a set method for setting various attributes for a HTMLElement.

in your code, mytitle is a reference to an Ext.form.TextField, which has a setValue method -- this is not the same as Ext.Element's set method. Ext.form.TextField's setValue method sets the value of both the js object (i.e. Ext.form.TextField) and the TextField's underlying <input> field, while Ext.Element's set method allows you to set any attributes of a HTMLElement.

devnull
31 Aug 2007, 7:41 AM
not also that if your form object ("simple") is available in the scope where you are calling setValue, you can also use 'simple.findField("first").setValue("hello")'.
Its always nice to have options :)

chun
3 Sep 2007, 1:31 AM
but i append attribute 'id' to this textFiled,i can't apply setValue,er.Message='Ext.get("myTextFieldId").setValue is not a function'
why???
i dont know
help me

chun
3 Sep 2007, 2:43 AM
In a dialog,I put a TextFiled,i want to set value to this TextFiled,but can't,ex.Message is
Ext.get("FirstName").setValue is not a function!!!

This is my jscode

Ext.onReady(function(){

Ext.QuickTips.init();

// turn on validation errors beside the field globally
Ext.form.Field.prototype.msgTarget = 'side';
var top = new Ext.form.Form({
labelAlign: 'center'
});

top.column(
{width:282}, // precise column sizes or percentages or straight CSS
new Ext.form.TextField({
id:'FirstName',
fieldLabel: 'First Name',
name: 'first',
width:125
})

top.render('form_proInfo');
});
var prjDialog = function(){
// everything in this space is private and only accessible in the HelloWorld block

// define some private variables
var dialog, showBtn;

// return a public interface
return {
init : function(){
showBtn = Ext.get('show-dialog-btn');
// attach to click event
showBtn.on('click', this.showDialog, this);

},

showDialog : function(){
if(!dialog){ // lazy initialize the dialog and only create it once
dialog = new Ext.LayoutDialog("prjInfoForm", {
modal:true,
width:800,
height:400,
shadow:true,
minWidth:800,
minHeight:400,
proxyDrag: true,
center: {
autoScroll:true,
tabPosition: 'top',
alwaysShowTabs: true
}
});
dialog.addButton('Submit', dialog.hide, dialog);
dialog.addButton('Close', dialog.hide, dialog);

var layout = dialog.getLayout();
layout.beginUpdate();
layout.add('center', new Ext.ContentPanel('form', {title: 'Project Description'}));
layout.endUpdate();
}
dialog.show(showBtn.dom);
Ext.get('FirstName').setValue('Name');
}
};
}();
Ext.EventManager.onDocumentReady(prjDialog.init, prjDialog, true);


why ?,i don't know, hlep me ,pls:(:(:(

java9394
11 Sep 2007, 3:22 PM
but i append attribute 'id' to this textFiled,i can't apply setValue,er.Message='Ext.get("myTextFieldId").setValue is not a function'
why???
i dont know
help me

I am brand new to Ext JS. I got the same problem.

function test() {
var el = Ext.get("txt1");
alert(el.getValue()); // worked OK
el.setValue("new value"); //got error: el.setValue is not a function
}

<input id="txt1" type="text">

mystix
11 Sep 2007, 7:06 PM
the get() (http://extjs.com/deploy/ext/docs/output/Ext.html#get) method returns an Ext.Element (http://extjs.com/deploy/ext/docs/output/Ext.Element.html).

Look at the docs for Ext.Element -- there's no setValue() method in Ext.Element at all.
use the set() (http://extjs.com/deploy/ext/docs/output/Ext.Element.html#set) method instead, like this


Ext.get('txt1').set({value: 'new value'});


i've edited the other post to reflect this.
i must have been asleep while typing the previous post ~o)

[edit]
the set method accepts an object configuration, and allows you to set any number of attributes on an HTMLElement at the same time, like so


Ext.get('txt1').set({
value: 'new value',
name: 'txt_1',
class: 'myText myField',
readOnly: 'readonly',
style: {
width: '240px'
}
});

java9394
11 Sep 2007, 7:14 PM
the get() (http://extjs.com/deploy/ext/docs/output/Ext.html#get) method returns an Ext.Element (http://extjs.com/deploy/ext/docs/output/Ext.Element.html).

Look at the docs for Ext.Element -- there's no setValue() method in Ext.Element at all.

hhmmm...

does ExtJS provide a generic getElementById function, like dojo.byId() ?

thnx.

mystix
11 Sep 2007, 7:18 PM
hhmmm...

does ExtJS provide a generic getElementById function, like dojo.byId() ?

thnx.

that would be Ext.get() (returns an Ext.Element), or Ext.getDom() (returns an HTMLElement), or Ext.fly() (returns a flyweight Ext.Element).

check the docs for Class Ext (http://extjs.com/deploy/ext/docs/output/Ext.html) on how to use these methods.

medosa
29 Jul 2008, 7:31 AM
How to set the value of test item that is in another js file not in the same file so the
Ext.get('fieldname)
returns null;

fruitwerks
10 Oct 2009, 10:32 AM
Just wanted to mention that is does not work with emptyText.

It makes sense why it does not. A month ago I would have not thought of it, but it is something I ran into today.

Hopefully this saves someone some time :)