Sub topic 10
Column widths and layout options
This chapter covers how lightning-datatable decides how wide each column is and which attributes change the layout. It assumes you have already built a table with a columns array, a data array, and a key-field, as in the earlier chapters. You should also be comfortable reading a column definition object and handling a custom event in a Lightning Web Component.
How the table sizes columns
The table has two width modes, set with the column-widths-mode attribute.
fixedis the default. Columns start with equal widths that fill the table. A column that has aninitialWidthorfixedWidthtakes that width first, and the remaining columns share what is left.autosizes columns from the width of their content and the width of the table.
Whatever the mode, a few rules apply.
- Text that does not fit in a cell is clipped with an ellipsis unless the column sets
wrapText. - Each column has a minimum and a maximum width. The
min-column-widthattribute defaults to 50 pixels andmax-column-widthdefaults to 1000 pixels. - If the minimum widths of all columns add up to more than the container, the table scrolls horizontally instead of squeezing the columns further.
Per-column width properties
Two properties on a column definition set a width in pixels.
| Property | Effect | User can resize? |
|---|---|---|
| `initialWidth` | The column starts at this width. | Yes |
| `fixedWidth` | The column stays at this width. | No |
Use fixedWidth for narrow columns with predictable content, such as an action menu, a checkbox-like icon, or a short status code. Use initialWidth when you want a sensible starting point that readers can still change, such as a wide Email column.
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text', sortable: true, initialWidth: 200 },
{ label: 'Email', fieldName: 'Email', type: 'email', initialWidth: 260 },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' },
{ label: 'Birthdate', fieldName: 'Birthdate', type: 'date-local' },
{
type: 'action',
fixedWidth: 50,
typeAttributes: {
rowActions: [
{ label: 'View', name: 'view' },
{ label: 'Delete', name: 'delete' }
]
}
}
];In fixed mode, Phone and Birthdate here split whatever space remains after Name (200), Email (260), and the action column (50).
Wrapping text and long content
By default every cell shows one line and clips the rest. To show all of a long value, set wrapText: true on that column. The row grows to fit the tallest cell.
{ label: 'Description', fieldName: 'Description', type: 'text', wrapText: true, initialWidth: 300 }Wrapping makes rows uneven in height, so use it only on columns where the full text matters. For other columns, clipping is usually better, and readers can widen the column by dragging its edge.
Resizing
Users can drag the edge of a column header to resize it. Three attributes control this.
resize-column-disabledturns off resizing for the whole table.min-column-widthandmax-column-widthset the limits a drag can reach.onresizefires after a resize. Itsevent.detailcontainscolumnWidths, an array of widths in column order, andisUserTriggered, which istruewhen the user dragged andfalsewhen the table resized itself, for example when the container changed size.
The table does not remember widths. If you assign a new columns array, widths are recomputed from the definitions, and a page reload starts over. To keep the user's widths, store them in onresize and write them back into initialWidth when you rebuild the columns.
import { LightningElement } from 'lwc';
export default class ResizableTable extends LightningElement {
columns = [
{ label: 'Name', fieldName: 'Name', initialWidth: 200 },
{ label: 'Email', fieldName: 'Email', type: 'email', initialWidth: 260 },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' }
];
handleResize(event) {
const { columnWidths, isUserTriggered } = event.detail;
if (!isUserTriggered) {
return;
}
this.columns = this.columns.map((column, index) => ({
...column,
initialWidth: columnWidths[index]
}));
}
}If a columns array contains a column without a fieldName, such as an action column, it still appears in columnWidths at its own index. Keep the mapping by index rather than by field name.
Row numbers and the checkbox column
The table can show two leading columns that are not part of your columns array.
- The checkbox column appears by default. Remove it with
hide-checkbox-column, as in the earlier chapters. - The row number column is off by default. Add it with
show-row-number-column.
The row-number-offset attribute shifts the numbers. This helps when you load data in pages and want the second page to start at 51 instead of 1. The attribute takes a number.
<template>
<lightning-card title="Contacts" icon-name="standard:contact">
<div class="table-container">
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
column-widths-mode="fixed"
min-column-width="80"
max-column-width="400"
show-row-number-column
row-number-offset={rowOffset}
hide-checkbox-column
onresize={handleResize}>
</lightning-datatable>
</div>
</lightning-card>
</template>Controlling table height
lightning-datatable has no height attribute. It grows to show every row you give it. When you want a scrolling table with a fixed viewport, wrap it in a container with a set height and let the table scroll inside it. This is also required for infinite loading, covered in a later chapter, because the table needs a scrollable area to detect when the user nears the bottom.
.table-container {
height: 320px;
}Put this rule in the component's CSS file. The header stays visible while the rows scroll, which is usually the layout readers expect for long lists.
Choosing between fixed and auto
| Situation | Better choice |
|---|---|
| Columns with similar, short content | `fixed` |
| A mix of very short and very long values and you want no manual tuning | `auto` |
| You need exact control over a few columns | `fixed` with `initialWidth` or `fixedWidth` |
| Content changes after load, such as a refreshed list | `fixed`, because widths do not jump when data changes |
In auto mode the table measures content, so a refresh that brings in longer values can shift the columns. In fixed mode the widths come from your definitions and stay put. For a table that users read repeatedly, that stability is usually worth the extra setup.
Common problems
- The table is scrolling sideways. The minimum widths add up to more than the container. Lower
min-column-width, removeinitialWidthon some columns, or hide a column on narrow screens by building a shortercolumnsarray. - A column ignores your width. The value is being clamped. A width below
min-column-widthrises to the minimum, and a width abovemax-column-widthdrops to the maximum. Raise the limit if the value is intentional. - A
fixedWidthcolumn cannot be dragged. This is by design. UseinitialWidthif you want it adjustable. - Widths reset after an edit or a refresh. A new
columnsarray was assigned. Store widths fromonresize, as shown above, or avoid reassigningcolumnswhen only the data changes. - Row numbers restart on each page. Set
row-number-offsetto the number of rows already shown.
Exercise
A table has columns Name, Email, and an action column. You want the action column locked at 50 pixels, the Email column to start at 260 pixels but stay adjustable, and the numbering to begin at 101 because the first 100 rows were loaded earlier. Which column properties and table attributes do you use?
Show answer
On the action column set fixedWidth: 50. On the Email column set initialWidth: 260. On the table add show-row-number-column and row-number-offset={rowOffset}, with rowOffset set to 100 in the component class, so the first new row is numbered 101.
Exercise
Users resize columns, then the component reloads the data and the widths snap back. What causes this, and how do you fix it?
Show answer
Reassigning the columns array makes the table recompute widths from the definitions, and the table does not remember user widths. Handle onresize, ignore events where isUserTriggered is false, and copy columnWidths[index] into initialWidth for each column. Alternatively, avoid reassigning columns when only data changes.