Button click event Ext JS - javascript

I have the simple form:
myForm = new Ext.form.FormPanel({
width:'100%',
frame:false,
items: [
new Ext.form.TextArea({
id:'notes',
name: 'notes',
hideLabel: true,
width:350,
height:200
})
],
buttons: [
{
text:"Save",
click: function (b,e) {
alert('x');
}
}
]
});
However I am having trouble getting the click event of the button to work. Do buttons created the following way have the same functionality of doing Ext.Button?

You either need
a) The handler option (a click shortcut)
new Ext.Button({
handler: function(){
// ....
}
});
b) Event listeners need to be registered in in a listeners block, so
new Ext.Button({
listeners: {
click: function(){
// ...
}
}
});
A) is preferred.

Related

Duplicate id when creating a dialog in OpenUI5

I need help with OpenUI5. I created button in View and by clicking on button it creates Dialog window and throws an error so I cant proceed to functionality of the Dialog.
Button in view:
<m:Button text="{i18n>RESULTS_CHANCES_SEND_EMAIL}"
class="sapUiMediumMarginBegin results-button"
tap="sendToEmail"
press="sendToEmail"
icon="sap-icon://email">
Function in Controller:
sendToEmail: function() {
var email = new Dialog({
title: 'שליחת תוצאות לדוא"ל',
type: 'Message',
content: [
new Input('submitEmailInput', {
liveChange: function (oEvent) {
var sText = oEvent.getParameter('value');
var parent = oEvent.getSource().getParent();
parent.getBeginButton().setEnabled(sText.length > 0);
},
width: '100%',
placeholder: 'דואר אלקטרוני'
})
],
beginButton: new Button({
text: 'שליחה',
enabled: false,
icon: 'sap-icon://email',
press: function () {
//var sText = sap.ui.getCore().byId('submitEmailInput').getValue();
//MessageToast.show('Email is: ' + sText);
// here comes the API request
email.close();
}
}),
endButton: new Button({
text: 'סגירה',
icon: 'sap-icon://decline',
press: function () {
email.close();
}
}),
afterClose: function () {
email.destroy();
}
});
email.open();}
The error: duplicate id
Many thanks!
you have attached the same event handler to "tap" and "press" events so sendToEmail is being called twice (and the second time the control with the same ID already exists)... remove "tap" as this is depreciated, so you should end up with:
<m:Button text="{i18n>RESULTS_CHANCES_SEND_EMAIL}"
class="sapUiMediumMarginBegin results-button"
press="sendToEmail"
icon="sap-icon://email">

Dijit Dialog in JSFiddle launching immediately - not onClick

I'm struggling to get a Dijit dialog to work for a reproducible example. I took the working code from this JSfiddle and simply tried to turn this into a named function to use throughout the example.
The author uses:
new Button({label: 'Show dialog', onClick: function() {
//Create dialog programmatically here
}
});
but I've changed this to be slightly different:
function launchSelectDialog(selectOptions) {
//Create dialog programmatically here
}
registry.byId("default-launch", "onClick", launchSelectDialog(allOpts));
Here is my version. Unfortunately, this just launches the dialog immediately upon loading the page, and never again when clicking on the button.
I have checked the NoWrap option in JSFiddle. I have no other clues about what's going on.
Please help if you have any ideas.
There are couple of issue.
1) Like others a have pointed out, you are invoking the function not setting up the event with function. hence the dialog is visible onload.
2) You need to wait till the html has been parse. or you need to use parser.parse()
Here is the updated fiddler: http://jsfiddle.net/49y3rxzg/9/
() is an invocation operator. You are calling the function yourself and the returned value of the function is set as the event handler. If you want to reuse the function, use a closure:
function launchSelectDialog(selectOptions) {
// the returned function will be used as the event handler
return function() {
// the passed `selectOptions` is remembered in this context
}
}
Another option is:
registry.byId("default-launch", "onClick", function() {
launchSelectDialog(allOpts);
});
You need to initiate your Button widget before retrieving with registry.byId().
In your code actually registry.byId("default-launch") was returning undefined;
Also registry.byId() function accept only an id so additional parameters will be ignored.
To fix it you should initiate a Button instance properly and declare launchSelectDialog(allOpts) withinonClick, as:
var myButton = new Button({
label: "Default Options",
onClick: function() {
launchSelectDialog(allOpts);
}
}, "default-launch");
Below fixed version for your script.
http://jsfiddle.net/usm829jq/
require([
"dojo/dom",
"dijit/Dialog",
"dijit/form/Button",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dijit/registry",
"dojo/domReady!"
], function(dom, DijitDialog, Button, BorderContainer, ContentPane, registry) {
var allOpts = [{
label: "Foo",
value: "foo"
}, {
label: "Bar",
value: "bar"
}]
var myButton = new Button({
label: "Default Options",
onClick: function() {
launchSelectDialog(allOpts);
}
}, "default-launch");
function launchSelectDialog(SelectOptions) {
var layout = new BorderContainer({
design: "headline",
style: "width: 400px; height: 400px; background-color: yellow;"
});
var centerPane = new ContentPane({
region: "center",
style: "background-color: green;",
content: "center"
});
var actionPane = new ContentPane({
region: "bottom",
style: "background-color: blue;"
});
(new Button({
label: "OK"
})).placeAt(actionPane.containerNode);
(new Button({
label: "Cancel"
})).placeAt(actionPane.containerNode);
layout.addChild(centerPane);
layout.addChild(actionPane);
layout.startup();
var dialog = new DijitDialog({
title: 'dialog title',
style: {
//width: '400px',
//height: '400px',
},
content: layout
});
dialog.containerNode.style.backgroundColor = "red";
dialog.startup();
dialog.show();
}
})

