1

I need to click on a radiofield and check it, click another time on the same radiofield and uncheck it.

My code doesen't work because the getChecked() is always true! This is my function, triggered on click event. My extjs version is 6.2.1.167

listeners: {
    click: {
        element: 'element',
        fn: function (event, target) {
            var radioField = this.component;
            if (radioField.getChecked()) {
                radioField.setChecked(false);
            }
            else {
                radioField.setChecked(true);
            }
        }
    }
},
  • Why using radio and not checkbox? – MrLine Mar 29 '19 at 12:37
  • @LenapCapo you are right, i know... but it not depends on me – Cristina Moretti Mar 29 '19 at 13:26
  • @CristinaMoretti, there is no click event on a modern radiofield. It is a check event and will always, always return true. Even attempting to use a change event will not work because change on radiofields only trigger when another radio field is checked. You need to create a custom override to either a) introduce a custom event or b) override an existing event. Here is the documentation on the radiofield. https://docs.sencha.com/extjs/6.2.1/modern/Ext.field.Radio.html – aallord Apr 02 '19 at 15:29

2 Answers2

1

Your example works in my case.

Sencha Fiddle

But as @LenapCapo said, you are trying to use radio boxes for other purposes. If you need a round checkbox, better redefine checkbox styles.

pvlt
  • 1,843
  • 1
  • 10
  • 19
  • please can you try with version 6.2.1.167 modern? it doesen't work with this version... – Cristina Moretti Mar 29 '19 at 15:04
  • Yes, on 6.2.1 this does not work. I suggest you use the following solution `{ xtype: 'checkboxfield', name: 'color', cls: 'x-radiofield', value: 'red', label: 'Red', checked: true }` https://fiddle.sencha.com/#view/editor&fiddle/2r49 – pvlt Mar 30 '19 at 10:10
  • Thanks you are the best. can you help me for the last time please? How should I do to enable just single selection? https://fiddle.sencha.com/#view/editor&fiddle/2r4t – Cristina Moretti Apr 01 '19 at 08:36
  • Remove the property `checked` from the needed field if you mean the initial state. If you want to be able to select only one field when switching, you need the radiofield https://docs.sencha.com/extjs/6.2.1/modern/Ext.field.Radio.html – pvlt Apr 01 '19 at 09:10
  • so isn't possible to select only one field and have the possibility to deselect? – Cristina Moretti Apr 01 '19 at 09:14
0

Are you sure you're checking the correct function on the radio? I've seen it be

radioField.checked

Or are you using your own method of checking the radio?

thecoolwinter
  • 861
  • 7
  • 22