PDA

View Full Version : Accessing tbar elements from other elements.



jfizer
8 Jul 2009, 2:25 PM
So I have a simple tool bar attached to a data grid.


tbar:[{
xtype: 'textfield',
name: 'searchField',
emptyText: 'enter search term'
}, '-',{
text:'Find',
handler : function(){
alert( Text from searchField );
}
}]How can I access the text from the searchField in the handler for the find button? Keeping in mind that the name may not be unique as these data grids are dynamically created and I've had problems using Ext.get in the past as a result.

mjlecomte
8 Jul 2009, 2:36 PM
Did you try setting the scope for the handler and then using setText?

Or, maybe I misunderstood your request, maybe you're looking to get the value of the textfield. Look at the API docs for id, itemId, and getComponent(). There's also a "ref" property you can use. "id" and getCmp() would be my last choice to use, as you indicated that requires the id be unique.

jfizer
8 Jul 2009, 2:45 PM
Did you try setting the scope for the handler and then using setText?

Or, maybe I misunderstood your request, maybe you're looking to get the value of the textfield. Look at the API docs for id, itemId, and getComponent(). There's also a "ref" property you can use. "id" and getCmp() would be my last choice to use, as you indicated that requires the id be unique.
Well, the problem with getting the text by the elements Id, Name etc is that there may be a great many of them since the user can create several different grids all of which use the same code.

However, a solution seems to have presented itself in that if I make a variable of the text field within the grid initialization method (so that the variable is within the context of the method and wont interfere with other variables of the same name elsewhere) I can then just reference it directly.


var findField = new Ext.form.TextField({ xtype: 'textfield',name: 'findField',emptyText: 'enter search term' });

....

tbar:[findField, '-',{
text:'Find',
handler : function(){
alert( findField.getValue() );
}
}]

mjlecomte
8 Jul 2009, 2:51 PM
Yes, that's another way to go. Do check into the itemId and ref properties I mentioned above though, they mitigate the need to even creating a reference like what you're saying.

jfizer
8 Jul 2009, 3:00 PM
Yes, that's another way to go. Do check into the itemId and ref properties I mentioned above though, they mitigate the need to even creating a reference like what you're saying.


I thought they where global in scope, but I'll look again.

mjlecomte
8 Jul 2009, 3:11 PM
nope, they are local to the container/component.