PDA

View Full Version : Dynamically adding button with handler



stirucherai
27 Aug 2011, 7:14 AM
I am trying to add dynamically button with handler in a loop. In the handler I am using valActiveItem which is what being incremented for each looping. But when the button is added the handler with variable valActiveItem instead of actually evaulating the value of the same. I wanted to create button with nestedList.setActiveItem(0),nestedList.setActiveItem(1)....
but it gets implemented as nestedList.setActive(valActiveItem) for all the button and hence setActiveItem is evaulated only during when the button is pressed -- and hence takes same value (last count of valActiveitem) for all the button



****


listeners: {
itemtap: function(record, index,item, e){
var valActiveItem=0;
actionBar.removeAll();
var currentNode=record.getStore().getAt(index).node
while (currentNode.id != 'root')
{
//alert('Index : ' + currentNode.getActiveItem(1));
var element = new Ext.Button({
style:"background-color:red; color:white",
handler:function(){
nestedList.setActiveItem([valActiveItem]);
}
});
element.update(currentNode.attributes.record.data.text);
//element.handler('function (){nestedList.setActiveItem('+valActiveItem + ')}');
actionBar.add(element);
actionBar.doLayout();
currentNode=currentNode.parentNode;
valActiveItem=valActiveItem+1;
}
parentNode=record.getStore().getAt(index).get('text');
//alert(this.displayField);
}},
****

bharatn
27 Aug 2011, 11:15 PM
Thats because valActiveItem is evaluated at handler execution time.

What you can do specify a function reference for all button handler as

handler: btnHandler,

And then in btnHandler function check the id or text of button and change tab/card accordingly.

stirucherai
28 Aug 2011, 7:15 AM
Thanks alot. I did add a custom attribute and was able to set that to the required number (level) and use it later.