Saturday, June 13, 2020

Lightning component to search contact (With Name and Email)

Apex Class :


public with sharing class SearchContact {
    
    @auraEnabled
    
    public static List<contact> getContact (string name1,string email1)
 {
  
        return [select id,Name,Email,Department from Contact where(Name =: name1 and                             Email =: email1)];

    }
    
}


Component :


<aura:component controller="SearchContact">
    
    <aura:attribute name="SearchedContact" type="Contact[]" />
    
    <aura:attribute name="name1" type="string" />
    <aura:attribute name="email1" type="string" />
 
    <lightning:input  type="string" name="input1" label="name" value="{!v.name1}" />
    <lightning:input type="string"  name="input2" label="email" value="{!v.email1}"/>
    <lightning:button label="Search" onclick="{!c.Search}" />
    
    
    <aura:iteration items="{!v.SearchedContact}" var="SearchedContactIt">
        <table>
            
            <tr>
            <th>name</th><th>Email</th><th>Department</th>
            </tr>

            <tr>
                <td>{!SearchedContactIt.Name}</td><td>{!SearchedContactIt.Email}</td>
            </tr>  
            
        </table>
        
    </aura:iteration>
    
</aura:component>


Controller :


({
    Search : function(component, event, helper) {
        
        var Action = component.get("c.getContact");
        
        var N1=component.get("v.name1");
        var E1=component.get("v.email1");
        newExpense.setParams({
            "name1":N1,
            "email1":E1
        });
        
        Action.setCallback(this, function(response) {
            component.set("v.SearchedContact", response.getReturnValue());
            
        });
       
        $A.enqueueAction(Action );
        
    },
 
});


Preview :

                  Here's your component looks like this.
                You just need to enter a name or email or both and hit the search button.
                All the contact with that name and email are shown as in img.


Thanks,
LoveSalesforceYes


No comments:

Post a Comment

Get selected radio button value in LWC

This post explains how to get selected radio button in LWC. Below is demo of output : To use the radio button in LWC you have to use the  li...