Intermediate 18 min read

lightning-datatable Fundamentals

By the end of this lesson, you'll be able to:

  • Render tabular data using lightning-datatable
  • Define a columns configuration for a datatable
  • Handle basic row selection

Prerequisites: Module 9: "Project: A Mobile-Responsive Salesforce Workspace"

Why lightning-datatable?

Building a data table from a plain HTML <table> means hand-rolling sorting, selection, accessibility, and styling from scratch. lightning-datatable provides all of that out of the box — the same component Salesforce's own list views use internally.

Columns Configuration

get columns() {
    return [
        { label: 'Name', fieldName: 'name', type: 'text' },
        { label: 'Industry', fieldName: 'industry', type: 'text' },
        { label: 'Annual Revenue', fieldName: 'revenue', type: 'currency' },
    ];
}

Each column is an object with a label (the header text shown), a fieldName (which key on each row's data object to read), and a type (text, currency, date, email, and more — each renders and formats differently).

Basic Usage

<lightning-datatable
    key-field="id"
    data={accounts}
    columns={columns}>
</lightning-datatable>

key-field names the property on each row that uniquely identifies it (similar in purpose to the key directive from Module 3's list rendering lesson) — required so the datatable can track rows correctly across re-renders.

Row Selection

<lightning-datatable
    key-field="id"
    data={accounts}
    columns={columns}
    onrowselection={handleRowSelection}>
</lightning-datatable>
handleRowSelection(event) {
    this.selectedRows = event.detail.selectedRows;
}

Adding checkboxes for row selection needs no extra configuration — it's automatic once an onrowselection handler is present, and event.detail.selectedRows gives the array of currently selected row objects.

Exercise

Write a columns getter for a Contact table with Name (text) and Email (email type) columns.

Show hint

Each column needs label, fieldName, and type.

JAVASCRIPT

Exercise

Challenge: explain, as a comment, why key-field is required and what would go wrong without a genuinely unique value in it.

Show hint

Recall Module 3's discussion of keys in list rendering.

JAVASCRIPT

lightning-datatable Fundamentals Quiz

1. What does the fieldName property on a column configuration object specify?

2. What is required on lightning-datatable to track rows correctly across re-renders?

3. What event fires when a user selects or deselects rows?

4. Does adding row selection checkboxes require extra column configuration beyond an onrowselection handler?

5. What does event.detail.selectedRows contain?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

lightning-datatable is the built-in component for displaying tabular data — sorting, selection, and inline editing all come built in, rather than needing to be hand-rolled from a plain HTML table.