• imKT
  • NEWBIE
  • 0 Points
  • Member since 2019
  • Birlasoft


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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:
<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();
    },
    
})


 
  • July 02, 2019
  • Like
  • 0
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:
<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();
    },
    
})


 
  • July 02, 2019
  • Like
  • 0