Search Username and Create a Custom Lightning-Pill Button With Add/Remove in Salesforce Lightning Component | Search Username and Create a Custom Lightning-Pill Button in lightning component Salesforce

1,880 views


Hey guys, today in this post we are going to learn about Search Username and Create a Custom Lightning-Pill Button with Add/Remove a Pill Label in Salesforce Lightning Component.

Files we used in this post example:-

lightningPillApp.app Lightning Application It is used for call the component to preview on browser.

lightningPillCmp.cmp

Lightning Component It is used for create a search input field &  custom pill button on lightning component .

lightningPillCmpController.js

JavaScript Controller File It is used for keyup function for search the username from database.
lightningPillCmp.css Component Style CSS It is used for create custom Style CSS for alignment the search input field and pill button.
pillController.apxc Apex Controller File It is used for search username value from database server

Live Demo

lightning pill button in salesforce lightning -- w3web.net

 

You can download file directly from github by Click Here.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 

 

Other related post that would you like to learn in Salesforce

 

Step 1:- Create Lightning Application : lightningPillApp.app

From Developer Console >> File >> New >> Lightning Application

lightningPillApp.app [Component Application File]

  1.    <aura:application extends="force:slds">
  2.   <c:lightningPillCmp/>	
  3. </aura:application>

Step 2:- Create Lightning Component : lightningPillCmp.cmp

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

From Developer Console >> File >> New >> Lightning Component

lightningPillCmp.cmp [Lightning Component File]

  1.    <aura:component controller="pillController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,force:lightningQuickActionWithoutheader" access="global" >
  2.  
  3.     <aura:attribute name="searchResults" type="User[]"/>
  4.     <aura:attribute name="recipients" type="String[]"/>
  5.     <aura:attribute name="searchLookup" type="string"/>
  6.     <aura:attribute name="pillSearch" type="boolean" default="false"/>
  7.  
  8.     <div class="slds slds-p-around--medium">       
  9.  
  10.         <form class="slds-form--inline">
  11.              <div class="slds-form-element">
  12.                 <select id="recipientTypeSelectBox" class="slds-select" onchange="{!c.changeType}">     
  13.                     <option>--Select--</option>
  14.                     <option>Username</option>
  15.                     <option>Email</option>                   
  16.                 </select>
  17.             </div>
  18.  
  19.             <div class="slds-form-element">
  20.  
  21.                 <div class="slds-lookup">
  22.                     <!--______### {!v.searchLookup.length}##{!v.searchLookup}-->
  23.                     <div class="slds-form-element__control"> 
  24.                         <ui:inputText class="slds-input" aura:id="searchLookupValue" keyup="{!c.search}" updateOn="keyup"  placeholder="Search..." value="{!v.searchLookup}" /> <br/>
  25.                         <ul id="emailVerify" class="uiInputDefaultError slds-hide">
  26.                             <li class="form-element__help">Email is not Correct </li>
  27.                         </ul>
  28.                     </div>
  29.  
  30.                    <aura:if isTrue="{!v.searchLookup.length > 2}">  
  31.                      <aura:if isTrue="{!v.pillSearch}">    
  32.                     <div class="slds-lookup__menu slds-show">
  33.                         <ul class="slds-lookup__list">
  34.                             <aura:iteration items="{!v.searchResults}" var="item">
  35.                                 <li role="presentation">
  36.                                     <span class="slds-lookup__item-action 
  37.                                                  slds-lookup__item-action--label">
  38.                                         <a href="" class="slds-truncate" 
  39.                                            data-sfid="{!item.Id}" 
  40.                                            data-email="{!item.Email}"
  41.                                            onclick="{!c.searchSelect}">
  42.                                             {!item.Name}
  43.                                         </a>
  44.                                     </span>
  45.                                 </li>
  46.                             </aura:iteration>
  47.                         </ul>
  48.                     </div> 
  49.                     </aura:if>      
  50.                   </aura:if>   
  51.  
  52.                 </div>
  53.  
  54.             </div>
  55.  
  56.             <div class="slds-form-element">
  57.                 <span class="slds-hide" aura:id="addItemBtn">
  58.                     <button type="button" class="slds-button slds-button--brand"
  59.                             onclick="{!c.addToRecipients}">Add</button>
  60.                 </span> 
  61.             </div>
  62.  
  63.         </form>
  64.         <br/><br/>
  65.         <div class="slds-form-element slds-m-top--medium">
  66.             <label class="slds-form-element__label label" style="color:#b51702;">
  67.                 <strong>To Recipients</strong> {!v.recipients}
  68.             </label>
  69.             <div id="recipientItem">
  70.                 <aura:iteration items="{!v.recipients}" var="rec" indexVar="index">
  71.                     <span class="slds-pill slds-m-around--xxx-small" 
  72.                           style="{!if(rec.isAssignedBA, 'background-color:rgba(0,103,41,0.38)', '')}">
  73.                         <label class="slds-pill__label">{!rec.Name}</label>
  74.                         <aura:if isTrue="{!not(rec.isDefault)}">
  75.                             <button class="slds-button slds-button--icon slds-pill__remove"
  76.                                     onclick="{!c.removeRecipient}" data-index="{!index}">
  77.                                 X
  78.                             </button>
  79.                         </aura:if>
  80.                     </span>
  81.                 </aura:iteration>
  82.             </div>
  83.         </div>
  84.  
  85.   <br/> <br/>
  86.    <!--Start RelatedTopics Section-->
  87. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  88.  
  89.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  90.  
  91.             <br/><br/>
  92.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
  93.             <div style="display:block; overflow:hidden;"> 
  94.                 <div style="width: 50%; float:left; display:inline-block">
  95.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  96.                         <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  97.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  98.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  99.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  100.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  101.                     </ul>
  102.             </div>
  103.  
  104.             <div style="width: 50%; float:left; display:inline-block">
  105.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  106.                         <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  107.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  108.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  109.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  110.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  111.                     </ul>
  112.                 </div>
  113.                <div style="clear:both;"></div> 
  114.                <br/>
  115.                 <div class="youtubeIcon">
  116.                     <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  117.                 </div>
  118.     </div>
  119.  
  120. </div>
  121.  
  122.   <!--End RelatedTopics Section-->       
  123.  
  124.     </div>  
  125. </aura:component>

Step 3:- Create Lightning Component : lightningPillCmpController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

lightningPillCmpController.js [JavaScript Controller]

  1.   ({
  2.     search : function(component, event, helper) {
  3.         var thisObjVal = event.getSource().get('v.value');
  4.         var thisObjValLnth = thisObjVal.length;
  5.         var srchLookupVal = component.find('srchLookupVal');
  6.         //alert(thisObjVal + thisObjValLnth);        
  7.         if(thisObjVal && thisObjValLnth>2){
  8.             var action = component.get("c.searchUsersName"); 
  9.             action.setParams({'key':thisObjVal});
  10.             action.setCallback(this, function(response){
  11.                 var state =  response.getState();
  12.                 if(state == 'SUCCESS'){
  13.                    var result = response.getReturnValue();                     
  14.                     if(result && result.length>0){
  15.                         component.set('v.searchResults',result);
  16.                         component.set('v.pillSearch',true);
  17.                        //$A.util.addClass(srchLookupVal,'slds-show'); 
  18.  
  19.                     }else{
  20.                         component.set('v.searchResults',"");
  21.                         $A.util.removeClass(srchLookupVal,'slds-show');
  22.                     }
  23.  
  24.  
  25.                 }
  26.             });
  27.             $A.enqueueAction(action);
  28.  
  29.         }
  30.  
  31.     },
  32.  
  33.     searchSelect:function(component, event, helper){
  34.         var thisObjVal = event.target.innerText;
  35.         var srchLookupVal = component.find('srchLookupVal');
  36.         var recipients = component.get("v.recipients");       
  37.         recipients.push({
  38.             Name: thisObjVal
  39.         }); 
  40.  
  41.          var recipientsNoDupes = [];        
  42.         for(var i = 0; i < recipients.length; i++) {
  43.             recipientsNoDupes.push(recipients[i]);
  44.         }
  45.         component.set("v.recipients", recipientsNoDupes);   
  46.          component.set('v.pillSearch',false);
  47.        // $A.util.removeClass(srchLookupVal,'slds-show');
  48.         component.set('v.searchLookup','');
  49.     },
  50.  
  51.     removeRecipient:function(component, event, helper){
  52.         var eventDataSet = event.target.dataset.index;
  53.         //alert('eventDataSet ' + eventDataSet);
  54.         var recipients = component.get('v.recipients');
  55.         recipients.splice(Number(eventDataSet),1);
  56.         component.set("v.recipients", recipients);
  57.     },
  58.  
  59.     addToRecipients:function(component, event, helper){
  60.          var regExpEmailformat = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;  
  61.          var recipients = component.get("v.recipients");  
  62.          var searchLookup = component.get("v.searchLookup");
  63.  
  64.         if(searchLookup){
  65.             if(searchLookup.match(regExpEmailformat)){
  66.                 document.getElementById("emailVerify").classList.add("slds-hide");
  67.                 var recipients = component.get("v.recipients");
  68.                 recipients.push({                    
  69.                     Name: searchLookup,
  70.                     Email: searchLookup,
  71.                     Id: ''
  72.                 });
  73.                 var recipientsNoDupes = [];
  74.  
  75.                 for(var i = 0; i < recipients.length; i++) {
  76.                     recipientsNoDupes.push(recipients[i]);
  77.                 }
  78.                 //alert('recipientsNoDupes ' + recipientsNoDupes);
  79.                 component.set("v.recipients", recipientsNoDupes);
  80.                 //alert('recipients ' + JSON.stringify(recipients));
  81.                 component.set("v.searchLookup","");
  82.             }
  83.              else{
  84.                 document.getElementById("emailVerify").classList.remove("slds-hide");
  85.             }
  86.         }
  87.  
  88.     },  
  89.  
  90.     changeType:function(component, event, helper){
  91.         var thisObjId = document.getElementById('recipientTypeSelectBox').value;
  92.         var addItemBtn = component.find('addItemBtn');
  93.         if(thisObjId == 'Email'){
  94.             $A.util.removeClass(addItemBtn,'slds-hide');
  95.         }else{
  96.             $A.util.addClass(addItemBtn,'slds-hide');
  97.         }
  98.     },   
  99.  
  100.  
  101. })

Step 4:- Create Lightning Component Style: lightningPillCmp.css

From Developer Console >> File >> New >> Lightning Component >> Component Style CSS

lightningPillCmp.css [Style CSS]

  1.   .THIS {
  2. }
  3. .THIS .slds-form-element__control .uiInputDefaultError{border:2px #c23934 solid; border-radius: 3px; position:absolute;  padding: 0 0 5px 5px; top: 44px;
  4.     width: 185px;}
  5. .THIS .slds-form-element__control .uiInputDefaultError .form-element__help:before{content: ''; width: 0; height: 0; border-left: 5px solid transparent; border-bottom: 10px solid #c23934;
  6.     border-right: 5px solid transparent; position: absolute; left: 0px; top:-11px;}

Step 5:- Create Apex Class : pillController.apxc

From Developer Console >> File >> New >> Apex Class

pillController.apxc [Apex Class Controller]

  1.   public class pillController {
  2.    @AuraEnabled
  3.     public static List<User> searchUsersName(String KEY){
  4.         KEY = '%' + KEY + '%';        
  5.         List<User> getUserList = [SELECT Id, Name, Email FROM USER WHERE Name LIKE:KEY AND isActive=TRUE];
  6.         RETURN getUserList;
  7.     }
  8.  
  9. }

 

lightning pill button in salesforce -- w3web.net

 

Further post that would you like to learn in Salesforce

 
 

 

 

FAQ (Frequently Asked Questions)

How do you remove pills from lightning pill containers?

To remove a pill, press the Delete or Backspace key when the pill receives focus. On mobile devices, you can tap the remove button to remove a pill.

How do you remove a pill from LWC?

Press the Tab key to focus on a pill. To remove a pill, press the Delete or Backspace key on the pill when it receives focus. Alternatively, press the Enter key when you tab to the remove button.

What is the advantage of Salesforce Lightning?

One of the major Salesforce Lightning advantages is that it's a component-based platform. In other words, you can simply integrate the components already built by the Salesforce partners and use them in your application. The best part is that you merely need to drag and drop the components to use them.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment