About ExtJS4.1: Problems with accelerator support _javascript tips

Source: Internet
Author: User
Tags comments
Problem a page has two panels, all have an "add (F2)" button, how to do shortcut key support? Picture signal

First time implementation
The feeling should be very simple, ExtJS provides "Ext.util.KeyMap", easy to do shortcut key support.
code example

Copy Code code as follows:

<reference path= "Ext/ext-all-debug-w-comments.js"/>

Ext.onready (function () {

var viewport = ext.create (' Ext.container.Viewport ', {
Layout: {
Type: ' VBox ',
Align: ' stretch '
},
Padding:10,
Items: [{
Xtype: ' Panel ',
ID: ' Panela ',
Title: ' Shortcut key test a ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. '
}, {
Xtype: ' Panel ',
ID: ' Panelb ',
Title: ' Shortcut key Test B ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. '
}]
});

Ext.create (' Ext.util.KeyMap ', {
Target: ' Panela ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add a ');

Ev.stopevent ();

return false;
}
});

Ext.create (' Ext.util.KeyMap ', {
Target: ' Panelb ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add B ');

Ev.stopevent ();

return false;
}
});
});

Actual results

Open the browser directly after the F2 no effect, open the browser with the mouse click a or B and then press F2 also no effect.
Second Implementation
The original DIV element must increase the tabindex=0 attribute.
code example
Copy Code code as follows:

<reference path= "Ext/ext-all-debug-w-comments.js"/>

Ext.onready (function () {

var viewport = ext.create (' Ext.container.Viewport ', {
Layout: {
Type: ' VBox ',
Align: ' stretch '
},
Padding:10,
Items: [{
Xtype: ' Panel ',
ID: ' Panela ',
Title: ' Shortcut key test a ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}, {
Xtype: ' Panel ',
ID: ' Panelb ',
Title: ' Shortcut key Test B ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}]
});

Ext.create (' Ext.util.KeyMap ', {
Target: ' Panela ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add a ');

Ev.stopevent ();

return false;
}
});

Ext.create (' Ext.util.KeyMap ', {
Target: ' Panelb ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add B ');

Ev.stopevent ();

return false;
}
});
});

Actual results
Open the browser directly after the F2 no effect, open the browser with the mouse click a or B and then press F2 will have effect.

third-time implementation
To resolve the shortcut key without clicking on the div after opening the browser, you need to manually invoke the Foucs () method.
code example
Copy Code code as follows:

<reference path= "Ext/ext-all-debug-w-comments.js"/>

Ext.onready (function () {

var viewport = ext.create (' Ext.container.Viewport ', {
Layout: {
Type: ' VBox ',
Align: ' stretch '
},
Padding:10,
Items: [{
Xtype: ' Panel ',
ID: ' Panela ',
Title: ' Shortcut key test a ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}, {
Xtype: ' Panel ',
ID: ' Panelb ',
Title: ' Shortcut key Test B ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}]
});

Ext.create (' Ext.util.KeyMap ', {
Target: ' Panela ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add a ');

Ev.stopevent ();

return false;
}
});

Ext.create (' Ext.util.KeyMap ', {
Target: ' Panelb ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add B ');

Ev.stopevent ();

return false;
}
});

Ext.get (' Panelb '). focus ();
});

Actual results
Open the browser directly after the F2 has effect, open the browser with the mouse click a or B and then press F2 will have effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.