How to display the contacts based on Account name


Apex class
==========
public class AccountSearchController
{
    @AuraEnabled
    public static List<Contact>  displayconRecords(String searchKeyWord)
    {
        String searchWord='%'+searchKeyWord+'%';
        List<Contact> returnconlist=new List<Contact>();
        for(Contact con:[select Id,Account.Name,Name,Email,Phone from Contact where Account.Name like:searchWord])
        {
            returnconlist.add(con);
         
        }
      return returnconlist;
     
    }
}

InputCarryEvent.evt
===================
<aura:event type="Component" description="Event template" >
    <aura:attribute name="accInfo" type="String"/>
</aura:event>

Childcomp.cmp
=============
<aura:component controller="AccountSearchController">
    <aura:attribute name="accName" type="String"/>
    <aura:registerEvent name="SampleComponentEvent" type="c:InputCarryEvent" />
    <lightning:input type="text" label="Enter the Name"  value="{!v.accName}" required="true" style="width:50%;"/>
    <lightning:button label="Show contacts"  class="slds-button slds-button_brand" onclick="{!c.showContacts}"/>

</aura:component>

Childcompcontroller
===================
({
showContacts : function(component, event, helper) {
        var inputEvt=component.getEvent("SampleComponentEvent");
        inputEvt.setParams({
            "accInfo":component.get("v.accName")
        });
        inputEvt.fire();
}
})

Parentcomp.cmp
==============
<aura:component controller="AccountSearchController" >
    <aura:attribute name="conlist" type="list"/>
    <aura:handler name="SampleComponentEvent" event="c:InputCarryEvent" action="{!c.reload}"/>
    <c:Childcomp/>
    <table  class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped">
        <tr>
         <td>Name</td>
            <td>Email</td>
          <td>Phone</td>
        </tr>
        <aura:iteration items="{!v.conlist}" var="Items">
            <tr>
             <td>
               {!Items.Name}
             </td>
              <td>
               {!Items.Email}
              </td>
              <td>
                {!Items.Phone}
              </td>
            </tr>
     
        </aura:iteration>
 
 
    </table>
 

 
</aura:component>

Controller
==========
({
 reload : function(component, event, helper)
 {
    var action=component.get("c.displayconRecords");
    action.setParams({
       'searchKeyWord':event.getParam("accInfo")
    });
    action.setCallback(this,function(response){
    var state = response.getState();
    if (component.isValid() && state === "SUCCESS")
    {
        component.set("v.conlist", response.getReturnValue());
    } 
    else
    {
       console.log('Error');       
    }

    });
    $A.enqueueAction(action);
 }
})

LightningApplication
====================
<aura:application  extends="force:slds">
    <c:Parentcomp/>
</aura:application>














Comments