function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anu Singh 40Anu Singh 40 

How Can I select different value in a picklist where picklist is in aura Iteration

Hi All
I  m new to salesforce I want to select different values from a picklist where picklist is in aura iteration. It shows Only one selected value in all picklist from aura iteration.
here is the code
 <aura:iteration items="{!v.Leadoptions}" var="fieldname">
     <tr> <td class="slds-line-height_reset">   
        <div>{!fieldname}</div> 
                 </td>
               <td class="slds-line-height_reset">
                 <lightning:select name="opportunity"  label="" aura:id="onjId" value="{!v.OppselectedValue}" onchange="{!c.changeAction}">
                 <option value="">None</option>
                <aura:iteration items="{!v.Opportunityoptions}" var="oppname">
                    <option value="{!oppname}" text="{!oppname}"/>  
                   </aura:iteration>
                      </lightning:select>
               </td>
                      
              </tr>  
               </aura:iteration>    User-added image
ShivankurShivankur (Salesforce Developers) 
Hi Anu,

Please check out below sample code for your understanding and try to fit in with your requirement:
implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
   <!--Two aura attributes defined for the 2 picklist fields--> 
    <aura:attribute name="picklistValues1" type="Object" />
    <aura:attribute name="picklistValues2" type="Object" />

   <c:SchoolPicklists sObjectName="School__c" fieldName="Type__c" picklistValues="{!v.picklistValues1}" />
   <c:SchoolPicklists sObjectName="School__c" fieldName="School_Category__c" picklistValues="{!v.picklistValues2}" />
<lightning:input aura:id="schName" name="schName" label="School Name" required="true" />
<lightning:input aura:id="" name="schDistrict" label="District" />
<lightning:input aura:id="schPhone" name="schPhone" label="Phone" />
<lightning:select aura:id="schType" name="schType" label="Type">
        <aura:iteration items="{!v.picklistValues1}" var="item">
    <option value="{!item}">{!item}</option>
</aura:iteration>

</lightning:select>
<lightning:input aura:id="schPrincipal" name="schPrincipal" label="Principal" />
<lightning:select aura:id="schCategory" name="schCategory" label="Category">
<aura:iteration items="{!v.picklistValues2}" var="items">
    <option value="{!items}">{!items}</option>
</aura:iteration>

</lightning:select>
<lightning:button variant="neutral" label="Cancel" />
<lightning:button variant="brand" label="Submit" />

</aura:component>

PicklistApexController can be implemented like below:
public class PicklistApexController {
 @AuraEnabled        
 public static List getPickListValues(String objectType, String selectedField) {
     List pkListValues = new List();
     Schema.SObjectType obj = Schema.getGlobalDescribe().get(objectType);
     Schema.DescribeSObjectResult res = obj.getDescribe();
     Schema.DescribeFieldResult result = res.fields.getMap().get(selectedField).getDescribe();
     List pkle = result.getPicklistValues();
     for( Schema.PicklistEntry pklVal : pkle){
         pkListValues.add(pklVal.getLabel());
     }     
     return pkListValues;
  }
 }

Now, you can create a service component which helps you display the values which were retrieved from the Apex:
<aura:component controller="PickListApexController" access="global">
<aura:attribute name="sObjectName" type="String" />
<aura:attribute name="fieldName" type="String" />
<aura:attribute name="picklistValues" type="Object" />   
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

</aura:component>

js controller for this service component would be like below:
({
    doInit : function(component) {
        var action = component.get("c.getPickListValues");
        action.setParams({
            objectType: component.get("v.sObjectName"),
            selectedField: component.get("v.fieldName")
        });
        action.setCallback(this, function(response) {
            var pklist = response.getReturnValue();
            component.set("v.picklistValues", pklist);            
        })
        
        $A.enqueueAction(action);
    }
})

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.​​​​​​​