PDA

View Full Version : getComponent fetches from wrong tab, Anyone know why?



mice-pace
13 Jan 2013, 6:00 PM
I'm having a problem accessing items by getComponent() on a series of dynamically generated tabs:


i generate the tabs inside the tab-panel with this code:



var data = job.variations;


var first = "";


if (data.length <= 0) {//alert("no variations");
}else {
//alert(data.length+" variations!");


var variations = Ext.getCmp('displayVar');


variations.removeAll();
variations.add(Ext.create('MyApp.view.varPanel', {title: 'New'}));


for (var count = 0; count < data.length; count++)
{
var tab = Ext.create('MyApp.view.varPanel', {title: data[count].title, id:("var_"+count)});
if (first==="") first = tab;
if (title==data[count].title) first = tab;
tab.variationno = count;
tab.setup(data[count]);
variations.add(tab);
}
first.show();
}




And here's a summarised definition of the class i am using for the tabs:





Ext.define('MyApp.view.varPanel', {
extend: 'Ext.panel.Panel',
alias: 'widget.varPanel',


requires: [
'MyApp.view.scopeGrid'
],


layout: {
type: 'border'
},
bodyPadding: 10,
title: '+ New',


initComponent: function() {
var me = this;


Ext.applyIf(me, {
items: [
{
xtype: 'container',
region: 'north',
height: 100,
itemId: 'details',
layout: {
type: 'column'
},
items: [
{
xtype: 'container',
columnWidth: 0.25,
hidden: true,
itemId: 'approved',
items: [
{
xtype: 'container',
itemId: 'approvedDetails',
margin: '0 0 30 0',
layout: {
align: 'stretch',
type: 'hbox'
},
items: [
{
xtype: 'displayfield',
itemId: 'approvedDate',
value: 'N/A',
fieldLabel: 'Approved',
labelWidth: 80
},
{
xtype: 'displayfield',
hidden: true,
itemId: 'approvedBy',
padding: '0 0 0 5',
value: 'D.J.Vida',
fieldLabel: 'by',
labelSeparator: ' ',
labelWidth: 15
}
]
},
{
xtype: 'displayfield',
itemId: 'addedTo',
value: 'N/A',
fieldLabel: 'Added To',
labelWidth: 80
}
]
},
{
xtype: 'container',
columnWidth: 0.25,
itemId: 'column1',
items: [
{
xtype: 'datefield',
itemId: 'date',
margin: '0 0 30 0',
width: 250,
inputId: 'date',
fieldLabel: 'Date',
labelWidth: 60
},
{
xtype: 'container',
itemId: 'buttons',
padding: '0 10 0 10',
layout: {
type: 'anchor'
},
items: [
{
xtype: 'button',
checkForm: function() {
return true;
},
anchor: '50%',
itemId: 'createButton',
text: 'Create',
listeners: {
click: {
fn: me.onCreateButtonClick,
scope: me
}
}
},
{
xtype: 'button',
anchor: '50%',
hidden: true,
itemId: 'approveButton',
text: 'Approve',
listeners: {
click: {
fn: me.onApproveButtonClick,
scope: me
}
}
},
{
xtype: 'button',
anchor: '50%',
text: 'Print'
}
]
}
]
},
{
xtype: 'textareafield',
columnWidth: 0.5,
itemId: 'description',
inputId: 'description',
fieldLabel: 'Description:',
labelAlign: 'top'
}
]
},
{
xtype: 'scopeGrid',
hidden: true,
itemId: 'tasks',
title: 'Job Procedure',
region: 'center'
},
{
xtype: 'container',
region: 'south',
height: 20,
hidden: true,
itemId: 'tasksTool',
items: [
{
xtype: 'toolbar',
items: [
{
xtype: 'tbfill'
},
{
xtype: 'tbseparator'
},
{
xtype: 'button',
text: 'Add'
},
{
xtype: 'tbseparator'
},
{
xtype: 'button',
text: 'Remove'
}
]
}
]
},
{
xtype: 'hiddenfield',
region: 'west',
itemId: 'variationNo',
width: 150,
fieldLabel: 'Label'
}
],
listeners: {
activate: {
fn: me.onPanelActivate,
scope: me
},
show: {
fn: me.onPanelShow,
scope: me
}
}
});


me.callParent(arguments);
},


onCreateButtonClick: function(button, e, options) {
//code here
},


onApproveButtonClick: function(button, e, options) {
//code here
},




onPanelShow: function(abstractcomponent, options) {
alert(abstractcomponent.title);
var details = abstractcomponent.getComponent('details');


var column1 = details.getComponent('column1');
var approved = details.getComponent('approved');


var buttons = column1.getComponent('buttons');


var variationDate = column1.getComponent('date');
abstractcomponent.beforeLoad(variationDate, this);
abstractcomponent.afterLoad(variationDate, this);


var variationDescription = details.getComponent('description');
abstractcomponent.beforeLoad(variationDescription, this);
abstractcomponent.afterLoad(variationDescription, this);


},


setup: function(data, num) {
//alert("data received, loading...");


//alert(this.title);


var details = this.getComponent('details');


var column1 = details.getComponent('column1');
var approved = details.getComponent('approved');


var buttons = column1.getComponent('buttons');


buttons.getComponent('createButton').hide();
buttons.getComponent('approveButton').show();


this.getComponent('tasks').show();
this.getComponent('tasksTool').show();


approved.show();


if (data.approved.approveddate !== null){
var approvedDetails = approved.getComponent('approvedDetails');
approvedDetails.getComponent('approvedDate').setRawValue(dateConvert(data.approved.approveddate, 'dd/mm/yyyy'));


var by = approvedDetails.getComponent('approvedBy');
by.show();
by.setRawValue(data.approved.approvedby);


approved.getComponent('addedTo').setRawValue(data.approved.addedto);
buttons.getComponent('approveButton').disable();
}


if (data.date !== null){
var variationDate = column1.getComponent('date');
variationDate.setRawValue(data.date);
}


if (data.description !== null && data.description !== ""){
var variationDescription = details.getComponent('description');
variationDescription.setRawValue(data.description);
}




},


beforeLoad: function(field, form) {
//
},


afterLoad: function(field, form) {
console.log("afterLoad");


alert(field.getValue());




}


});


The data is loaded fine, but when i switch between the panels it displays the title of that tab (onShow alerts the correct value), but instead of displaying the details for that tab's field, for some reason the field information displayed (alerted from afterLoad) is always for the first tab instead of the tab selected (which is the one whose title is displayed).

41247

(They don't alert at the same time, I put this together just to demonstrate)

mitchellsimoens
15 Jan 2013, 9:14 AM
It will find the first component with itemId or id of 'date'. So if it's not finding what you want then maybe your expecting something more than what will happen. I personally don't use getComponent but use ComponentQuery to target components...


cnt.down('combobox')

mice-pace
15 Jan 2013, 4:55 PM
down( [selector] )
Retrieves the first descendant of this container which matches the passed selector


^^^ Is having the same problem as getComponent. When i alert the title in OnPanelShow i get the right value, so i assume i have the right component... even if i grab the tab by it's unique id i have the same dilemma...

It's definitely the component from the 'First' tab, not some default value or something, because when i change the values on that tab's fields all of the alerts change

siggi
16 Jan 2013, 7:42 AM
I had a similar problem.

I solved it by getting the active tab of the tab bar and doing a ComponentQuery.




var activetab=Ext.ComponentQuery.query(/*the tabbar*/)[0].getActiveTab();

var component=Ext.ComponentQuery.query(/*the component to search*/,activetab)[0]



This did the trick for me

mice-pace
17 Jan 2013, 3:39 PM
Nope, same problem there too siggi :-?

Here's what the code now looks like with the other things i have tried commented out:


alert(abstractcomponent.title);
//alert(abstractcomponent.variationno);


//alert(this.title);
//var tab = Ext.getCmp("var_"+abstractcomponent.variationno);
var tab = Ext.getCmp('displayVar').getActiveTab();
alert("TAB2: "+tab.title);
//var details = abstractcomponent.down('#details');
var details = Ext.ComponentQuery.query('#details', abstractcomponent)[0];


//var column1 = details.down('#column1');
//var approved = details.down('#approved');
var column1 = Ext.ComponentQuery.query('#column1', details)[0];
var approved = Ext.ComponentQuery.query('#approved', details)[0];
var buttons = Ext.ComponentQuery.query('#buttons', column1)[0];


//var variationDate = column1.down('#date');
var variationDate = Ext.ComponentQuery.query('#date', column1)[0];
alert("A: "+variationDate.getRawValue());
abstractcomponent.beforeLoad(variationDate, this);
abstractcomponent.afterLoad(variationDate, this);


var details2 = Ext.ComponentQuery.query('#details', tab)[0];
var column12 = Ext.ComponentQuery.query('#column1', details2)[0];
var variationDate2 = Ext.ComponentQuery.query('#date', column12)[0];
alert("B: "+variationDate2.getRawValue());


//var variationDescription = details.down('#description');
var variationDescription = Ext.ComponentQuery.query('#description', details)[0];
abstractcomponent.beforeLoad(variationDescription, this);
abstractcomponent.afterLoad(variationDescription, this);


My best guess is it might have something to do with the fact that they shall all the same xtype (I change the title and the id when i instantiate them so the title alert should work, but i don't change anything else) But i don't think that is meant to cause problems ...the only other remote possibility is the selectors i'm using.... When i used down() and componentquery i was still selecting by ItemId (using a # at the start of the selector)... That's not wrong is it?

mice-pace
22 Jan 2013, 2:59 PM
...Reusing a custom class shouldn't cause this problem, should it?