Open rodriggj opened 2 years ago
We first need to create a wrapper
or container
Class to enable the current Filter
selection properties coming from the carFilter
class to be available for the CarController.cls
class. Because the filter
options are of different data types, we will create a wrapper
or container
class to pull in the filter properties
. This wrapper Class
in out CarController.cls
file.
CarController.cls
file and enter the following code. This will create a Filter
Object which we will later pass to the getCars()
method. public with sharing class CarController {
//wrapper class
public class Filters {
@AuraEnabled
public String searchKey {get;set;}
@AuraEnabled
public Decimal maxPrice {get;set;}
@AuraEnabled
public String[] categories {get;set;}
@AuraEnabled
public String[] makeType {get;set;}
}
@AuraEnabled(Cacheable=true)
public static List<Car__c> getCars(Filters filters){
}
}
Filter
Object in the getCars()
method which creates a SOQL Query, we can now use the attributes in the Filter
Object and dynamically utilize the Filters
that were selected in the UI to modify the response of the SOQL query. public with sharing class CarController {
//wrapper class
public class Filters {
@AuraEnabled
public String searchKey {get;set;}
@AuraEnabled
public Decimal maxPrice {get;set;}
@AuraEnabled
public String[] categories {get;set;}
@AuraEnabled
public String[] makeType {get;set;}
}
@AuraEnabled(Cacheable=true)
public static List<Car__c> getCars(Filters filters){
String key, whereClause ='';
Decimal maxPrice;
String[] categories, makeType, criteria = new List<String>{};
return [SELECT Id, Name, MSRP__c, Description__c, Category__c, Picture_URL__c FROM Car__c];
}
}
filter
Object we intended to pass are captured. With all the properties accounted for, we will assemble a String that executes the SOQL
query and return the response from the SOQL query. public with sharing class CarController {
//wrapper class
public class Filters {
@AuraEnabled
public String searchKey {get;set;}
@AuraEnabled
public Decimal maxPrice {get;set;}
@AuraEnabled
public String[] categories {get;set;}
@AuraEnabled
public String[] makeType {get;set;}
}
@AuraEnabled(Cacheable=true)
public static List<Car__c> getCars(Filters filters){
String key, whereClause ='';
Decimal maxPrice;
String[] categories, makeType, criteria = new List<String>{};
if(filters !=null){
maxPrice = filters.maxPrice;
categories = filters.categories;
makeType = filters.makeType;
if(!String.isEmpty(filters.searchKey)){
key = '%'+filters.searchKey + '%';
criteria.add('Name LIKE: key');
}
if(filters.maxPrice >= 0){
criteria.add('MSRP__c <= :maxPrice');
}
if(filters.categories != null){
criteria.add('Category__c IN :categories');
}
if(filters.makeType != null){
criteria.add('Make__c IN :makeType');
}
if(criteria.size()>0){
whereClause = 'WHERE ' + String.join(criteria, ' AND ');
}
}
String query = 'SELECT Id, Name, MSRP__c, Description__c,Category__c, Picture_URL__c FROM Car__c' + whereClause + ' WITH SECURITY_ENFORCED' + ' ORDER BY Name';
return Database.query(query);
}
}
cars
array returned from the SOQL query when the getCars(Filter filters)
method executes. We can now pass those results back to our carTileList.js
file just like we did previously. We already prepared the @wire
adaptor, so now we just need to pass the results of the getCars(Filter filters)
back to the adaptor by referencing the filters
object.carTileList.js file
import { LightningElement, wire } from 'lwc';
import getCars from '@salesforce/apex/CarController.getCars'
export default class CarTileList extends LightningElement {
cars
error
filters = {}
@wire(getCars, {filters: '$filters'})
carsHandler({data, error}){
if(data){
console.log(data)
this.cars = data
}
if(error){
console.error(error)
this.error = error
}
}
}
jdfj;afjlk