PDA

View Full Version : get component by name



sofway
23 Apr 2010, 9:11 AM
Hi,
I can get a reference to a component with the id
Ext.getCmp('id_component')

But with radios, i cant use id, i have to use name. Then i need to get the radio checked.
Ext.getCmp('name_componentí) doesnt return.

Any advise?
thanks

jtpacheco
23 Apr 2010, 9:20 AM
Your radio buttons should first be in a radio group, yes? Then that radio group should be in a form, yes?

To get their values, you call Ext.getCmp(<id of component holding the form>).getForm().getValues()[<id of radio group>];

If <id of radio group> doesn't work, try <name of radio group>. Although I'm 90% certain it is obtained by id, like every other component in Ext

sofway
23 Apr 2010, 9:37 AM
Thanks jtpacheco,
Im not using radiogroup because i need each radio with a label and a textfield besides.
Any advise to achieve this estructure?

jtpacheco
23 Apr 2010, 9:42 AM
Hmm. You still should be able to set a unique id for each radio button, and use getForm().getValues()[<id of a radio button>] in a loop to get the value of each one.

I think you may want to use a checkbox, though. I'm under the impression that unless a collection of radio buttons is in a radiogroup, they have no "exclusiveness" and therefore you'd be able to select multiple ones. Hence, a checkbox...

But its your preference. But you should still be able to set an id for the radio button itself.

anjelika
25 May 2010, 7:12 AM
Hi,
I'm having the same problem (I have few radios without a radio group) and I'm unable to get their values.
You found any solution to this in the mean time?
Thanks

jtpacheco
25 May 2010, 7:15 AM
What have you tried? Ideally, all you should need to do is
Ext.getCmp(<id of radio>).getValue() which will either return true or false.

anjelika
25 May 2010, 7:17 AM
Thanks for the prompt reply, but I don't have an id for the radios, only a name. Do I have to put the same id for all of them? I think this will get me some javascript problems.

jtpacheco
25 May 2010, 7:30 AM
Well, that's why radio groups were created :)

If you don't want to (or can't) use radio groups, you need to give each radio button an individual id (like id : radio1), then use the getCmp like I said.

There is a way to get it by name, but its a lot longer, slower, and a bit more complex, as it involves looping through every item that the radio's parent contains, then comparing the name of the item to the name that you want. Slow, expensive, and easy to work around, as there should be no trouble assigning an id to a compnent.

anjelika
25 May 2010, 7:34 AM
I managed to give each radio a unique ID and it works fine, thanks so much:)

jtpacheco
25 May 2010, 7:35 AM
Great! Glad I could help.