You need to sign in to do that
Don't have an account?
imKT
Lightning component is not working...
Hi everyone,
I am trying to search Account records using Lightning Component but functionality is not working. Please help me to find out where I did wrong.
Component:
Search Result comp:
Js controller:
I am trying to search Account records using Lightning Component but functionality is not working. Please help me to find out where I did wrong.
Component:
<aura:component controller="AccountListController" implements="force:appHostable"> <aura:attribute name="acList" type="Account[]" /> <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> <aura:handler event="c:SearchKeyChange" action="{!c.searchKeyChange}"/> <div class="slds-grid"> <div class="slds-col slds-align-bottom" style="margin-top:10px"> <c:SearchResultComponent /> </div> </div> <table class="slds-table slds-table_bordered slds-table_striped slds-table_cell-buffer slds-table_fixed-layout"> <thead> <tr class="slds-text-heading_label"> <th scope="col"><div class="slds-truncate" title="Id">Id</div></th> <th scope="col"><div class="slds-truncate" title="Name">Name</div></th> <th scope="col"><div class="slds-truncate" title="Type">Type</div></th> <th scope="col"><div class="slds-truncate" title="AccountNumber">Account Number</div></th> <th scope="col"><div class="slds-truncate" title="Industry">Industry</div></th> </tr> </thead> <tbody> <!-- Use the Apex model and controller to fetch server side data --> <aura:iteration items="{!v.acList}" var="ac"> <tr> <th scope="row"> <div class="slds-truncate" title="{!ac.Id}"> <a href="{! '#/sObject/' + ac.Id + '/view'}">{!ac.Id}</a> </div> </th> <td><div class="slds-truncate" title="{!ac.Name}">{!ac.Name}</div></td> <td><div class="slds-truncate" title="{!ac.Type}">{!ac.Type}</div></td> <td><div class="slds-truncate" title="{!ac.AccountNumber}">{!ac.AccountNumber}</div></td> <td><div class="slds-truncate" title="{!ac.Industry}">{!ac.Industry}</div></td> </tr> </aura:iteration> </tbody> </table> </aura:component>Js Controller:
({ doInit : function(component, event, helper) { helper.getAccountList(component); }, searchKeyChange : function(component, event) { var searchKey = event.getParam("searchKey"); //console.log('@@@@@@@search Value====',searchValue); var accList = component.get("v.acList"); var action = component.get('c.findByName'); //console.log('action Value====',action); action.setParam({ "searchKey" : searchKey, "accList" : accList }); action.setCallback(this, function(result){ component.set('v.acList', result.getReturnValue()); }); $A.enqueueAction(action); }, })Helper Js:
({ getAccountList : function(component) { var action = component.get('c.getAccounts'); action.setCallback(this,function(result){ component.set('v.acList', result.getReturnValue()); }); $A.enqueueAction(action); }, })Server side Controller:
public class AccountListController { @AuraEnabled public static list<Account> getAccounts(){ return[select id, name , Type, Industry, AccountNumber from Account ORDER BY createdDate ASC]; } @AuraEnabled public static list<Account> findByName(String searchKey, Account[] accList){ Set<Id> recordIds = new Map<Id, Account>(getAccounts()).keySet(); System.debug('recordIds@@@@@@'+' '+recordIds); System.debug('ACCOUNT LIST' + accList); String searchName = searchKey + '%'; return[ SELECT id, Name, Industry, Type, AccountNumber FROM Account WHERE (Name LIKE : searchName OR AccountNumber LIKE :searchName OR Type LIKE :searchName OR Industry LIKE :searchName) AND Id IN:recordIds]; } }
Search Result comp:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" > <div> <input type="text" class="form-control" placeholder="Search Account.." onkeyup="{!c.searchKeyChange}"/> </div> </aura:component>
Js controller:
({ searchKeyChange : function(component, event, helper) { var appevt = $A.get("e.c:SearchKeyChange"); appevt.setParam({ "searchKey" : event.target.value }); appevt.fire(); }, })
Greetings to you!
I read your problem and implemented in my Org. Please use the below code [Solved] : -
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
I don't need button for searching records in this scenario. If I have more than 3 records which starts from A. like ABC, Amar, Amir .
Suppose I put Am in searchbox then 2 records should be shown or If I type Ama then only Amar should be shown.
I hope you understand that.
Greetings to you!
- You have not used aura:registerEvent in SearchResultComponent.
- Please use the below line .
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.