Toggle Side Panel
Forcetalks
  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
    • Salesforce Industry Expertise
    • Salesforce Experts by Location
  • Webinars
  • Contact Us
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
      • Salesforce Industry Expertise
      • Salesforce Experts by Location
    • Webinars
    • Contact Us
    Close search

    Implementation of Dynamic Search in Salesforce Lightning Component

    avnishyadav2561995gmail-com
    Avnish Yadav September 10, 2018
    Implementation of Dynamic Search in Lightning Component

    Today, I am sharing the code, that, how you can implement Dynamic Search using Lightning Component. For this task, we need three things:-

    1. A Lightning Component Bundle
    2. A Lightning Application Bundle
    3. An Apex Controller.

    Apex Controller –

    public class AccountShowController {
        @AuraEnabled
        public static List showAccounts(){
            return [SELECT Id, Name, Phone, AnnualRevenue From Account];
        }
    }

    Lightning Component –

    <aura:component controller="AccountShowController">
        <aura:attribute name="Account" type="Account[]" />
        <aura:attribute name="SearchKey" type="String" />
        <aura:handler name="init" value="{!this}" action="{!c.myAction}"/>
        <div class="slds-form-element">
            <label class="slds-form-element__label" for="text-input-id-1">Input Label</label>
            <div class="slds-form-element__control">
                <input type="text" value="{!v.SearchKey}" onkeyup="{!c.myFunction}" id="myInput" class="slds-input" placeholder="Search Accounts" />
            </div>
        </div>
        <br/>
        <table class="slds-table slds-table--bordered" id="myTable">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Phone</th>
                    <th>AnnualRevenue</th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration items="{!v.Account}" var="a">
                    <tr>
                        <td>{!a.Name}</td>
                        <td>{!a.Phone}</td>
                        <td>{!a.AnnualRevenue}</td>
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
    </aura:component>

    JavaScript Controller –

    ({
    	myAction : function(component, event, helper) {
    		var action = component.get("c.showAccounts");
    		action.setCallback(this, function(response) {
    			var state=response.getState();
    			if(state === 'SUCCESS') {
    				component.set("v.Account", response.getReturnValue());
    			} else {
    				alert("error");
    			}
    		});
    		$A.enqueueAction(action);
    	},
    	myFunction:function(component, event, helper) {
    		var input, filter, table, tr, td, i;
    		input = document.getElementById("myInput");
    		//input = component.get("v.SearchKey");
    		//var input = component.find("myInput").get("v.value");
    		console.log(input.value);
    		filter = input.value.toUpperCase();
    		table = document.getElementById("myTable");
    		tr = table.getElementsByTagName("tr");
    		//console.log(tr.length)
    		for (i = 0; i < tr.length; i++) {
    			td = tr[i].getElementsByTagName("td")[0];
    			if (td) {
    				if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
    					tr[i].style.display = "";
    				} else {
    					tr[i].style.display = "none";
    				}
    			}
    		}
    	}
    })

    Lightning Application –

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

    Thanks.

    Categories: Lightning
    Tags: Dynamic Search, Lightning Component Bundle, Metadata, Salesforce Account, Salesforce Code, Salesforce Javascript Controller, Salesforce Lightning, Salesforce Lightning Application, Salesforce Lightning Application Bundle, Salesforce Lightning Component

    Get listed your company

    Have an innovative Salesforce solution that delivers faster, smarter results?
    Join the Marketplace

    [adinserter block=”16″]

    best salesforce consultants
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    Forcetalks

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.