[SalesForce] Lightning RadioGroup not getting preselected value

I've a problem loading selected value in a radio group. This is my code

<lightning:radioGroup
         aura:id="{!question.Id}"
         name="{!question.Id}"
         label="{!question.E3_Question__c}"
         options="{!question.domain}"
         value="{!question.E3_Answer__c}"
         />

question.domain are the options of the radio button. Something like this

[{label:"Yes", value:100}, {label:"No", value:0}]

This question is displayed correctly and stored in question.E3_Answer__c field. But when I load this questions I want that the radiogroup load the answer and select the options. But this is not happening. ¿What could be the problem?.

Best Answer

You code will be looking like this

<aura:component controller="QuestionAnswerClass" access="global" >  
      <aura:attribute name="Questions" type="List"/>  
   <aura:attribute name="radioGrpValue" type="List"/>   
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>  
   <aura:iteration items="{!v.Questions}" var="ques">  
     <lightning:radioGroup name="{!ques.QuestionText__c}"  
                           label="{! ques.QuestionText__c}"   
                           options="{!ques.Answers__r}"  
                           value="{! v.radioGrpValue}"  
                           type="radio"  
                           required="true"/>  
   </aura:iteration>   
 </aura:component>

For detailed explanation, refer my blog post: Displaying lightning:radioGroup dynamically from SOQL query

Related Topic