function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Victoria UkatuVictoria Ukatu 

How to use <template> to correctly iterate and display related Custom Objects in LWC (using Visual Studio Code)

I am relatively new to LWCs, so any help would be appreciated! :)
The project that I'm working on is a dictionary that pulls together terms and their definitions from my company's Salesforce org. Here's an example of a similar concept: https://docs.kumu.io/overview/collaboration.html  On the left, I have a sidebar table of contents which helps users navigate to the particular defintion they're looking for, which is displayed. 

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. 

Does anyone know how to accomplish this? Thank you for your help! :)
Victoria UkatuVictoria Ukatu
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;
    }
}
I want to be able to accomplish the same thing, but in Lightning Web Components as described above in my January 27th post. Again, any help or ideas would be great, since I am pretty stuck as of right now. Thank you!
Arti Dhamale 1Arti Dhamale 1
Did anyone got the answer?