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
Fred13Fred13 

Add "All" value to Lightning:Select field

I have a component with a few Lightning:Select fields that I'm using to filter the page.  I am populating the values using an apex class to pull the values for all existing records.  See below for snipet of the apex class.  
 
//get a deduped list of group numbers 
 @AuraEnabled 
    public static List<group_structure__c> getgroupnumbers() {
        List<group_structure__c> groupstructures = 
                [SELECT Id, group_number__c FROM group_structure__c];
  
        //added to pull in group numbers
        List<group_structure__c> groupnumbers = new List<group_structure__c>();
        
        Set<String> fgroupnumbers = New Set<String>();
        
        for (group_structure__c gs : groupstructures){
		      if (fgroupnumbers.Contains(gs.group_number__c) == FALSE){
	              fgroupnumbers.add(gs.group_number__c);
	              groupnumbers.add(gs);
              }
            groupnumbers.add('all');
            system.debug('group numbers' + groupnumbers);
        }
        return groupnumbers;
    }
I'm trying to add a value of "All" which would be the default value.  In my client side controller I will then filter the overall list based on these values.  I can't figure out how to add an "All" value.  I tried to just add it in the component, however, no value gets associated to selected All when using this method (see below)
<lightning:layoutItem padding="around-small">
            <!-- Create a dropdown menu with options for Group Number-->
            <lightning:select aura:id="selectGroupNum" label="GroupNum" name="sourceGroupNum" onchange="{!c.handleSelect}">
                 <option value="">All</option>
       			 <aura:iteration items="{!v.groupnumbers}" var="gs">
            		<option value="{!gs.id}">{!gs.Group_Number__c}</option>
        		 </aura:iteration>
            </lightning:select>
     	 </lightning:layoutItem>
Any help would be greatly appreciated!!!
Thanks!!

Fred
 
Raj VakatiRaj Vakati
Give me complete code ./ .. 


Try this pls
 
//get a deduped list of group numbers 
@AuraEnabled 
public static List<String> getgroupnumbers() {
	List<group_structure__c> groupstructures = 
			[SELECT Id, group_number__c FROM group_structure__c];

	//added to pull in group numbers
           List<String> groupnumbers = new List<String>();
groupnumbers.add('All') ;
	Set<String> fgroupnumbers = New Set<String>();
	
	for (group_structure__c gs : groupstructures){
			fgroupnumbers.add(gs.group_number__c) ;   
	}
	groupnumbers.addAll(fgroupnumbers) ; 
	return groupnumbers;
}



 
Fred13Fred13
Thanks for the reply!  The issue I have now is that the actual values are null in the list.. I can see that there is a space for them when I click the dropdown, just no values.  Here is what I have:

Here is the component:
Here is the attribute (I also tried to change this to a string but still the same results)

<aura:attribute name="groupnumbers" type="Group_Structure__c[]"/>

Here is the field:

 <lightning:layoutItem padding="around-small">
            <!-- Create a dropdown menu with options for Group Number-->
            <lightning:select aura:id="selectGroupNum" label="GroupNum" name="sourceGroupNum" onchange="{!c.handleSelect}">
                 <option value="">All</option>
       			 <aura:iteration items="{!v.groupnumbers}" var="gs">
            		<option value="{!gs.id}">{!gs.Group_Number__c}</option>
        		 </aura:iteration>
            </lightning:select>
     	 </lightning:layoutItem>

Here is the helper:
 
loadGroupNumbers : function(cmp) {
        // Load all contact data
        var action = cmp.get("c.getgroupnumbers");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.groupnumbers", response.getReturnValue());
             }

            // Display toast message to indicate load status
            var toastEvent = $A.get("e.force:showToast");
            if (state === 'SUCCESS'){
                toastEvent.setParams({
                    "title": "Success!",
                    "message": " Your group numbers have been loaded successfully."
                });
            }
            else {
                toastEvent.setParams({
                        "title": "Error!",
                        "message": " Something has gone wrong."
                });
            }
            toastEvent.fire();
        });
         $A.enqueueAction(action);
    },

Here is the Apex controller:
//get a deduped list of group numbers 
 @AuraEnabled 
    public static List<String> getgroupnumbers() {
        List<group_structure__c> groupstructures = 
                [SELECT Id, group_number__c FROM group_structure__c];
  
        //added to pull in group numbers
           List<String> groupnumbers = new List<String>();
			groupnumbers.add('All') ;
        
        Set<String> fgroupnumbers = New Set<String>();
        
        for (group_structure__c gs : groupstructures){
			fgroupnumbers.add(gs.group_number__c) ;   
	}
	groupnumbers.addAll(fgroupnumbers) ; 
        system.debug('here is where its at' + groupnumbers);
        return groupnumbers;
    }


 
Fred13Fred13
One more thing.. I know that the helper is getting back the correct values (see screenshot below) but I am unsure what the actual component is getting

User-added image