Custom Data Table with Images in LWC

Extend Lightning Datatable to display images with custom column types

Component Overview

This implementation creates a custom Lightning Datatable component that:

Key Features

  1. Custom Column Types: Extends LightningDatatable with new column type
  2. Image Display: Shows images from URLs in table cells
  3. Reusable Component: Can be used across your org
  4. Lightning Design: Matches Salesforce UI standards

Implementation Steps

Follow these steps to implement this solution:

1. Create the Parent Component

This component configures and uses the custom datatable.

HTML (customDataTable.html)

<template>
    <c-custom-data-table-type-controller
        key-field="id"
        data={data} 
        columns={columns} 
        hide-checkbox-column
    ></c-custom-data-table-type-controller>
</template>

JavaScript (customDataTable.js)

import { LightningElement, track } from 'lwc';

// Table column definitions
const columns = [   
    { label: 'Name', fieldName: 'name' },
    { label: 'Model', fieldName: 'type' },
    { 
        label: 'Image',
        fieldName: 'imageUrl',
        type: 'imageColumenForDataTable', // custom column type for image
        typeAttributes: {
            imgUrl: { fieldName: 'imageUrl' }
        }
    },
];

export default class customDataTable extends LightningElement {
    accountData;
    header = [];
    columns = columns;

    // Table data
    @track data = [
        { id: 1, name: 'Hyundai', type: 'Creta', buttonname: 'CS1', imageUrl:'https://imgd.aeplcdn.com/370x208/n/cw/ec/106815/creta-exterior-right-front-three-quarter-5.jpeg?isig=0&q=80'},
        { id: 2, name: 'Toyota', type: 'Fortuner', buttonname: 'EC2', imageUrl:'https://imgd.aeplcdn.com/664x374/n/cw/ec/44709/fortuner-exterior-right-front-three-quarter-21.jpeg?isig=0&q=80'},
    ];
}

2. Create the Custom Data Table Controller

This extends LightningDatatable to support the custom image column type.

JavaScript (customDataTableTypeController.js)

import { LightningElement } from 'lwc';
import LightningDatatable from 'lightning/datatable';
import imageColumenForDataTable from './imageColomManage.html';

export default class customDataTableTypeController extends LightningDatatable {
    static customTypes = {
        imageColumenForDataTable: {
            template: imageColumenForDataTable,
            typeAttributes: ['imgUrl']
        }
    };
}

3. Create the Custom Column Template

This defines how the image column should be rendered.

HTML (imageColomManage.html)

<template>
    <img src={value} alt="Table image" style="max-height: 50px;"/>
</template>

How to Use This Component

  1. Create all three LWC components in your Salesforce org
  2. Deploy the components to the appropriate pages
  3. Configure the parent component with your data:
    • Update the columns array to match your field structure
    • Provide your data in the data property
    • Ensure image URLs are accessible (consider CORS restrictions)
  4. Add any additional custom column types as needed

Best Practices

Component Output

Enhancement Ideas