Created
April 1, 2020 16:32
-
-
Save salesforce-casts/0b1c81f820564c838913dca2abc87abd to your computer and use it in GitHub Desktop.
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
public with sharing class ExploreCustomContactController { | |
public ExploreCustomContactController() { | |
} | |
@AuraEnabled | |
public static Map<String, String> getFieldTypes(List<String> selectedFields){ | |
String objectName = 'Contact'; | |
Map<String, String> fieldTypeMap = new Map<String, String>(); | |
for(String field : selectedFields){ | |
Schema.DisplayType fieldType = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(field).getDescribe().getType(); | |
fieldTypeMap.put(field, String.valueOf(fieldType)); | |
} | |
return fieldTypeMap; | |
} | |
@AuraEnabled(cacheable=true) | |
public static List<Contact> getContacts(List<String> selectedFields){ | |
String s = ''; | |
Integer i = 0; | |
for(String field : selectedFields){ | |
if(i == 0){ | |
s+= field; | |
}else{ | |
s+= ','+field; | |
} | |
i++; | |
} | |
String query = 'SELECT ' + s + ' FROM Contact'; | |
return Database.query(query); | |
} | |
@AuraEnabled(cacheable=true) | |
public static List<String> getContactFields(){ | |
SObjectType contactType = Schema.getGlobalDescribe().get('Contact'); | |
Map<String,Schema.SObjectField> mfields = contactType.getDescribe().fields.getMap(); | |
List<String> sObjectDetailsList = new List<String>(); | |
for(String s : mfields.keySet()){ | |
sObjectDetailsList.add(String.valueOf(mfields.get(s))); | |
} | |
return sObjectDetailsList; | |
} | |
} |
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-datatable | |
key-field="id" | |
data={fetchedContacts} | |
columns={columns} | |
> | |
</lightning-datatable> | |
<label>Select the fields you want to display in the Data Table</label> | |
<select class="slds-select" name="optionSelect" multiple> | |
<template for:each={wiredContactFields.data} for:item="field"> | |
<option key={field} value={field}>{field}</option> | |
</template> | |
</select> | |
<lightning-button | |
label="Looks Fine" | |
onclick={handleSelectedFields} | |
></lightning-button> | |
</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
import { LightningElement, track, wire } from 'lwc'; | |
import getContacts from '@salesforce/apex/ExploreCustomContactController.getContacts'; | |
import getContactFields from '@salesforce/apex/ExploreCustomContactController.getContactFields'; | |
import getFieldTypes from '@salesforce/apex/ExploreCustomContactController.getFieldTypes'; | |
export default class ExploreReimplementDataTable extends LightningElement { | |
@track columns = [ | |
{ label: 'Id', fieldName: 'Id' }, | |
{ label: 'Name', fieldName: 'Name' } | |
]; | |
@track selected = ['Id', 'Name']; | |
@track fetchedContacts = []; | |
@wire(getContactFields) wiredContactFields; | |
@wire(getContacts, { selectedFields: '$selected' }) | |
wiredContacts({ error, data }) { | |
if (data) { | |
this.fetchedContacts = data; | |
} else if (error) { | |
console.log(error); | |
} | |
} | |
handleSelectedFields(event) { | |
//clean the array | |
this.selected.splice(0, this.selected.length); | |
this.fetchedContacts = []; | |
this.columns = []; | |
for (var option of this.template.querySelector('select').options) { | |
if (option.selected) { | |
this.selected.push(option.value); | |
} | |
} | |
getContacts({ selectedFields: this.selected }) | |
.then(result => { | |
this.fetchedContacts = result; | |
this.handleFetchFieldTypes(); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); | |
} | |
handleFetchFieldTypes() { | |
getFieldTypes({ selectedFields: this.selected }) | |
.then(result => { | |
this.columns = this.selected.map(field => { | |
const dType = result[field]; | |
if (dType === 'STRING' || dType === 'ID') { | |
return { label: field, fieldName: field }; | |
} else if (dType === 'DATE') { | |
return { label: field, fieldName: field, type: 'date' }; | |
} else if (dType === 'DATETIME') { | |
return { | |
label: field, | |
fieldName: field, | |
type: 'datetime' | |
}; | |
} else if (dType === 'Integer') { | |
return { | |
label: field, | |
fieldName: field, | |
type: 'Integer' | |
}; | |
} else if (dType === 'BOOLEAN') { | |
return { | |
label: field, | |
fieldName: field, | |
type: 'text' | |
}; | |
} else { | |
return { label: field, fieldName: field }; | |
} | |
}); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment