Skip to content

Instantly share code, notes, and snippets.

@jeremyvial
Created December 9, 2020 21:45
Show Gist options
  • Save jeremyvial/8c0b0cc4566c3452defdfbcf670dfc02 to your computer and use it in GitHub Desktop.
Save jeremyvial/8c0b0cc4566c3452defdfbcf670dfc02 to your computer and use it in GitHub Desktop.
<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>
// 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);
}
}
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