1

I was looking to do the following and I'm not sure if it's possible in LWC.

I have a list of records which I'd like to loop through in the UI. I also have a map of records where the key is the iD of the records in the first list. I'd iike to loop through a list of records in my inner loop where the inner list has come from using the map with the key as the ID of the outer record being looped.

The desired code being something like

    <template for:each={transactions} for:item="transaction">
      <div class="rows" key={transaction.Id}>
          {transaction.Name}
      </div>
      <template for:each={transactionIdToDetailsMap[{transaction.Id}]} for:item="detail">
        <div class="rows" key={detail.Id}>
           {detail.Name}
        </div>
      </template>
   </template>

It seems this is not possible using template's. Does anyone have any tips on the best way I can achieve this? Thanks in advance for any tips on best approaches.

2 Answers 2

3

This is not possible in Aura or LWC, actually. for:each must have an Array to work off of. That means you must structure your data such that:

<template for:each={transactions} for:item="transaction">
  <div class="rows" key={transaction.Id}>
      {transaction.Name}
  </div>
  <template for:each={transaction.details} for:item="detail">
    <div class="rows" key={detail.Id}>
       {detail.Name}
    </div>
  </template>

This is a trivial transformation in JavaScript, though:

this.transactions = this.transactions.map(
  (transaction) => ({
    ...transaction,
    details: this.transactionIdToDetailsMap[transaction.Id]
  })
);
3
  • Hi @sfdcfox Thanks. That's a great help. I do receive an "aura_prod.js:12 Uncaught (in promise) TypeError: e[jr] is not a function" error when including the following ...<template for:each={transaction.details} for:item="detail"> <div class="rows" key={detail.Id}> {detail.Name} </div> </template> ... I was thinking it may be because not all transactions will have an entry in the ransactionIdToDetailsMap. Would this seem a possible cause? If so can an exception be caught in setting the transaction.details if none exist? Thanks again.
    – RedQueries
    Aug 6, 2021 at 14:17
  • @RedQueries If there are missing entries, that could cause the problem. Try changing your template to <template for:each={transaction.details} for:item="detail" if:true={transaction.details}>
    – sfdcfox
    Aug 6, 2021 at 14:32
  • HI @sfdcfox. Apologies, I've noted it was an error on my side. I was setting the detail values in my transactionIdToDetailsMap to a single value instead of an array. On making sure it's an key-to-array combination it's now working correctly. Thanks for your help. You've been very helpful and super quick in your replies. Much appreciated.
    – RedQueries
    Aug 6, 2021 at 15:00
0

Might be able to get away with using a button to set a var with its key and an array to the values of the map entry and using that for the template iterable

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 19, 2022 at 4:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .