submit form on ENTER press int EXT js - extjs

i am very new to EXT.js; i need to submit the form when ENTER is pressed below is my code but i dont know what to call in the listener of the password field here is my code:
ie:what is the function to call in the listener
<script type="text/javascript">
Ext.onReady(function() {
Ext.tip.QuickTipManager.init();
Ext.create("Ext.container.Viewport", {
layout: "border",
rtl: <spring:theme code='theme.rtl' text='false' />
});
Ext.create("Ext.window.Window", {
title: "<spring:message code='title.login' text='Login' />",
height: 310,
width: 450,
closable: false,
layout: "border",
items: [{
xtype: "panel",
border: false,
bodyCls: "login-header",
height: 160,
region: "north"
}, {
id: "<%=loginFormId%>",
url: "<spring:url value='/secure/auth'/>",
xtype: "form",
layout: "form",
region: "center",
bodyPadding: 10,
border: false,
buttons: [{
handler: function() {
var form = this.up("form").getForm();
if (form.isValid()) {
Ext.getCmp("<%=submitBtnId%>").disable();
form.standardSubmit = true;
form.method = "POST";
form.submit();
}
},
id: "<%=submitBtnId%>",
text: "<spring:message code='button.submit' text='Submit' />"
}, {
handler: function() {
var form = this.up("form").getForm();
form.reset();
},
id: "<%=clearBtnId%>",
text: "<spring:message code='button.clear' text='Clear' />"
}],
defaultType: "textfield",
defaults: {
msgTarget: "side",
labelWidth: 100
},
items: [{
fieldLabel: "<spring:message code='input.username' text='Username' />",
name: "selfcare_username"
}, {
fieldLabel: "<spring:message code='input.password' text='Password' />",
name: "selfcare_password",
enableKeyEvents:true,
inputType: "password",
listeners: {
scope: this,
specialkey: function(f, e) {
if (e.getKey() === e.ENTER) {
}
}
}
}]
}]
}).show();
<c:if test="${not empty param.error}">
var errorMsg = "<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />";
if (errorMsg !== "") {
Ext.MessageBox.show({
title: "<spring:message code='title.error' text='Error' />",
msg: errorMsg,
closable: false,
buttons: Ext.Msg.OK
});
}
</c:if>
});
</script>

These days it is better to use the defaultButton property on the form to designate the default button on the form. This is the button who's handler will handle your ENTER key.:
http://docs.sencha.com/extjs/6.0/6.0.2-classic/#!/api/Ext.panel.Panel-cfg-defaultButton

You should attach key event of the components listener, here is the sample which is working if the field not empty and pressed key ENTER or TAB inside the field.
suppliers is a JsonStore where I am loading store by params which means you can call whatever you wrote in the app.
{
xtype: 'textfield',
id: 'supplier-id',
flex: 1,
tabIndex: 1,
fieldLabel: 'SUPPLIER NO',
fieldStyle: 'text-align: right; font-size: 12pt',
margins: '0 5 0 0',
enablekeyEvents: true,
listeners: {
specialkey: function (field, e) {
if (field.getValue() != 'null') {
if (e.getKey() === e.ENTER || e.TAB) {
suppliers.load({
params: {'supplier': field.getValue(), 'type': 'supplier'},
callback: function () {
Ext.getCmp('supplier-name').setValue(suppliers.data.items[0].data['MATCH_NAME']);
}
});
}
}
},
focus: function (e) {
e.setValue('');
Ext.getCmp('supplier-name').setValue("");
suppliers.loadData([], false);
}
}
}

For Sencha:
listeners: {
specialkey: function(field, e){
if (e.getKey() == e.ENTER) {
//submitLogin();
}
}
},

Add listener with afterrender
listeners: {
afterRender: function(thisForm, options){
this.keyNav = Ext.create('Ext.util.KeyNav', this.el, {
enter: fnLogin,//give here to login function
scope: this
});
}
}

Related

Extjs how to get the cursor position in a textareafield

I'm new to Extjs, I need to know how to get te position of the cursor in a textareafield.
I've been googleing an I found these links:
EXTJS 5: Get the current cursor position in a textfield or lookupfield
and
In ExtJs, how to insert a fixed string at caret position in a TextArea?
From there I got this:
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.define({
xtype: 'container',
renderTo: Ext.getBody(),
layout: 'vbox',
padding: 20,
defaults: {
xtype: 'button',
margin: '0 0 12 0'
},
items: [
{
xtype: 'textareafield',
grow: false,
width: 545,
height: 120,
name: 'message',
fieldLabel: '',
id: 'mytextarea',
anchor: '100%'
},
{
xtype: 'button',
text: 'Go',
scale: 'medium',
id: 'mybutton',
listeners: {
click: function() {
var zone = Ext.getCmp('mytextarea');
var text = zone.getValue();
var posic = zone.el.dom.selectionStart;
console.log(posic); // undefined
}
}
}
]
});
}
});
this fiddle
Oh, and I'm using Ext 6.x, Linux Mint, Firefox and Chromium.
But always posic will return undefined... How can I solve this?
You may try with the following approach:
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.define('Trnd.TestWindow', {
extend: 'Ext.window.Window',
closeAction: 'destroy',
border: false,
width: 400,
height: 500,
modal: true,
closable: true,
resizable: true,
layout: 'fit',
title: 'Close window to see the position',
getCaretPos: function() {
var me = this;
var el = me.myTextArea.inputEl.dom;
if (typeof(el.selectionStart) === "number") {
return el.selectionStart;
} else if (document.selection && el.createTextRange){
var range = document.selection.createRange();
range.collapse(true);
range.moveStart("character", -el.value.length);
return range.text.length;
} else {
throw 'getCaretPosition() not supported';
}
},
initComponent: function() {
var me = this;
me.callParent(arguments);
me.myTextArea = Ext.create('Ext.form.field.TextArea', {
width: 500,
height: 500,
editable: true,
selectOnFocus: false,
listeners: {
afterrender: function() {
this.focus(true);
var cursorPos = this.getValue().length;
this.selectText(cursorPos, cursorPos);
}
}
});
me.panel = Ext.create('Ext.panel.Panel', {
items: [
me.myTextArea
]
});
me.add(me.panel);
},
listeners: {
'close': function() {
var me = this;
alert(me.getCaretPos());
}
}
});
var win = new Trnd.TestWindow({
});
win.show();
}
});
Test example with this fiddle.
Use Ext.getDOM() instead of Ext.getCmp() like this:
var myTextArea = Ext.getDom('mytextarea');
var textInArea = myTextArea.value;
var caretPosition = myTextArea.selectionStart;
console.log(caretPosition);
EDIT:
Also xtype of the field must be changed to textarea. In this case your initial example should work too.

ExtJS: Issue with scope in class

I'm keep facing with a issue to choice exact component with scope. As you'll notice below I've created 2 different functions inside gridpanel. One of those creates a Ext.MessageBox for confirm. And other function creates a Ext.window.Window depends on button click of MessageBox.
The thing here is; It should destroy related component with cancel and no buttons. Both buttons always point to gridpanel because of var me = this state and destroys the gridpanel itself.
How can I point destroy method directly to related component?
Ext.define('MyApp.FooGrid', {
extend: 'Ext.grid.Panel',
reference: 'fooGrid',
getGridMenu: function () {
// Here is the 'Update' function; with right-click user being able to see `contextmenu`
var me = this;
var ret = [
{
text: 'Update',
listeners: {
click: me.onUpdate,
scope: me
}
}
];
return me.callParent().concat(ret);
},
onUpdate: function () {
var me = this,
gridRec = this.getSelectionModel().getSelection(); // Here being able to retrieve row data.
Ext.MessageBox.confirm(translations.confirm, translations.confirmChange, me.change, me);
return gridRec;
},
change: function (button) {
var me = this;
var selectedRec = me.onUpdate();
var selectedRecEmail = selectedRec[0].data.email; //Retrieves selected record's email with right-click action
if (button === "yes") {
return new Ext.window.Window({
alias: 'updateWin',
autoShow: true,
title: translations.update,
modal: true,
width: 350,
height: 200,
items: [
{
xtype: 'container',
height: 10
},
{
xtype: 'textfield',
width: 300,
readOnly: true,
value: selectedRecEmail //Display selected record email
},
{
xtype: 'textfield',
width: 300,
fieldLabel: translations.newPassword
}
],
dockedItems: [
{
xtype: 'toolbar',
dock: 'bottom',
items: [
{
xtype: 'tbfill'
},
{
xtype: 'button',
text: translations.cancel,
listeners: {
click: function () {
me.destroy(); // Here is the bug: When user clicks on this button; should destroy current window but it destroys 'gridpanel' itself
}
}
},
{
xtype: 'button',
text: translations.save,
listeners: {
click: function () {
console.log("I'll save you!");
}
}
}
]
}
]
});
} else {
console.log('this is no!');
me.destroy(); // Another bug raises through here: If user will click on No then 'messagebox' should destroy. This one is destroys the gridpanel as well.
}
}
});
How can I point destroy method directly to related component?
Firstly on confirmation box button's(No) click, you don't need to destroy it will automatically hide the box whenever you click into No.
And for update window instead of using me.destroy() you need to use directly button.up('window').destroy() so it will only destroy your update window not the grid.
And also you don't need to again call me.onUpdate() inside of change function otherwise it will again show the confirmation box. You can directly get selected record on the change function like this me.getSelection().
In this Fiddle, I have created a demo using your code and I have put my efforts to get result.
CODE SNIPPET
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.create('Ext.data.Store', {
storeId: 'demostore',
fields: ['name', 'email', 'phone'],
data: [{
name: 'Lisa',
email: 'lisa#simpsons.com',
phone: '555-111-1224'
}, {
name: 'Bart',
email: 'bart#simpsons.com',
phone: '555-222-1234'
}, {
name: 'Homer',
email: 'homer#simpsons.com',
phone: '555-222-1244'
}, {
name: 'Marge',
email: 'marge#simpsons.com',
phone: '555-222-1254'
}]
});
Ext.create('Ext.grid.Panel', {
title: 'Demo GRID',
store: 'demostore',
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
listeners: {
itemcontextmenu: function (grid, record, item, index, e, eOpts) {
e.stopEvent();
grid.up('grid').getGridMenu().showAt(e.getXY());
}
},
renderTo: Ext.getBody(),
getGridMenu: function () {
var me = this;
if (!me.contextMenu) {
me.contextMenu = Ext.create('Ext.menu.Menu', {
width: 200,
items: [{
text: 'Update',
handler: me.onUpdate,
scope: me
}]
});
}
return me.contextMenu;
},
onUpdate: function () {
var me = this;
Ext.MessageBox.confirm('Confirmation ', 'Are your sure ?', me.change, me);
},
change: function (button) {
var me = this,
selectedRecEmail = me.getSelection()[0].data.email; //Retrieves selected record's email with right-click action
if (button === "yes") {
return new Ext.window.Window({
autoShow: true,
title: 'Update',
modal: true,
width: 350,
height: 200,
items: [{
xtype: 'tbspacer',
height: 10
}, {
xtype: 'textfield',
width: 300,
readOnly: true,
value: selectedRecEmail //Display selected record email
}, {
xtype: 'textfield',
inputType:'password',
width: 300,
fieldLabel: 'New Password'
}],
dockedItems: [{
xtype: 'toolbar',
dock: 'bottom',
items: [{
xtype: 'tbfill'
}, {
xtype: 'button',
text: 'cancel',
listeners: {
click: function (btn) {
btn.up('window').destroy(); // Here is the bug: When user clicks on this button; should destroy current window but it destroys 'gridpanel' itself
}
}
}, {
xtype: 'button',
text: 'save',
listeners: {
click: function () {
console.log("I'll save you!");
}
}
}]
}]
});
}
}
});
}
});

