This blog post delves into the development journey of a Lightning Web Component (LWC) intricately linked with the Salesforce Apex controller. This component is engineered to efficiently retrieve data and present it within a Lightning Datatable, augmented by a sophisticated search bar functionality, empowering users to dynamically filter and pinpoint specific records.
CODE EXPLANATION:
Within the Apex Controller (SearchControllerInDatatable), the fetch data method is prominently annotated with @AuraEnabled. This method meticulously fetches a curated collection of Account records, encompassing vital attributes such as ID, Name, and a bespoke Email__c field. Noteworthy is the incorporation of the public sharing keyword, exemplifying the Apex class’s steadfast adherence to Salesforce’s stringent sharing regulations.
SearchControllerInDatatable.cls
public with sharing class SearchControllerInDatatable{
@AuraEnabled(cacheable=true)
public static List<Account> getData() {
return [SELECT Id, Name, Email__c FROM Account];
}
}
- Imports LightningElement and wire from ‘lwc’.
- Utilizes the @wire decorator to import the fetchData Apex method.
- Defines a set of columns for the Lightning Datatable, specifying labels, field names, and types.
- Implements a search bar using the <lightning-input> component and binds it to the searchTerm property.
- Utilizes a debounce mechanism to optimize performance by delaying the execution of the filtering logic.
- Implements the filterData method to dynamically filter records based on the search term.
Metadata Configuration (XML File):
- The Lightning component bundle is configured for exposure and targeted for various Salesforce page types, including Record Page, App Page, Home Page, and Tab.
SearchBarInDatatable.html
<template>
<div>
<lightning-input type="search" label="Search" value={searchTerm} onchange={handleSearchChange}>
</lightning-input>
<lightning-datatable key-field="id" data={filteredData} columns={columns} hide-checkbox-column="true">
</lightning-datatable>
</div>
</template>
SearchBarInDatatable.js
import { LightningElement, wire } from 'lwc';
import getData from '@salesforce/apex/SearchControllerInDatatable.getData';
const columns = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Email', fieldName: 'Email__c', type: 'Email' }
];
export default class SearchBarInDatatable extends LightningElement {
searchTerm = '';
data = [];
filteredData = [];
delayTimeout;
@wire(getData)
wiredData({ error, data }) {
if (data) {
this.data = data;
this.filterData();
} else if (error) {
console.error('Error loading data: ', error);
}
}
get columns() {
return columns;
}
handleSearchChange(event) {
this.searchTerm = event.target.value;
this.debounceFilter();
}
debounceFilter() {
clearTimeout(this.delayTimeout);
this.delayTimeout = setTimeout(() => {
this.filterData();
}, 300);
}
filterData() {
this.filteredData = this.data.filter(record =>
Object.values(record).some(value =>
value.toLowerCase().includes(this.searchTerm.toLowerCase())
)
);
}
}
Outcome: Data Table with Search Functionality:
Conclusion: Developing a Lightning Web Component with search capabilities elevates user interaction by facilitating seamless navigation and information retrieval within the Salesforce platform. Leveraging the synergy between Apex and Lightning Web Components empowers developers to craft sophisticated, responsive, and streamlined interfaces that align precisely with organizational requirements. Continuously refine and expand upon the provided code to accommodate evolving needs and optimize user engagement within your Salesforce ecosystem.