How to return value from Ext.Dialog? - extjs

How to return value from Ext.Dialog? I think this is a very frequent task, but I could never find the appropriate answer.
The two problems that I see here are: 1) how to return value from another dialog, and 2) how to wait for that value from the view the dialog has been called.
As for the first problem, one thing that pops up into my head is to write the value in the viewModel of the caller view, but I have no idea how to wait for that value.
I prepared a simple example, in which I need to create 'MyApp.view.main.MyView', but for that I need to know whether user selected A1, A2, A3 or A4.
onBtnClick: function(){
//call Dialog
Ext.create('MyApp.view.main.MyDialog', {}).show();
//how to get value from dialog and wait for that here?
//then call another view
Ext.create('MyApp.view.main.MyView', {
type: type,
}).show();
},
Ext.define('MyApp.view.main.MyDialog', {
extend: 'Ext.Dialog',
items: [
{
xtype: 'button',
text: 'A1',
handler: function(){
this.up().close();
}
},
{
xtype: 'button',
text: 'A2',
handler: function(){
this.up().close();
}
},
{
xtype: 'button',
text: 'A3',
handler: function(){
this.up().close();
}
},
{
xtype: 'button',
text: 'A4',
handler: function(){
this.up().close();
}
}
],
});

You can use custom events:
Ext.define('MyApp.view.main.MyDialog', {
extend: 'Ext.Dialog',
items: [{
xtype: 'button',
text: 'A1',
handler: function () {
this.up('dialog').fireEvent('dialogbuttonclick', 'A1');
this.up().close();
}
}, {
xtype: 'button',
text: 'A2',
handler: function () {
this.up('dialog').fireEvent('dialogbuttonclick', 'A2');
this.up().close();
}
}, {
xtype: 'button',
text: 'A3',
handler: function () {
this.up('dialog').fireEvent('dialogbuttonclick', 'A3');
this.up().close();
}
}, {
xtype: 'button',
text: 'A4',
handler: function (btn) {
this.up('dialog').fireEvent('dialogbuttonclick', 'A4');
this.up().close();
}
}],
});
Ext.create('MyApp.view.main.MyDialog', {
listeners: {
'dialogbuttonclick': function (buttonId) {
console.log(info.buttonId);
/*Ext.create('MyApp.view.main.MyView', {
type: buttonId,
}).show();*/
},
}
}).show();

Related

Why the ext component is not overriden although I did exactly as written in documentation?

I would like to override Ext.ux.LiveSearchGridPanel so that instead of text ‘Search’ some other text appears, for example ‘xyz’.
I have a LiveSearchGridPanel as:
Ext.define('MyApp.view.main.List', {
//extend: 'Ext.grid.Panel',
extend: 'Ext.ux.LiveSearchGridPanel',
xtype: 'mainlist',
requires: [
'MyApp.store.Personnel'
],
title: 'Personnel',
store: {
type: 'personnel'
},
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone', flex: 1 }
],
listeners: {
select: 'onItemSelected',
afterrender: function() {
console.log('We have been rendered');
}
}
});
I created new file LiveSearchGridPanel123.js in ${app.dir}/overrides directory:
Ext.define('Ext.overrides.LiveSearchGridPanel123', {
override: 'Ext.ux.LiveSearchGridPanel',
initComponent: function () {
var me = this;
console.log(this.self.getName() + ' created 123');
me.tbar = ['XXX', {
xtype: 'textfield',
name: 'searchField',
hideLabel: true,
width: 20,
listeners: {
change: {
fn: me.onTextFieldChange,
scope: this,
buffer: 100
}
}
}, {
xtype: 'button',
text: '<',
tooltip: 'Find Previous Row',
handler: me.onPreviousClick,
scope: me
}, {
xtype: 'button',
text: '>',
tooltip: 'Find Next Row',
handler: me.onNextClick,
scope: me
}
];
me.bbar = Ext.create('Ext.ux.StatusBar', {
defaultText: me.defaultStatusText,
name: 'searchStatusBar'
});
me.callParent(arguments);
}
});
And I added
requires: [
'Ext.overrides.LiveSearchGridPanel123',
],
In app.js.
Then I did sencha app refresh, but component remained the same.
Could you please tell me where the the error is?
screenshot
It's because you call parent this.callParent();.
You can try:
Ext.define(null,{
override:'Ext.ux.LiveSearchGridPanel',
initComponent: function() {
const me = this;
me.tbar = [{
xtype:'textfield'
}];
this.superclass.superclass.initComponent.call(this); //<-- this does the magic
}
});

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!");
}
}
}]
}]
});
}
}
});
}
});

Form panel not showing by set active item

Am new to sencha touch, my problem is i have a separate view to show a form panel by clicking on control event the form panel view will load, but when i click on the event nothing display on my screen. Just a blank screen to show..I dont know what am doing. The form panel is taken from sencha form panel tutorial
The following are my form panel view
Ext.define('WinReo.view.AddContact', {
extend: 'Ext.Container',
xtype: 'addcontact',
requires: [
'Ext.TitleBar'
//'Ext.Video'
],
config: {
layout:'fit'
},
initialize:function(){
console.log('inside initialize');
var formPanel = Ext.create('Ext.form.Panel', {
//xytpe:'formpanel',
fullscreen: true,
layout:'fit',
items: [{
xtype: 'fieldset',
items: [
{
xtype: 'textfield',
name : 'name',
label: 'Name'
},
{
xtype: 'emailfield',
name : 'email',
label: 'Email'
},
{
xtype: 'passwordfield',
name : 'password',
label: 'Password'
}
]
}]
});
formPanel.add({
xtype: 'toolbar',
docked: 'bottom',
layout: { pack: 'center' },
items: [
{
xtype: 'button',
text: 'Set Data',
handler: function() {
formPanel.setValues({
name: 'Ed',
email: 'ed#sencha.com',
password: 'secret'
})
}
},
{
xtype: 'button',
text: 'Get Data',
handler: function() {
Ext.Msg.alert('Form Values', JSON.stringify(formPanel.getValues(), null, 2));
}
},
{
xtype: 'button',
text: 'Clear Data',
handler: function() {
formPanel.reset();
}
}
]
});
}
});
This is the controller event to show form panel view
onItemSwiped: function(list,index,target,record,e)
{
var addcontact= Ext.create('WinReo.view.AddContact');
Ext.Viewport.add(addcontact);
Ext.Viewport.setActiveItem(addcontact);
},
Just a simple task but am spending too much time to fix this one..please help me to solve this issue. Thanks in advance..
Your right about setActiveItem and you need to use it. because Ext.Viewport.add() only adds to viewPort not shows the view.
So only problem in your code is you created formPanel, but not added it in the AddContact View.
...........
// same code
{
xtype: 'button',
text: 'Get Data',
handler: function() {
Ext.Msg.alert('Form Values', JSON.stringify(formPanel.getValues(), null, 2));
}
},
{
xtype: 'button',
text: 'Clear Data',
handler: function() {
formPanel.reset();
}
}
]
});
this.add(formPanel); // add this line
}
});
See this fiddle

ExtJS 4.1 event delegation not working

I am using extjs 4.1 and trying to do the event delegation which in my case is to attach a blur event to all the textfields of my form and it is not working and i am not getting any error in firebug too, i don't know where i am going wrong in attaching the event, is it the wrong place where i am putting the code and also i have noticed that as per the docs below link:
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.Panel-method-on
the delegate property of the options object no longer exists. Below is my code:
Ext.onReady(function () {
var loadForm = function () {
Ext.getCmp('TestForm').getForm().setValues({ name: 'some text', email: 'first', dob: '12/12/2009' });
}
Ext.define('userForm', {
extend: 'Ext.form.Panel'
, alias: 'widget.userform'
, frame: true
, initComponent: function () {
Ext.apply(this, {
title: 'User Form'
, height: 350
, items: [{
xtype: 'textfield'
, fieldLabel: 'Name'
, name: 'name'
, id: 'nameId'
, enableKeyEvents: true
}, {
xtype: 'textfield'
, fieldLabel: 'Email'
, name: 'email'
, id: 'emailId'
}, {
xtype: 'datefield',
fieldLabel: 'DOB',
id: 'dob',
name: 'dob',
format: 'Y-m-d'
}, {
xtype: 'textfield',
fieldLabel: 'Age',
id: 'age',
name: 'age'
}, {
xtype: 'textfield',
fieldLabel: 'Guardian',
id: 'guardian',
name: 'guardian'
}]
});
this.callParent(arguments);
} //eoinitComponent
});
var userForm = Ext.create('userForm', {
renderTo: 'loadPanel',
id: 'TestForm',
listeners: {
afterrender: function (formCmp, eOpts) {
loadForm();
},
render: function (formCmp, eOpts) {
Ext.getCmp("TestForm").on(
'blur',
function (e, t) {
// handle blur
console.info(t.id);
},
this,
{
// filter the target element to be a descendant with the class '.x-form-field'
delegate: '.x-form-field'
}
);
}
}
});
});
To begin with, the on method does not take delegate as a parameter, so that line is completely unnecessary.
Then, within your render event, you can just use formCmp.on() rather than Ext.getCmp().on().
Lastly, you want blur event on every field, not on the form itself. The following code should work:
render: function (formCmp, eOpts) {
// For each field.
formCmp.getForm().getFields().each( function( aField ) {
// If it's a textfield.
if ( aField.is('textfield') ) {
aField.on( 'blur', this.onFieldBlur, this)
}
}, this ); // notice the this scope for the each method.
}

create Custom Component and re use it

i am new to ext, i want to create a custom component containing a form and use it by registering it as a xtype, where i want:
MyComponent = Ext.extend(Ext.FormPanel, {
initComponent: function () {
Ext.apply(this, {
labelWidth: 50,
// label settings here cascade unless overridden
items: [{
xtype: 'label',
text: 'Name'
},
{
xtype: 'textfield',
label: 'First Name',
name: 'firstname',
id: 'firstname',
allowBlank: true
},
{
xtype: 'label',
text: 'Last Name',
style: {
'color': 'black',
'font-size': '10px'
}
},
{
xtype: 'textfield',
label: 'lastname',
name: 'lastname',
id: 'lastname'
},
{
xtype: 'label',
text: 'Teams'
},
{
xtype: 'button',
text: 'Save',
handler: function () {},
id: 'save',
},
{
xtype: 'button',
text: 'cancel',
handler: function () { //Ext.Msg.alert('Status', 'Changes saved successfully.');
Ext.example.msg(' Click', 'You cancelled');
}
}
]
});
MyComponent.superclass.initComponent.apply(this, arguments);
},
onRender: function () {
MyComponent.superclass.onRender.apply(this, arguments);
}
});
Ext.reg('mycomponentxtype', MyComponent); /* paste in your code and press Beautify button */
if ('this_is' == /an_example/) {
do_something();
} else {
var a = b ? (c % d) : e[f];
}
When i create my own Components i do this:
Ext.define('MyApp.view.dialog.MyDialog', {
'extend' : 'Ext.window.Window',
'alias' : 'widget.MyDialog',//declare xour own xtype here
'title' : 'My Dialog',
'items' : [{
'xtype' : 'textfield',
'fieldLabel' : 'Surename',
//other config here
}] // other items here
});
Then you can say:
Ext.widget('MyDialog').show();

Resources