scroll grid sometimes not reload again in extjs

i have a grid and scroll in a grid. this is my code for generate the grid
gridMain = Ext.create('Ext.grid.Panel', {
renderTo: Ext.get("sa-grid"),
store: 'pf-store',
height: mainContentHeight - 63,
title: 'Promotion Form',
columns: [{
text: 'No PF',
width: 115,
dataIndex: 'PFID'
},
{
text: 'Title',
flex: 1,
dataIndex: 'Title'
},
{
text: 'Promotion From',
dataIndex: 'PromotionFrom'
},
{
text: 'Promotion To',
dataIndex: 'PromotionTo'
},
{
text: 'Limit Payment Date',
width: 120,
dataIndex: 'LimitPaymentDate'
},
{
text: 'Request Status',
width: 150,
dataIndex: 'RequestDescription'
},
{
text: 'Initiator',
width: 150,
dataIndex: 'CreateByName'
}],
dockedItems: [{
xtype: 'toolbar',
items: [actAddPF, actEditPF, actDeletePF, actExtendPF]
}],
listeners: {
itemdblclick: function (view, record, item, index, e, eOpts) {
isGridClicked = true;
ViewDetails(FormState.VIEW);
}
}
});
function ViewDetails(FormState) {
var data = GetSelectedRecord(gridMain);
var id = data.PFID;
ShowLoading("sa-body", "Please Wait ...");
$.ajax({
type: 'POST',
url: root + "PF/GetDataByID",
data: { PFID: id },
success: function (result) {
try {
DataToArray(result);
DataToControl(result);
CreatePivotPeriode();
CreateMainPivot();
ChangeFormState(FormState);
tabs.setActiveTab('pageDetails');
} catch (err) {
MsgErr(err);
}
},
complete: function () {
try {
HideLoading();
} catch (err) {
MsgErr(err);
}
}
});
}
it works fine, but when i insert or modify data and load again or click search by parameter. the scroll is not working. the content in grid is update based on search parameter, the problem is only the scroll which is not working anymore. then I have to do F5 to refresh it again so the grid can be scroll again.
this is my code for searching in grid
$("#btnSearch").click(function () {
ShowLoading("sa-body", "Please Wait ...");
$.post(root + "PF/Search", { FieldName: $("#cbSearch").val(), Pattern: $("#cbPattern").val(), Condition: $("#txtSearch").val() }, function (data) {
storePF.loadDataViaReader(data);
HideLoading();
});
});
Does someone have a solution about this problem ?
Many thanks
i can do by this
:)
Ext.override(Ext.grid.Scroller, {
onAdded: function() {
this.callParent(arguments);
var me = this;
if (me.scrollEl) {
me.mun(me.scrollEl, 'scroll', me.onElScroll, me);
me.mon(me.scrollEl, 'scroll', me.onElScroll, me);
}
}
});

Why is event not fired again when click inside ExtJS 4.2.2 text input field

