Create an LWC Data Table With Nested Iterators in Salesforce

In Salesforce, using LWC components, we create a data table with Nested Iterators to display hierarchical or grouped data within a data table.

The Lightning data table utilises nested iterators to render rows and columns, and fetches data via Apex. After deploying it on the record page, it displays related records with customizable fields and labels.

In this Salesforce tutorial, we will learn how to create an LWC data table with nested iterators in Salesforce.

Use of Nested Iterators in Salesforce Lightning Data table?

In Salesforce, by using the nested iterators, we can create a reusable Lightning Web Component (LWC) that displays a dynamic table of records on a Salesforce object record page.

Nested iterators in an LWC data table use ‘for:each’ directive to build a dynamic table. The outer iterator ‘for:each loops‘ over a list of records, generating table rows (<tr> elements) for each entry.

The inner iterator ‘for:each’ iterates over field names or columns, creating cells (<td> elements) for each field in the current record.

This structure allows the table to display data according to the number of rows and columns based on data and configuration. It automatically updates the records using a unique key, such as a record ID.

Create a Reusable LWC Data Table with Nested Iterators in Salesforce

In the example below, we will create a reusable data table that will display related records on the current record page. In this data table, the user will be able to specify the object, filter field, columns, and labels via Lightning App Builder.

For this Lightning data table, we will create a Parent Component ( customTable) to handle data fetching and table structure. Child Component (customTableColumn) to render individual cell values and a controller class to dynamically fetch the related records.

Now, follow the steps below to create a reusable LWC data table with nested iterators.

1. First, we will create an Apex controller class to fetch the current object’s related records dynamically.

public with sharing class CustomTableController {
    
    @AuraEnabled(cacheable = true)
    public static List<sObject> fetchRecords(String strRecordId, String strField, String strObjectName, String strColumns) {

        String strSOQL = 'SELECT Id, ' + String.escapeSingleQuotes(strColumns) + 
                         ' FROM ' + String.escapeSingleQuotes(strObjectName) + 
                         ' WHERE ' + String.escapeSingleQuotes(strField) + 
                         ' = :strRecordId';

        System.debug('SOQL is: ' + strSOQL);

        return Database.query(strSOQL);
    }
}

2. Create a child LWC component, customTableColumn; this component will render individual cell values in the table.

  • Enter the code below in the HTML file for the child component.
<template>
    {strValue}
</template>
  • To define the rendering logic of related records, enter the code below in the JS file of the child component.
import { LightningElement, api } from 'lwc';

export default class CustomTableColumn extends LightningElement {

    @api column;
    @api record;
    strValue

    connectedCallback() {

        this.strValue = this.record[ this.column ];
    }
}

We will call this child component in the parent one, so there is no need to expose it to the Lightning record page and make changes to the meta.xml file.

3. Create the parent component “customTable” and enter the code below in the HTML file.

To render the data dynamically in the data table, we have used the ‘for:each’ directive is used to iterate over an array and render a template for each item.

Nested iterators refer to placing one ‘for:each’ loop inside another, allowing us to iterate over a collection of rows and, for each item, iterate over another column.

The output will be in the grid form suitable for displaying