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
Guru Vemuru 11Guru Vemuru 11 

Custom LWC datatable

Hi All,
I wanted to build custom LWC data table.Like in a table that shows list of accounts,now If I Click any account then it should show all related records for that account.
User-added image
Thanks in advance
CharuDuttCharuDutt
Hii Guru
Try Below Code
LWC
<template>
    <div class="slds-box slds-theme--default">
        <div class="slds-text-color_inverse slds-text-heading_large" style="padding:0.5rem;background:#16325c">        
            Accounts
        </div>
        <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped">
            <thead>
                <tr class="slds-line-height_reset">
                    <th>
                        Account Name
                    </th>
                    <th>
                        Account Number
                    </th>
                    <th>
                        Industry
                    </th>
                    <th>
                        Rating
                    </th>
                    <th>
                        Type
                    </th>
                </tr>
            </thead>
            <tbody>
                <template iterator:it={records}>
                    <tr class="slds-hint-parent" key={it.value.objAccount.Id} style="cursor: pointer;">                        
                        <td data-label="Account Name" data-record-id={it.index} onclick={hideAndShow}>       
                                            
                                {it.value.objAccount.Name}
                            
                        </td>
                        <td data-label="Account Number">
                                             
                                {it.value.objAccount.AccountNumber}
                            
                        </td>
                        <td data-label="Industry">
                                             
                                {it.value.objAccount.Industry}
                            
                        </td>
                        <td data-label="Rating">
                                             
                                {it.value.objAccount.Rating}
                            
                        </td>
                        <td data-label="Type">
                                        
                                {it.value.objAccount.Type}
                            
                        </td>    
                    </tr>
                    <template if:false={it.value.hideBool} key={it.value.objAccount.Id} style="padding: 5px;">
                        <tr key={it.value.objAccount.Id}>
                            <td colspan="5">
                                <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped">
                                    <thead>
                                        <tr class="slds-line-height_reset">
                                            <th>
                                                Contact Name
                                            </th>
                                            <th>
                                                Email
                                            </th>
                                        
                                        </tr>
                                    </thead>
                                    <tbody>
                                <template if:true={it.value.contactBool}>    
                                   
                                        <template iterator:it={it.value.objAccount.Contacts}>
                                            <tr style="padding:1px;" key={it.value.Id}>
                                               <td>{it.value.FirstName} {it.value.LastName}</td>
                                               
                                                <td>{it.value.Email}</td>
                                                
                                            </tr>
                                        </template>
                                </template>
                                </tbody>
                            </table>
                                <template if:false={it.value.contactBool}>    
                                    <b key={it.value.objAccount.Id}>No Contacts found for this Account!!!</b>
                                </template>
                            </td>
                        </tr>
                    </template>                    
                </template>
            </tbody>
        </table>
    </div>
</template>




JS
import { LightningElement,wire, } from 'lwc';
import fetchAccounts from '@salesforce/apex/AccountController55.fetchAccounts';
export default class ExpandableTable extends LightningElement {
    
    records;
    error;
    @wire( fetchAccounts )  
    wiredAccount( { error, data } ) {
        if (data) {
            console.log( 'Fetched Data ' + JSON.stringify( data ) );
            this.records = data;
        } else if ( error ) {
            this.error = error;
            this.records = undefined;
        }
    }  
    hideAndShow( event ) {
        let indx = event.target.dataset.recordId;
        console.log( 'Index is ' + indx );
        if ( this.records ) {
            let recs =  JSON.parse( JSON.stringify( this.records ) );
            let currVal = recs[ indx ].hideBool;
            console.log( 'Current Val ' + currVal );
            recs[ indx ].hideBool = !currVal;
            this.records = recs;
            console.log( 'After Change ' + this.records[ indx ].hideBool );
        }
    }
}



Apex
public with sharing class AccountController55 {
 
    @AuraEnabled( cacheable = true )
    public static List< AccountWrapper > fetchAccounts() {
     
        List< AccountWrapper > listWrap = new List< AccountWrapper >();
        
        for ( Account objAcc : [ SELECT Id, Name, Industry, AccountNumber, Rating, Type,  
                                        ( SELECT Id, FirstName, LastName, Email FROM Contacts )
                                   FROM Account
                                  LIMIT 10 ] ) {
            
            AccountWrapper objWrap = new AccountWrapper( true, objAcc, objAcc.Contacts.size() > 0 ? true : false );
            listWrap.add( objWrap );

        }

        return listWrap;
         
    }

    public class AccountWrapper {

        @AuraEnabled
        public Boolean hideBool;
        @AuraEnabled
        public Boolean contactBool;
        @AuraEnabled
        public Account objAccount;

        public AccountWrapper( Boolean hideBool, Account objAccount, Boolean contactBool ) {

            this.hideBool = hideBool;
            this.objAccount = objAccount;
            this.contactBool = contactBool;

        }

    }
     
}
Please Mark It As Best Answer If It Helps
Thank You!
Guru Vemuru 11Guru Vemuru 11
Hi CharuDutt,
Thank you so much.
Is That Possible to make it dynamic like we will pass Objectname, Feild name so that It is available to all objects?
Regards
Guru