tags:

views:

75

answers:

2

In ExtJs I would like to achieve the equivalent of:

<input type="radio"><label><input type="text"></label>

Where the input box is associated to the radio button.

new Ext.form.RadioGroup({
                        id:"alerts",

                        items: [
                            new Ext.form.Radio({
                                boxLabel:'Now',

                            }),
                            new Ext.form.Radio({
                                boxLabel:'On',
                            })

                         ]
);

I would like the "On" label to have a Ext.form.TextField next to it.

I have tried adding a Ext.form.TextField to the RadioGroup but it will not show (as I assume its because its not a Radio) and I am unable to add items to the Radio object.

Any advice apprecaited, thanks.

A: 

I don't think that's possible unless you override the RadioButton class and change its rendering logic (and I think that would be more complex than you would think to make a textfield render correctly the way you proposed). A better approach would be to simply add the radio and the textfield as two separate fields and link them programmatically. In Ext 3.2 you could use a CompositeField to do this easily. In the radio's handler fn you can set focus to its associated textbox or whatever other logic you'd need.

bmoeskau
A: 

Add an <input/> element in the boxLabel, then on the RadioGroup render event create a TextField and apply it to that element

new Ext.form.RadioGroup({
  id:"alerts",
  items: [
    {boxLabel: 'Now',},
    {boxLabel: 'On <input id="on_date" type="text"/>'}, // contains <input/> id is "on_date"
  ],
  listeners: {
    change: function() {
      // really, check if "on" was clicked
      if(true) {
        Ext.getCmp('on_date_field').focus();
      } else {
        // otherwise, disable the field?
      }
    },
    render: function() {
      new Ext.form.TextField({
        id: 'on_date_field',
        applyTo: 'on_date', // apply textfield to element whose id is "on_date"
      });
    }
  }
});

I've confirmed that this works with TextField, and although I haven't tried it, it should work with DateField or ComboBox too. Also untested, but instead of creating an <input/>, you could create a container element and create the TextField and renderTo that element.

Jesse Dhillon
What browsers did you test this in? In Firefox, the radio element steals focus from the text field and does not allow typing into it (until you tab off of the radio). In IE, you get "fieldLabel is null or not an object". Not sure if you are the one who voted my answer down, but I did not say it would not be possible to do this, I said it would be more complex than you might think. I think this answer proves my point.
bmoeskau
I tried it in Firefox 3.6. Stealing of focus is because the label is being clicked and there is a handler on RadioGroup that tries to intercept label clicks. I edited the answer to make it work properly.I did vote you down, sorry I'm new to SO, I don't understand if that has a big impact or not, I thought I was just indicating disagreement with your answer. I tried to take it back but it wouldn't let me.
Jesse Dhillon
There is no "rule" but since down-voting actually removes reputation from the person you downvote, the purpose is normally to indicate an answer is off-topic or is actively misleading. Typically when someone is legitimately trying to offer help, even if it's not an answer you think is best, that's not really a reason to downvote (instead, maybe comment on why you think the answer is not best, thus starting a useful conversation, like this one). Maybe some other nice person will come along and up-vote me to call it even... :)
bmoeskau
BTW, this still does not work in IE (even after removing the extra commas in your code, the tell-tale sign that you did not test this in IE since it does not even "compile")
bmoeskau
I did not test in IE, as I have a Linux machine for my personal use. This is a proof of concept, not a production quality solution (yet), although I'm sure it could be with a little bit of thought and effort.
Jesse Dhillon
Yeah, again, basically proves the exact point I made in my answer above. I would strongly recommend linking two fields rather than trying to shoehorn a solution into a radio override. But that's just me.
bmoeskau
Are you just complaining because I gave you a -1? The OP wanted to know how to make this happen, and I pointed him in the right direction. As a matter of fact, this can be done with a couple of event listeners or an override to prevent focus-grabbing. Your proposal is basically "don't do it" which is why I gave you a -1. It's perfectly doable, and the fact that I didn't run it through a Selenium testing farm before posting the code doesn't prove that it's a bad idea.
Jesse Dhillon