Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Step-by-step guide to creating a dynamic tooltip in a Salesforce Lightning Datatable component.
LWC

How to Create a Tooltip in Lightning Datatable

Add hover tooltips to lightning-datatable account links showing phone and address using LWC and Apex.

The short answer

You can add hover tooltips to lightning-datatable links by configuring a URL column type with a dynamic tooltip property inside typeAttributes. In the LWC JavaScript controller, map queried Apex data into a formatted string to display related record details on hover without page navigation.

Key takeaways Configure the datatable column type as url and map the tooltip property in typeAttributes to display dynamic hover text. Annotate read-only Apex query methods with @AuraEnabled(cacheable=true) to enable client-side caching with the @wire decorator. Construct multi-line tooltip strings using newline characters while guarding against null related record references. Use a custom cell renderer or popover component instead of standard tooltips if you need to display formatted HTML or images.

Add hover tooltips to lightning-datatable account links showing phone and address using LWC and Apex.

Overview

This article shows a practical approach to add a tooltip to an Account Name link inside a lightning-datatable built with a Lightning Web Component (LWC). The tooltip provides quick access to the Account's Phone and Billing Address without navigating away from the table — similar to the Classic mini page layout experience but implemented for Lightning Experience.

What you need

  • An Apex controller to query Contact and Account fields.
  • A Lightning Web Component with a lightning-datatable.
  • Configure the Account column as a URL and set its tooltip attribute dynamically.

1. Data preparation (Apex)

Query Contact records with related Account fields (Name, Phone, Billing Address). Mark the Apex method @AuraEnabled(cacheable=true) so it can be used with @wire and supports client-side caching.

public class ContactsController {
    @AuraEnabled(cacheable=true)
    public static List getContacts(){
        return [SELECT Id,Name,Phone,Email,AccountId,Account.Name,Account.Phone,
            Account.BillingStreet,Account.BillingCity,Account.BillingState,
            Account.BillingpostalCode
            FROM Contact
            LIMIT 10 ];
    }
}

2. LWC - JavaScript

Use the wired Apex method to transform contact rows so the datatable receives a URL field (AccountUrl), a label (AccountName) and a tooltip string (accountToolTip). The tooltip supports multi-line text; include phone and formatted address.

import { LightningElement, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactsController.getContacts';

const columns = [
    { label: 'Contact Name', fieldName: 'Name', type: 'text' },
    { label: 'Phone', fieldName: 'Phone', type: 'phone' },
    { label: 'Email', fieldName: 'Email', type: 'email' },
    {
        label: 'Account Name',
        fieldName: 'AccountUrl',
        type: 'url',
        typeAttributes: {
            label: { fieldName: 'AccountName' },
            target: '_blank',
            tooltip: { fieldName: 'accountToolTip' },
        },
    },
];

export default class ToolTipExample extends LightningElement {
    contacts = [];
    error;
    columns = columns;

    @wire(getContacts)
    wiredContacts({ error, data }) {
        if (data) {
            this.contacts = data.map((item) => ({
                ...item,
                AccountName: item.Account?.Name || '',
                AccountUrl: item.AccountId ? `/lightning/r/Account/${item.AccountId}/view` : '',
                accountToolTip: `Phone : ${item.Account?.Phone || 'N/A'}\nAddress : ${item.Account?.BillingStreet || ''}, ${item.Account?.BillingCity || ''}, ${item.Account?.BillingState || ''}`,
            }));
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.contacts = undefined;
        }
    }
}

3. LWC - HTML

A simple card containing the lightning-datatable. Use key-field="Id" and hide-checkbox-column if you don't need row selection.

<template>
  <lightning-card title="Contacts Table With ToolTip">
    <lightning-datatable key-field="Id" data={contacts} hide-checkbox-column columns={columns} >
    </lightning-datatable>
  </lightning-card>
</template>

Notes and Best Practices

  • Use cacheable=true for read-only Apex methods to improve performance with @wire.
  • Guard against null relationships (e.g., item.Account may be null) when building the tooltip string.
  • Keep tooltip content concise — show the most useful fields (phone, short address) to avoid cluttered popups.
  • If you need richer content (images or formatted HTML), consider a custom cell renderer or a popover component instead of the standard tooltip attribute.

Conclusion

Adding tooltips to a lightning-datatable Account link is an efficient way to give users quick access to related record information without navigation. This pattern improves productivity for users who frequently scan records and reduces context switching.

Why this matters for Salesforce admins and developers:

  • Admins can deliver better UX without changing page layouts or adding custom record pages.
  • Developers can implement lightweight, performant UI enhancements using standard LWC + Apex patterns.
  • Business users get faster access to key data points, improving decision speed and reducing clicks.

#datatable #LWC #tooltip #lightning-datatable #Salesforce

For more, please follow our page!

Frequently asked questions

How do you add a tooltip to a URL column in lightning-datatable?

Define the column with type 'url' and specify a tooltip field name inside the typeAttributes object. In your component's JavaScript, populate that field name on each row with the string you want to display on hover.

Can you display multi-line text in a lightning-datatable tooltip?

Yes, you can create multi-line tooltip text by combining fields with newline characters (\n) when mapping your row data in JavaScript.

How do you display images or rich HTML inside a lightning-datatable tooltip?

Standard datatable tooltips only support concise plain text. To render images, formatted HTML, or interactive content, use a custom cell renderer or a custom popover component.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment