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.
- Don’t forget to check out:- Custom record search and delete functionality in lightning component Click Here For More Information
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
You can download file directly from github by Click Here.
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]
|
<aura:application extends="force:slds">
<c:lightningPillCmp/>
</aura:application>
Step 2:- Create Lightning Component : lightningPillCmp.cmp
From Developer Console >> File >> New >> Lightning Component
lightningPillCmp.cmp [Lightning Component File]
|
<aura:component controller="pillController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,force:lightningQuickActionWithoutheader" access="global" >
<aura:attribute name="searchResults" type="User[]"/>
<aura:attribute name="recipients" type="String[]"/>
<aura:attribute name="searchLookup" type="string"/>
<aura:attribute name="pillSearch" type="boolean" default="false"/>
<div class="slds slds-p-around--medium">
<form class="slds-form--inline">
<div class="slds-form-element">
<select id="recipientTypeSelectBox" class="slds-select" onchange="{!c.changeType}">
<option>--Select--</option>
<option>Username</option>
<option>Email</option>
</select>
</div>
<div class="slds-form-element">
<div class="slds-lookup">
<!--______### {!v.searchLookup.length}##{!v.searchLookup}-->
<div class="slds-form-element__control">
<ui:inputText class="slds-input" aura:id="searchLookupValue" keyup="{!c.search}" updateOn="keyup" placeholder="Search..." value="{!v.searchLookup}" /> <br/>
<ul id="emailVerify" class="uiInputDefaultError slds-hide">
<li class="form-element__help">Email is not Correct </li>
</ul>
</div>
<aura:if isTrue="{!v.searchLookup.length > 2}">
<aura:if isTrue="{!v.pillSearch}">
<div class="slds-lookup__menu slds-show">
<ul class="slds-lookup__list">
<aura:iteration items="{!v.searchResults}" var="item">
<li role="presentation">
<span class="slds-lookup__item-action
slds-lookup__item-action--label">
<a href="" class="slds-truncate"
data-sfid="{!item.Id}"
data-email="{!item.Email}"
onclick="{!c.searchSelect}">
{!item.Name}
</a>
</span>
</li>
</aura:iteration>
</ul>
</div>
</aura:if>
</aura:if>
</div>
</div>
<div class="slds-form-element">
<span class="slds-hide" aura:id="addItemBtn">
<button type="button" class="slds-button slds-button--brand"
onclick="{!c.addToRecipients}">Add</button>
</span>
</div>
</form>
<br/><br/>
<div class="slds-form-element slds-m-top--medium">
<label class="slds-form-element__label label" style="color:#b51702;">
<strong>To Recipients</strong> {!v.recipients}
</label>
<div id="recipientItem">
<aura:iteration items="{!v.recipients}" var="rec" indexVar="index">
<span class="slds-pill slds-m-around--xxx-small"
style="{!if(rec.isAssignedBA, 'background-color:rgba(0,103,41,0.38)', '')}">
<label class="slds-pill__label">{!rec.Name}</label>
<aura:if isTrue="{!not(rec.isDefault)}">
<button class="slds-button slds-button--icon slds-pill__remove"
onclick="{!c.removeRecipient}" data-index="{!index}">
X
</button>
</aura:if>
</span>
</aura:iteration>
</div>
</div>
<br/> <br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<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>
<br/><br/>
<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>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<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>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</aura:component>
Step 3:- Create Lightning Component : lightningPillCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
lightningPillCmpController.js [JavaScript Controller]
|
({
search : function(component, event, helper) {
var thisObjVal = event.getSource().get('v.value');
var thisObjValLnth = thisObjVal.length;
var srchLookupVal = component.find('srchLookupVal');
//alert(thisObjVal + thisObjValLnth);
if(thisObjVal && thisObjValLnth>2){
var action = component.get("c.searchUsersName");
action.setParams({'key':thisObjVal});
action.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var result = response.getReturnValue();
if(result && result.length>0){
component.set('v.searchResults',result);
component.set('v.pillSearch',true);
//$A.util.addClass(srchLookupVal,'slds-show');
}else{
component.set('v.searchResults',"");
$A.util.removeClass(srchLookupVal,'slds-show');
}
}
});
$A.enqueueAction(action);
}
},
searchSelect:function(component, event, helper){
var thisObjVal = event.target.innerText;
var srchLookupVal = component.find('srchLookupVal');
var recipients = component.get("v.recipients");
recipients.push({
Name: thisObjVal
});
var recipientsNoDupes = [];
for(var i = 0; i < recipients.length; i++) {
recipientsNoDupes.push(recipients[i]);
}
component.set("v.recipients", recipientsNoDupes);
component.set('v.pillSearch',false);
// $A.util.removeClass(srchLookupVal,'slds-show');
component.set('v.searchLookup','');
},
removeRecipient:function(component, event, helper){
var eventDataSet = event.target.dataset.index;
//alert('eventDataSet ' + eventDataSet);
var recipients = component.get('v.recipients');
recipients.splice(Number(eventDataSet),1);
component.set("v.recipients", recipients);
},
addToRecipients:function(component, event, helper){
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,}))$/;
var recipients = component.get("v.recipients");
var searchLookup = component.get("v.searchLookup");
if(searchLookup){
if(searchLookup.match(regExpEmailformat)){
document.getElementById("emailVerify").classList.add("slds-hide");
var recipients = component.get("v.recipients");
recipients.push({
Name: searchLookup,
Email: searchLookup,
Id: ''
});
var recipientsNoDupes = [];
for(var i = 0; i < recipients.length; i++) {
recipientsNoDupes.push(recipients[i]);
}
//alert('recipientsNoDupes ' + recipientsNoDupes);
component.set("v.recipients", recipientsNoDupes);
//alert('recipients ' + JSON.stringify(recipients));
component.set("v.searchLookup","");
}
else{
document.getElementById("emailVerify").classList.remove("slds-hide");
}
}
},
changeType:function(component, event, helper){
var thisObjId = document.getElementById('recipientTypeSelectBox').value;
var addItemBtn = component.find('addItemBtn');
if(thisObjId == 'Email'){
$A.util.removeClass(addItemBtn,'slds-hide');
}else{
$A.util.addClass(addItemBtn,'slds-hide');
}
},
})
Step 4:- Create Lightning Component Style: lightningPillCmp.css
From Developer Console >> File >> New >> Lightning Component >> Component Style CSS
lightningPillCmp.css [Style CSS]
|
.THIS {
}
.THIS .slds-form-element__control .uiInputDefaultError{border:2px #c23934 solid; border-radius: 3px; position:absolute; padding: 0 0 5px 5px; top: 44px;
width: 185px;}
.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;
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]
|
public class pillController {
@AuraEnabled
public static List<User> searchUsersName(String KEY){
KEY = '%' + KEY + '%';
List<User> getUserList = [SELECT Id, Name, Email FROM USER WHERE Name LIKE:KEY AND isActive=TRUE];
RETURN getUserList;
}
}
Further post that would you like to learn in Salesforce
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
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 |