PDA

View Full Version : Getting a value from a textfield



eoinyp
3 Aug 2010, 6:52 AM
Hi all, I am new to Ext, I have a simple problem, to get input from a textfield, but I cant find a simple solution to it anywhere:
Within a GridPanel I have a bbar. The bbar has a textfield and a button. When the button is pressed I would like to process the text from the textfield by calling a function on it to produce a result. I am unsure how to get the text from the textfield. I guessed something like 'inputtext.getValue()' is what I need to get the text from the textfield, but that is not correct. I would appreciate any help on this.
Many thanks,
Eoin.

The following is the code for the bbar in the GridPanel:
bbar:[ {
xtype: 'textfield',
width: 100,
id:'inputtext'
},
{
text: "process text",
handler: function() {
ProcessTextInput(inputtext.getValue());
}
}]

//function to process text input
function ProcessTextInput(string){
//do some stuff with the input string.
}

arthurakay
3 Aug 2010, 7:04 AM
First - use CODE tags when posting. It makes reading your code easier :D

You have a few options here:

First, you could use Ext.getCmp()... which is somewhat frowned upon by some members of the community, but it should do the trick with minimal code changes:


ProcessTextInput(Ext.getCmp('inputtext').getValue());


Second option would be to rewrite the way in which you initialize your bbar. Maybe something like this:



var inputTextBox = new Ext.form.TextField({
width: 100
});

//other code...

bbar:[
inputTextBox,
{
text: "process text",
handler: function() {
ProcessTextInput(inputTextBox.getValue());
}
}]

//function to process text input
function ProcessTextInput(string){
//do some stuff with the input string.
}


You could probably solve it a few other ways as well, but that should get you thinking.

christian130
3 Aug 2010, 7:36 AM
also u can declare a global variable (it is declare at the beginning of the code) containg THE GRID. and somewhere u can call it using

grid.getStore().data.items.data.inputtext // inputtext is the id that u gave to it

hope this will help! regards

arthurakay
3 Aug 2010, 7:58 AM
also u can declare a global variable (it is declare at the beginning of the code) containg THE GRID.

I wouldn't use a global variable for this... IMO global variables should be avoided unless absolutely necessary. Namespacing is a better idea in most cases. But you are correct in saying that a *properly scoped* variable for the grid panel could work.

That being said, I don't think this will work because the bbar property is on the grid panel itself... not the data store.


grid.getStore().data.items.data.inputtext


I also wouldn't use ".data.items.data.inputtext" as that's messy and doesn't really describe what you're doing. Maybe:


grid.getBottomToolbar().get('inputtext').getValue()


But I would honestly use one of my original suggestions.