1

I'M trying to display radio groups inside aura iteration,the number relays on the number of elements of the Questions list,but not only selected options are not being checked ,it also happens that when I try to store the selected values of all questions,they are being stored in the first element of another list called radioGrpValue list . Thing is,i dont know how to iterate over that list to store the rest of the answers so i can use them in a controller, since the index of aura iteration component cannot be used like that. Since I have three questions ,i would like to store the three answers in the radioGrpValue list. Any thoughts on this issue?

<aura:attribute name="options" type="List" default="[
         {'label': 'very good', 'value': 'very good'},
         {'label': 'good', 'value': 'good'},
         {'label': 'Regular', 'value': 'Regular'},
         {'label': 'bad', 'value': 'bad'}]"/>


<aura:attribute name="questions" type="List" default="[
         {'label': 'Question1', 'value': 'radioButtonGroup'},
         {'label': 'Question2', 'value': 'radioButtonGroup2'},
         {'label': 'Question3', 'value': 'radioButtonGroup3'}]"/>

<aura:attribute name="radioGrpValue" type="list" default="[]"/> 

<aura:iteration items="{!v.questions}" var="quest" indexVar="index">

<lightning:radioGroup 
    aura:id="mygroup"
    name="{!quest.name}"
    label="{!quest.label}"

    options="{! v.options }"
    value="{! v.radioGrpValue}"
/>

</aura:iteration>    

2 Answers 2

1

You can directly create a property selected in questions and get it onchange.

HTML:

<aura:attribute name="options" type="List" default="[
                                                        {'label': 'very good', 'value': 'very good'},
                                                        {'label': 'good', 'value': 'good'},
                                                        {'label': 'Regular', 'value': 'Regular'},
                                                        {'label': 'bad', 'value': 'bad'}]"/>


    <aura:attribute name="questions" type="List" default="[
         {'label': 'Question1', 'value': 'radioButtonGroup','selected':''},
         {'label': 'Question2', 'value': 'radioButtonGroup2','selected':''},
         {'label': 'Question3', 'value': 'radioButtonGroup3','selected':''}]"/>


    <aura:iteration items="{!v.questions}" var="quest" indexVar="index">

        <lightning:radioGroup 
                              aura:id="mygroup"
                              name="{!quest.value}"
                              label="{!quest.label}"
                              onchange="{!c.handleChange}"
                              options="{! v.options }"
                              value="{! quest.selected}"
                              />

    </aura:iteration> 

JS:

({  
    handleChange: function(component, event, helper) {
        console.log(JSON.stringify(component.get("v.questions")));
    }
})

You will see:

[{"label":"Question1","value":"radioButtonGroup","selected":"Regular"},{"label":"Question2","value":"radioButtonGroup2","selected":"very good"},{"label":"Question3","value":"radioButtonGroup3","selected":"Regular"}]

3
  • aww men, i was trying to do it with a new attribute,but didn't know the name, i was trying to set the field with default as a name. thank you very much Commented Aug 6, 2019 at 19:02
  • Do you know why i cannot acces selected values in controller,if i use q1=component.get("v.questions[0].selected"), I can see the value in logs,but in validations q1 is undefined. Commented Aug 6, 2019 at 19:45
  • it should be component.get("v.questions")[0].selected Commented Aug 7, 2019 at 1:55
0

Your selection values will need to be stored in the "questions" list along with the questions:

value="{!quest.answer}"

Which you can then extract from the list via Array.map:

var qAndA = component.get("v.questions").map(q => ({ question: q.label, answer: q.answer }));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .