Created
December 9, 2020 21:45
-
-
Save jeremyvial/8c0b0cc4566c3452defdfbcf670dfc02 to your computer and use it in GitHub Desktop.
LWC Challenge component 3 (inspired from https://github.com/trailheadapps/lwc-recipes/tree/master/force-app/main/default/lwc/apexWireMethodWithParams)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<lightning-card | |
title="Christmas gift ideas for children" | |
icon-name="custom:custom57" | |
> | |
<div class="slds-var-m-around_medium"> | |
<lightning-input | |
type="search" | |
onchange={handleKeyChange} | |
class="slds-show slds-is-relative slds-var-m-bottom_small" | |
label="Search" | |
></lightning-input> | |
<template if:true={gifts}> | |
<template for:each={gifts} for:item="gift"> | |
<li key={gift.Id}> | |
{gift.Name} | |
</li> | |
</template> | |
</template> | |
</div> | |
</lightning-card> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inspired from LWC recipes | |
// https://github.com/trailheadapps/lwc-recipes/tree/master/force-app/main/default/lwc/apexWireMethodWithParams | |
import { LightningElement, wire, api } from 'lwc'; | |
import findGifts from '@salesforce/apex/GiftController.findGifts'; | |
/** The delay used when debouncing event handlers before invoking Apex. */ | |
const DELAY = 350; | |
export default class Component3 extends LightningElement { | |
gifts; | |
error; | |
handleKeyChange(event) { | |
// Debouncing this method: Do not actually invoke the Apex call as long as this function is | |
// being called within a delay of DELAY. This is to avoid a very large number of Apex method calls. | |
window.clearTimeout(this.delayTimeout); | |
const searchKey = event.target.value; | |
// eslint-disable-next-line @lwc/lwc/no-async-operation | |
this.delayTimeout = setTimeout(() => { | |
findGifts({ searchKey }) | |
.then((result) => { | |
this.gifts = result; | |
this.error = undefined; | |
}) | |
.catch((error) => { | |
this.error = error; | |
this.gifts = undefined; | |
}); | |
}, DELAY); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ublic with sharing class GiftController { | |
@AuraEnabled(cacheable=true) | |
public static List<Cadeau__c> findGifts(String searchKey) { | |
String str = 'SELECT Id,Name FROM Cadeau__c where ((publicCible__c = \'Enfant\') and Name like \'%' + searchKey + '%\')' ; | |
return Database.query(str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment