Skip to content

Instantly share code, notes, and snippets.

@edwinvijay
Last active June 8, 2019 20:15
<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="LightningDataTableController">
<!-- attributes -->
<aura:attribute name="data" type="Map"/>
<aura:attribute name="filteredData" type="Map"/>
<aura:attribute name="columns" type="List"/>
<!-- handlers-->
<aura:handler name="init" value="{!this }" action="{!c.init }"/>
<span>
<lightning:input onchange="{!c.searchTable}" type="search" label="Searh" variant="label-hidden" placeholder="Enter search term" aura:id="SearchBox"/>
</span>
<br/>
<lightning:datatable
columns="{!v.columns}"
data="{!v.filteredData}"
keyField="id"
/>
</aura:component>
public class LightningDataTableController {
@AuraEnabled
public static List<sObject> fetchData() {
//Query and return list of Contacts
List<SObject> objRecords = [SELECT FirstName, LastName,Email from Contact LIMIT 10];
return objRecords;
}
}
({
init: function (cmp, event, helper) {
cmp.set('v.columns', [
{ label: 'First Name', fieldName: 'FirstName', type: 'text' },
{ label: 'Last Name', fieldName: 'LastName', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'text' }
]);
var action = cmp.get("c.fetchData");
action.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS") {
cmp.set("v.data", response.getReturnValue());
cmp.set("v.filteredData", response.getReturnValue());
}
});
$A.enqueueAction(action);
},
searchTable : function(cmp,event,helper) {
var allRecords = cmp.get("v.data");
var searchFilter = event.getSource().get("v.value").toUpperCase();
var tempArray = [];
var i;
for(i=0; i < allRecords.length; i++){
if((allRecords[i].FirstName && allRecords[i].FirstName.toUpperCase().indexOf(searchFilter) != -1) ||
(allRecords[i].LastName && allRecords[i].LastName.toUpperCase().indexOf(searchFilter) != -1 ) ||
(allRecords[i].Email && allRecords[i].Email.toUpperCase().indexOf(searchFilter) != -1 ) )
{
tempArray.push(allRecords[i]);
}
}
cmp.set("v.filteredData",tempArray);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment