View Full Version : 2 Fold Question: How To Acces FieldSet Methods and How To setFocus on Fieldset Field

5 Jun 2013, 10:10 AM
I have a form that is displayed in a window. This form/window is reused for add/edit features. So instead of coding a separate form for add and edit, i reuse the same form with a "mode" config. The form contains a fieldset that defines all of my fields in the form. Any time the form is displayed (whether in add or edit mode) I want to set focus on the first field. I have already added an afterrender listener to the fieldset definition for this field:

xtype: 'textfield',
itemId: 'inputCode',
name: 'inputCode',
fieldLabel: 'Code',
width: 175,
labelWidth: 75,
margin: '5 0 5 0',
msgTarget: 'side',
maxLength: 10,
enforceMaxLength: true,
allowBlank: false,
listeners: {
afterrender: function(field) {
field.focus(false, 800);

This works great "only when the window first opens". The "add" feature opens the window as follows:

text: 'Add',
iconCls: 'add',
handler: function(btn) {

myFormPanel.getForm().setValues({status: 'A'}); // make sure we default status to Active
myWindow.setTitle('Add Record');
myFormPanel.processMode = 'add';



The edit feature opens the window when you double click on a grid record, and opens the window as follows:

function editRecord(inRecord) {

myWindow.setTitle('Edit Record');

myFormPanel.getForm().setValues({status: inRecord.get('RSTATUS')});
myFormPanel.processMode = 'update';



Again, whenever the window is "1st rendered" the 1st field has focus. But anytime after that window is closed, it will never set focus again.

So, my first thought was to manually set focus prior/after showing the window. But I cannot seem to find a way to set focus on a field that is defined in a fieldset.

Any thoughts/suggestions? Oh, I'm on ExtJs 4.0.3.

5 Jun 2013, 11:08 AM
You could do this in the Window's show event:

myWindow.down('textfield').focus(false, 800);

This should focus the first textfield found in your window, every time the window is shown.

6 Jun 2013, 3:20 PM
Thank you for showing me that solution. I does work.

How would I "query" the field directly when a field is defined in a fieldset?

7 Jun 2013, 5:35 AM
Since your textfield is using an itemId of 'inputcode', you can access the field from your Window (or Form), like so:

var textfield = myWindow.down('#inputCode');

If you need to access the textfield from outside the scope of the Window/Form:

var textfield = Ext.ComponentQuery.query('window #inputCode')[0];

7 Jun 2013, 5:47 AM
gracias friend!