Dynamic search in Lightning Webcomponent
Apex Class
==========
public with sharing class DynamicSearchController {
public DynamicSearchController() {
}
@AuraEnabled(cacheable=true)
public static List<EntityDefinition> displaySobjects(){
return [SELECT Id,QualifiedApiName FROM EntityDefinition order by QualifiedApiName];
}
@AuraEnabled
public static List<sObject> displayRecords(String selectedSobject){
System.debug('method called');
List<sObject> returnlist=new List<sObject>();
System.debug('@@@Value@@'+sObjectName);
String query='select Id,Name from '+selectedSobject;
returnlist=DataBase.query(query);
System.debug('@@Query'+returnlist);
return returnlist;
}
}
sobjectSearchFunctionality.html
===============================
<template>
<div class="slds-p-around_small">
<lightning-combobox name="Picklist" label="Select" value={value}
placeholder="Select"
options={sObjectTypes}
onchange={handleChange}>
</lightning-combobox>
</div>
<lightning-layout>
<div class="slds-grid slds-wrap" if:true={records}>
<template for:each={records} for:item="items">
<div class="slds-col slds-size_1-of-3 slds-p-around_small" key={items.Id} >
<lightning-layout-item key={items.Id} padding="around-small" size="12" small-device-size="6"
large-device-size="6" medium-device-size="6">
<lightning-card title={items.Name} icon-name="custom:custom9">
<p class="slds-p-horizontal-small">
{items.Name} <br/>
</p>
</lightning-card>
</lightning-layout-item>
</div>
</template>
</div>
</lightning-layout>
</template>
sobjectSearchFunctionality.js
=============================
import { LightningElement, wire,api ,track} from 'lwc';
import displaySobjects from '@salesforce/apex/DynamicSearchController.displaySobjects';
import displayRecords from '@salesforce/apex/DynamicSearchController.displayRecords';
export default class SobjectSearchFunctionality extends LightningElement {
@track sObjectTypes = [{label:"",value: "" }];
@track value;
@track records;
@wire(displaySobjects) displaySobjectsOnLoad({ error, data }) {
if (data) {
let tempArray = [];
for(let i=0;i<data.length;i++){
let tempObject={};
tempObject.label=data[i].QualifiedApiName;
tempObject.value=data[i].QualifiedApiName;
tempArray.push(tempObject);
}
this.sObjectTypes=tempArray;
}
else if(error){
this.error=error;
this.data=undefined;
console.log('unable to load the data due to'+JSON.stringify(error));
}
}
handleChange(event){
let currentVal=event.target.value;
displayRecords({
selectedSobject:currentVal
})
.then(result => {
this.records = result;
})
.catch(error => {
this.error = error;
console.log('Error in fetching the data'+JSON.stringify(this.error));
});
}
}
Comments
Post a Comment