# How do you configure and manage data display using the lightning-datatable component?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Datatable, UI Components

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-configure-and-manage-data-display-using-the-lightning-datatable-component

## Answer

**Basics:** It is a base component for tables of records. You describe each column (`label`, `fieldName`, `type`) and pass rows as plain objects. Common types: `text`, `number`, `currency`, `percent`, `date`, `date-local`, `boolean`, `url`, `email`, `phone`, `button`, `button-icon`, `action`. `key-field` names a property whose value is unique per row (usually `Id`).

**How it works:** Setup: define columns, assign `data` from a wire or Apex call. Sorting: mark columns `sortable: true`, bind `sorted-by`, `sorted-direction` and `onsort`, and sort the array yourself (the table only emits the event). Selection: `onrowselection`, `getSelectedRows()`, `selected-rows`, `max-row-selection`. Row actions: a column `{ type: 'action', typeAttributes: { rowActions } }` with `onrowaction` giving `event.detail.action` and `event.detail.row`. Inline editing: set `editable: true` on columns, handle `onsave` (`event.detail.draftValues` holds changed fields plus the key), save, then clear `draft-values`. Infinite scroll: `enable-infinite-loading` and `onloadmore`.

**Advanced / deep dive:** Data must be flat: relationship fields such as `Account.Name` are not read by `fieldName` with a dot path, so flatten rows (`AccountName: r.Account?.Name`). Wire results are read-only, so clone before adding properties. Custom cell types are made by extending `LightningDatatable` with `static customTypes` and a template per type (useful for pickers or lookups, which are not native). Mutating a row in place will not re-render; reassign a new array. For very large data sets, page or load incrementally; rendering thousands of rows hurts performance. Edits are not saved automatically: you decide whether to call `updateRecord` per row (LDS, runs triggers and validation) or Apex for bulk, and you should surface per-row problems with the `errors` attribute.

**Code / config example:**
```javascript
const COLUMNS = [
    { label: 'Name', fieldName: 'Name', type: 'text', sortable: true, editable: true },
    { label: 'Amount', fieldName: 'Amount', type: 'currency', editable: true },
    { label: 'Close', fieldName: 'CloseDate', type: 'date-local' }
];
// html: <lightning-datatable key-field="Id" data={rows} columns={columns}
//        draft-values={drafts} onsave={handleSave} sorted-by={sortBy}
//        sorted-direction={sortDir} onsort={handleSort}></lightning-datatable>
async handleSave(e) {
    const recs = e.detail.draftValues.map(d => ({ fields: { ...d } }));
    await Promise.all(recs.map(r => updateRecord(r)));
    this.drafts = [];
}
```

**Hidden gotchas & must-remember points:**
- `key-field` values must be unique or selection and editing behave erratically.
- Dot-notation field names are not supported; flatten data.
- The table never sorts or saves for you.
- Draft values persist until you set `draft-values` to an empty array.
- Not every field type is editable inline; picklist and lookup need custom types.
- Wired data is frozen; spread into new objects.

**Best practice:** Keep the table presentational: flatten data in one mapper function and put sorting, paging and saving in clearly named handlers.

---
Source: ForceChallenges · last updated 2026-11-02
