I've created three Custom Objects Dictionary_Header__c, Dictionary_Section__c, and Dictionary_Term__c. Terms belong to a corresponding section, and each section belongs to a corresponding header (an example to illustrate: the structure of the dictionary is that Fruit would be a header, Citrus Fruit would be a section, and Lemon, Grapefruit, and Orange would all be terms underneath that section).
Currently I am using Visual Studio Code to create the LWC. My JavaScript file looks like this:
import { LightningElement, wire } from 'lwc';
import getHeaderData from '@salesforce/apex/RetrieveDictionaryData.getHeaderData';
import getSectionData from '@salesforce/apex/RetrieveDictionaryData.getSectionData';
import getTermsData from '@salesforce/apex/RetrieveDictionaryData.getTermsData';
export default class Dictionary extends LightningElement {
@wire(getHeaderData) header;
@wire(getSectionData) section;
@wire(getTermsData) term;
}
My HTML file looks like this:
<template>
<div id="top" class="slds-grid slds-wrap" style="padding: 10px;">
<template if:true={header.data}>
<div class="site-sticky-navbar site-sticky-navbar_docblock slds-col slds-large-size--2-of-12"
style="padding: 10px;">
<nav class="slds-nav-vertical" aria-label="Sub page">
<div class="slds-form-element slds-p-horizontal_large">
<label class="slds-form-element__label slds-assistive-text" for="input-id-01">Filter
navigation
items</label>
<div class="slds-form-element__control slds-input-has-icon slds-input-has-icon_left">
<span class="slds-icon_container slds-icon-utility-search">
<svg class="slds-icon slds-input__icon slds-input__icon_right slds-icon-text-default"
aria-hidden="true">
<use xlink:href="/assets/icons/utility-sprite/svg/symbols.svg#.search"></use>
</svg>
</span>
<input type="search" id="input-id-01" placeholder="Type to search" class="slds-input" />
</div>
</div>
<div class="slds-nav-vertical__section">
<template for:each={header.data} for:item="h">
[ This is where I want the nested looping of headers, section titles, and dictionary terms ]
</nav>
</div>
</template>
</div>
</template>
My Apex class looks like this:
public with sharing class RetrieveDictionaryData {
@AuraEnabled(cacheable=true)
public static List<Dictionary_Header__c> getHeaderData(){
return [SELECT Id, Name FROM Dictionary_Header__c];
}
@AuraEnabled(cacheable=true)
public static List<Dictionary_Section__c> getSectionData(){
return [SELECT Id, Name, Related_Dictionary_Header__c FROM Dictionary_Section__c];
}
@AuraEnabled(cacheable=true)
public static List<Dictionary_Term__c> getTermsData(){
return [SELECT Id, Name, Related_Dictionary_Section__c, Definition__c FROM Dictionary_Term__c];
}
}
And my Visualforce page looks like this:
<apex:page showHeader="false">
<apex:includeLightning />
<div id="LightningComponentid" />
<script>
$Lightning.use("c:MainDictionary", function() {
$Lightning.createComponent("c:dictionary",
{
},
"LightningComponentid",
function(cmp) {
console.log('LWC Componenet added in VF page');
});
});
</script>
</apex:page>
I'm able to succesfully bring the Custom Object records into Visual Studio Code, and I can display the headers so far. But now I want to correctly display all of the header, section, and term records. What I am confused about is how to use <template> in such a way that the correct section titles display under each header, and the correct terms display under each section title. Right now, the template for:each loops through and displays the headers okay, but I don't know how to go from there without having duplicates or having the wrong sections appear under the wrong headers.
For reference, I have it working just using Visualforce and Apex.
Visualforce Page:
<apex:page Controller="DictionaryController">
<!-- Navigation sidebar-->
<apex:repeat value="{!headerSectionInfo}" var="h">
{!headerSectionInfo[h].Name}
<apex:repeat value="{!headerSectionInfo[h].Dictionary_Sections__r}" var="s">
{!s.Name}
<apex:repeat value="{!sectionTermInfo[s.Id].Dictionary_Terms__r}" var="t">
{!t.Name}
</apex:repeat>
</apex:repeat>
</apex:repeat>
<!-- Dictionary terms display-->
</apex:page>
Apex class:
public with sharing class DictionaryController {
public Map<Id, Dictionary_Header__c> headerSectionInfo;
public Map<Id, Dictionary_Section__c> sectionTermInfo;
// Collect data from header, section, and term objects
public static Map<Id, Dictionary_Header__c> getHeaderSectionInfo(){
Map<Id, Dictionary_Header__c> headerSectionData = new Map<Id, Dictionary_Header__c>([SELECT Id, Name, (SELECT Id, Name from Dictionary_Sections__r) FROM Dictionary_Header__c]);
return headerSectionData;
}
public static Map<Id, Dictionary_Section__c> getSectionTermInfo(){
Map<Id, Dictionary_Section__c> sectionTermData = new Map<Id, Dictionary_Section__c>([SELECT Id, Name, (SELECT Id, Name, Definition__c from Dictionary_Terms__r) FROM Dictionary_Section__c]);
return sectionTermData;
}
}
Any ideas of how to accomplish this using Lightning Web Component would much appreciated! :)
Hi Victoria,
Given the technical nature of this question, if you don't get an answer I would try posting this question on the Developer Forums. They would be better suited at providing guidance on this type of question at https://developer.salesforce.com/forums#!/feedtype=RECENT&criteria=ALLQUESTIONS&
Thanks,
Mikey