PDA

View Full Version : Dynamically add events to buttons



JazzMan88
26 Nov 2010, 10:28 AM
Hello, i need help. I create form in loop with comboboxes, data stores, textfields and buttons. I want to add different events for each button. I use the next logic:


....
//this count = first_data_store.getCount()
for(var i = 0; i < count; i++){
var combo = 'combo_store_' + i;
new Ext.data.Store({
id: combo,
....
});

new Ext.Container({

id: container_' + i,
items:[
{

id: 'newButton' + i,
text: 'text' + i,
//listener.......

}
]
....
});
}After create button text, I want add listener and this listener must be unique for each button which creating in loop. After "click" on button 'newButton' + i I want take data from 'combo_store_' + i. That is my main problem. If I do this:


....
{

id: 'newButton' + i,
text: 'text' + i,
listeners: function(){
someFunction();
//For example
alert(combo);
//If count = 5, it returns 'combo_store_4'
}

}
....
That's I can't add unique listener for button, because variable i equals value of last loop iteration. And addListener don't work too.
I know how to do it to use eval function, but if I need create very difficult function it takes more time.

What other methods exist?

Thanks.

Condor
27 Nov 2010, 7:15 AM
Why not simply store a reference to the combobox or combobox store in the button itself?

Example:


var comboStore = new Ext.data.Store({
....
});
new Ext.Container({
items:[{
id: 'newButton' + i,
text: 'text ' + i,
store: comboStore,
listeners: {
click: function(btn) {
var store = btn.store;
// actually, comboStore is also available from the closure
...
}
}
},{
...
}]
});

JazzMan88
28 Nov 2010, 8:01 PM
This discovery for me, I didn't know that I can add a link to date store in a button. Thank you. Now I try do this

JazzMan88
28 Nov 2010, 8:11 PM
How I can "getRawValue()" from combobox near the current button. Just this code don't work:


var v = Ext.getCmp(id_combo).getRawValue();

May be I can add link on combobox into this button?

Condor
28 Nov 2010, 11:06 PM
Can't you use the same method as I posted above, but for the combobox instead of the store?

JazzMan88
29 Nov 2010, 7:25 AM
Oh, I see)). Now I understood this, thanks