Manipulating a button via id

I have a button add that opens a window (win) that contains some textarea and a submit button.
My problem is that i want to use the id of the add button the function submit(btn). How can i get it?
var btn_add = Ext.create('Ext.Button', {
width:65,
height:30,
text:'Add',
id:"btn_add",
iconCls: 'add',
handler: onButtonClick
});
function onButtonClick(btn) {
switch (btn.id){
case "btn_add": {
win.show();
break;
}
}
win = new Ext.Window({
title: 'Form',
autoScroll: true,
y: 120,
width: 300,
height: 160,
modal: true,
closeAction: 'hide',
items: [name,btn_submitform]
});
var btn_submitform = Ext.create('Ext.Button', {
width:75,
height:30,
text:'Submit',
id:"btn_submitform",
handler: submit
});
function submit(btn) {
if(btn.id="btn_add"){
}
}
Try to create a class in order to define your button. After that, try to catch it by its alias\xtype wherever you want it.
Ext.define('MyApp.CoolBtn', {
extend: 'Ext.Button',
alias: ['widget.coolbtn'],
title: 'Yeah!'
});
// Using Ext.create
Ext.create('widget.coolbtn');
Ext.ComponentQuery.query('coolbtn'); // Get you cmp by alias

How to handle ctrl+tab key in extjs 3.4?

I am working on Extjs3.4. I want to keymap ctrl+tab in my application. but when I try to use it, it opens my next browser tab. How can I solve it?
This is my code :-
var keyMap = new Ext.KeyMap(Ext.getDoc(), {
key: Ext.EventObject.TAB,//9
ctrl: true,
stopEvent : true,
fn: function () { console.log('it works'); },
scope: this
});
Please give some suggestion.
Try to put a listener in the respective field (text or whatever)
listeners: {
keydown: { //tab could be listened in keydown
element: 'el',
fn: function(e){
if(e.urKey && e.urAnotherKey)
alert('keydown, execute my action');
}
},
keypress: {
element: 'el',
fn: function(){
alert('keypress');
}
}
}

extjs: how can I add event handlers to a component in its raw object form (xtype)

I have an extjs component in its raw object type, for example:
var x = {
xtype: 'button',
text: 'Delete',
handler: whatever,
more:config,
more2: config2};
Now I want to add some listener to x. In my scenario I don't have access to the x object before or right after it is created. I just want to add an event handler when it is just a javascript object without overwriting existing handlers. How can that be done?
You can use the listeners config to do this
{
xtype: 'button',
text: 'Delete',
handler: whatever,
more:config,
more2: config2,
listeners:{
scope : this,
event1 : function(){},
event2 : function(){}
}
};
A listeners config is needed:
var x = {
xtype: 'button',
text: 'Delete',
handler: whatever,
more:config,
more2: config2,
listeners: {
click: function() {
...
},
render: function() {
...
}
}
};

Categories

Resources