In the below ExtJS 4.2.2 code, you can click repeatedly on the "Search" and "Show Label" controls, and the label "here is the text" will toggle visible/hidden.
But if you click in the search text input field, the label is only hidden the first time you click there. If you then click "Show Label" to once again display the label, and then again click the search text input field, the label if not hidden.
Ext.define('MyToolbar', {
extend: 'Ext.grid.feature.Feature',
alias: 'feature.myToolbar',
requires: ['Ext.grid.feature.Feature'],
width: 160,
init: function () {
if (this.grid.rendered)
this.onRender();
else{
this.grid.on('render', this.onRender, this);
}
},
onRender: function () {
var panel = this.toolbarContainer || this.grid;
var tb = panel.getDockedItems('toolbar[dock="top"]');
if (tb.length > 0)
tb = tb[0];
else {
tb = Ext.create('Ext.toolbar.Toolbar', {dock: 'top'});
panel.addDocked(tb);
}
this.createSearchBox(tb);
},
createSearchBox: function (tb) {
tb.add({
text: 'Search',
menu: Ext.create('Ext.menu.Menu'),
listeners: {
click: function(comp) {
MyApp.app.fireEvent('onGridToolbarControlClicked', comp);
}
}
});
this.field = Ext.create('Ext.form.field.Trigger', {
width: this.width,
triggerCls: 'x-form-clear-trigger',
onTriggerClick: Ext.bind(this.onTriggerClear, this)
});
this.field.on('render', function (searchField) {
this.field.inputEl.on('click', function() {
MyApp.app.fireEvent('onGridToolbarControlClicked', searchField);
}, this, {single: true});
}, this, {single: true});
tb.add(this.field);
}
});
Ext.define('MyPage', {
extend: 'Ext.container.Container',
alias: 'widget.myPage',
flex: 1,
initComponent: function () {
var me = this;
Ext.applyIf(me, {
items: [{
xtype: 'container',
layout: {
type: 'vbox',
align: 'middle'
},
items: [{
xtype: 'button',
text: 'Show Label',
handler: function(comp) {
comp.up('myPage').down('label').setVisible(true);
}
},{
xtype: 'label',
itemId: 'testLbl',
text: 'here is the text'
},{
xtype: 'gridpanel',
width: 250,
height: 150,
store: Ext.create('Ext.data.Store', {
fields: ['name'],
data: [
{name: 'one'},
{name: 'two'},
{name: 'three'}
]
}),
columns: [{
text: 'Text',
flex: 1,
dataIndex: 'name'
}],
features: [{
ftype: 'myToolbar'
}]
}]
}]
});
me.callParent(arguments);
MyApp.app.on({onGridToolbarControlClicked: function(comp) {
if('function' == typeof comp.up && !Ext.isEmpty(comp.up('myPage')) &&
'function' == typeof comp.up('myPage').down &&
!Ext.isEmpty(comp.up('myPage').down('label'))) {
comp.up('myPage').down('label').setVisible(false);
}
}});
}
});
Ext.onReady(function() {
Ext.application({
name: 'MyApp',
launch: function() {
Ext.create('Ext.container.Viewport', {
renderTo: Ext.getBody(),
width: 700,
height: 500,
layout: 'fit',
items: [{
xtype: 'myPage'
}]
});
}
});
});
Here
this.field.inputEl.on('click', function() {
MyApp.app.fireEvent('onGridToolbarControlClicked', searchField);
}, this, {single: false});
instead of {single:true} in your code. onRender IS single, onClick - (in your case) is not.

Listen change on Filefield in extjs

I want listen when file has been change like. But It not working
{
xtype: 'filefield',
id: 'form-file',
fieldLabel: 'Photo',
name: 'photo-path',
buttonText: '',
buttonConfig: {
iconCls: 'upload-icon'
},
listeners: {
'change': function(this, value){
alert('change');
}
}
}
You can't do it with filefield of Extjs
I have the solution.
Example: http://jsfiddle.net/e3M3e/e8V7g/
var itemFile = null;
Ext.create('Ext.panel.Panel', {
title: 'Hello',
width: 400,
html: "<input id='inputFile' type='file' name='uploaded'/>",
renderTo: Ext.getBody(),
listeners: {
afterrender: function() {
itemFile = document.getElementById("inputFile");
itemFile.addEventListener('change', EventChange, false);
}
}
});
function EventChange(e){
var files = itemFile.files;
console.log(files);
}
I found solution: function change must be:
change: function(f,new_val) { alert(new_val); }

Resources