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 

Component select field

I'm trying to display a select list of values from a field on one of my attributes.  I have been able to display them, however, I was wondering if there was an easy way to eliminate the duplicates.  

The attribute is  <aura:attribute name="groupstructures" type="Group_Structure__c[]"/>

The section of code for the selection is
 
<lightning:layoutItem padding="horizontal-medium" size="4">
            <!-- TEST Create a dropdown menu with options for Section code-->
            <lightning:select aura:id="selectSection" label="Section" name="sourceSection" >
       			 <aura:iteration items="{!v.groupstructures}" var="gs">
            		<option value="{!gs.id}">{!gs.Section_Code__c}</option>
        		 </aura:iteration>
            </lightning:select>
</lightning:layoutItem>


 
Best Answer chosen by Fred13
Raj VakatiRaj Vakati

Yes ..  Add it at the JS controller level .. .


OR add default   record 
 
@AuraEnabled
    public static List < Group_Structure__c > getG() 
    {
        Set<Group_Structure__c> idsSet =   new Set<Group_Structure__c>
        List<Group_Structure__c>  uni = new  List<Group_Structure__c> () ;
uni .add( new Group_Structure__c(name='all'));

		List<Group_Structure__c> ids = [Select Id ,Name from Group_Structure__c] ; 
		
		// do a for loop and remove all the duplicats and add the to Set and 
		// then convert set to list 
		// return the value 
		
		
        uni .addAll(idsSet);
        return uni ;
    }

 

All Answers

Raj VakatiRaj Vakati
Yes .. i Have an Easy way ..  

As You cannt return the Set from the @AuraEnabled methods .. you can create a set and then convert to list as shonw below 
 
@AuraEnabled
    public static List < Group_Structure__c > getG() 
    {
        Set<Group_Structure__c> idsSet =   new Set<Group_Structure__c>
        List<Group_Structure__c>  uni = new  List<Group_Structure__c> () ;
		List<Group_Structure__c> ids = [Select Id ,Name from Group_Structure__c] ; 
		
		// do a for loop and remove all the duplicats and add the to Set and 
		// then convert set to list 
		// return the value 
		
		
        uni .addAll(idsSet);
        return uni ;
    }

 
Fred13Fred13
Thanks Raj!  I have one more question for you.. is it possible to add an "all" to the list?  I just realized that now that I have the list of values, the user should also be able to see all values returned.  thanks again!
Fred
Raj VakatiRaj Vakati

Yes ..  Add it at the JS controller level .. .


OR add default   record 
 
@AuraEnabled
    public static List < Group_Structure__c > getG() 
    {
        Set<Group_Structure__c> idsSet =   new Set<Group_Structure__c>
        List<Group_Structure__c>  uni = new  List<Group_Structure__c> () ;
uni .add( new Group_Structure__c(name='all'));

		List<Group_Structure__c> ids = [Select Id ,Name from Group_Structure__c] ; 
		
		// do a for loop and remove all the duplicats and add the to Set and 
		// then convert set to list 
		// return the value 
		
		
        uni .addAll(idsSet);
        return uni ;
    }

 
This was selected as the best answer
Fred13Fred13

 

Awesome! thanks again for all of your help!

Fred

Fred13Fred13
Sorry, one small issue.. when I use your example, it adds a blank value to the list and not All.  I thought maybe because the other values are numbers so I tried 9999 instead of All but it still shows a blank selection instead of the value.  Here is what I have:
//get a deduped list of section codes
     @AuraEnabled 
    public static List<group_structure__c> getsections() {
        List<group_structure__c> groupstructures = 
                [SELECT section_code__c FROM group_structure__c];
  	
        //added to pull in group numbers
        List<group_structure__c> sections = new List<group_structure__c>();
        groupstructures.add( new Group_Structure__c(name='All'));
        Set<String> fsections = New Set<String>();
        
        for (group_structure__c sec : groupstructures){
		      if (fsections.Contains(sec.section_code__c) == FALSE){
	              fsections.add(sec.section_code__c);
	              sections.add(sec);User-added image
              }
        }
        return sections;
     }
This is what I see when I add that line:

 
Raj VakatiRaj Vakati
Can you give me an image and js code