Jacob
Jacob

Reputation: 4031

Adding component to container doesn't work

I could hardly found an easier example but for some unknown reason i have problems with this few lines of code. I dynamically create buttons and add them to my container to the end.

I don't know why but only the first button is added. Please help

Code:

var buttonCount = this.getFoldersContainer().query('button').length;
var button = Ext.create('Ext.button.Button');

button.id = 'folderButton' + record.get('id');
button.setText(record.get('name') + " >>");

console.debug('count');
console.debug(buttonCount);

this.getFoldersContainer().insert(buttonCount,button);

I created a new blank project with only this functionality and it works fine. I don't have a clue what could be causing this in my existing project.

Upvotes: 0

Views: 167

Answers (1)

sra
sra

Reputation: 23973

First you should be sure that all buttons get a application wide unique id! Next is that the id should be present at construction time of the button (in your case it will not be critical but I recommend it). It makes no sense when you are saying that add() would insert at the beginning, because it always insert at the end!

// ....getFoldersContainer().query('button').length; // count all the items!!
// you may do a check if the id is unique while debugging
if(Ext.getCmp('folderButton' + record.get('id')) != null)
    console.error('Id duplicated! >> ','folderButton' + record.get('id'))
var ct = this.getFoldersContainer(),
    itemCount = ct.items.getCount(), 
    button = Ext.create('Ext.button.Button', {text:record.get('name') + " >>",id:'folderButton' + record.get('id')});

ct.insert(itemCount > 0 ? --itemCount : itemCount ,button);
// if you just want to insert at the end you will be fine with 
// ct.add(button);

Upvotes: 1

Related Questions