Thursday, 21 September 2017

SLDS - ui:inputselect example to display values from salesforce

define  component like this,

<aura:component controller="NewCampaignController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

 <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="CampSearchDataListWrp" type="NewCampaignController.CampSearchDataListWrapper" />


<ui:inputSelect class="multiple uiInputSelect"  multiple="true" aura:id="ContactType" value="{!v.CampSearchDataListWrp.ContactType}" required="true"/>


<ui:inputSelect aura:id="sic" label="SIC" change="{!c.onSicChange}">
                        <aura:iteration items="{!v.groupAccount.sicCodes}" var="level">
                            <ui:inputSelectOption text="{!level}" label="{!level}"/>
                        </aura:iteration>
                    </ui:inputSelect>


</aura:component>


controller class will be like,

 //get data from apex
          var inputsel = component.find("ContactType");
      var opts=[];
         var action2 = component.get("c.getContactType");
       /* action2.setCallback(this, function(a) {         
        for(var i=0;i< a.getReturnValue().length;i++){
            console.log('value: '+a.getReturnValue()[i]);
            opts.push({"class": "optionClass", label: a.getReturnValue()[i], value: a.getReturnValue()[i]});
        }
        inputsel.set("v.options", opts);
console.log('response data: '+JSON.stringify(response.getReturnValue()));
    });*/
        action2.setCallback(this, function(response) {
            if(component.isValid() && response.getState() === "SUCCESS" ) {
                try{
                console.log('response data: '+JSON.stringify(response.getReturnValue()));
                component.set("v.CampSearchDataListWrp",response.getReturnValue());
                var data=component.get("v.CampSearchDataListWrp");
                    var dt=data.ContactType;
                    for(var x in dt){
                console.log('data: '+dt[x]);
                        opts.push({"class": "optionClass", label: dt[x], value: dt[x]});
                    }
                     inputsel.set("v.options", opts);   
                }catch(err){
                    console.log('Exception:'+err.stack);
                }
            }
        });
        $A.enqueueAction(action2);       



apex controller is like,

global with sharing class NewCampaignController {

  global class CampSearchDataListWrapper{
                                                                     @AuraEnabled
                                                                     public String EmailType{get;set;}
                                                                     @AuraEnabled
                                                                     public String CustomerClass{get;set;}
                                                                     @AuraEnabled
                                                                     public String MemberStatus{get;set;}
                                                                     @AuraEnabled
                                                                     public List<String> ContactType{get;set;}
                                                                   
                                                                 }
                                                               
                                                                 @AuraEnabled
                                                                 public static CampSearchDataListWrapper getContactType(){
                                                                     Map<String,String> dupmap=new Map<String,String>();
                                                                   
                                                                     //get all contact types
                                                                     String contacttypes='';
                                                                     for(Contact c:[SELECT Contact_Types__c FROM Contact where Contact_Types__c!=null]){
                                                                       
                                                                          contacttypes+=c.Contact_Types__c+';';
                                                                           
                                                                          }
                                                                 
                                                                     List<String> ctlist=new List<String>();
                                                                 if(contacttypes.contains(';')){
                                                                         for(String s:contacttypes.split(';')){
                                                                             if(s!='' && s!=null){
                                                                                  if(!dupmap.containsKey(s)){
                                                                                  ctlist.add(s);
                                                                                       dupmap.put(s,s);
                                                                                  }
                                                                             }
                                                                         }
                                                                 }
                                                                 System.debug('ctlist :'+ctlist);
                                                                    CampSearchDataListWrapper csw=new CampSearchDataListWrapper();
                                                                     csw.ContactType=ctlist;
                                                                     return csw;
                                                                 }
}




No comments:

Post a Comment