Sub topic 10
Column widths and layout options
This chapter assumes you can already define a columns array with label, fieldName, and type, and that your table renders data with a key-field. It covers how the table decides column widths, how to override them, and the layout attributes that sit on the table itself.
How widths are decided
lightning-datatable sizes columns in one of two modes, set with column-widths-mode:
fixed(the default) gives every column the same width, unless a column sets its own width.autosizes each column to fit its content and the available space.
Individual columns can override the mode with two properties in the column definition:
initialWidthsets the starting width in pixels. The user can still resize the column.fixedWidthsets a width in pixels that the user cannot change.
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text', initialWidth: 220 },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone', fixedWidth: 140 },
{ label: 'Birthdate', fieldName: 'Birthdate', type: 'date-local', initialWidth: 130 }
];Here Name and Birthdate start at a set width, Phone is locked, and Email takes a share of what is left.
Table-level layout attributes
| Attribute | Effect |
|---|---|
| `column-widths-mode` | `fixed` or `auto`, as described above |
| `resize-column-disabled` | Turns off user resizing for all columns |
| `min-column-width` | Smallest width a user can drag a column to (default 50) |
| `max-column-width` | Largest width a user can drag a column to (default 1000) |
| `show-row-number-column` | Adds a column that numbers the rows |
| `row-number-offset` | Starts the numbering after the given offset |
| `hide-checkbox-column` | Removes the selection checkboxes |
A template that combines several of them:
<template>
<lightning-card title="Contacts" icon-name="standard:contact">
<div style="height: 320px;">
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
column-widths-mode="auto"
min-column-width="80"
show-row-number-column
hide-checkbox-column
onresize={handleResize}>
</lightning-datatable>
</div>
</lightning-card>
</template>The wrapping div matters. The table scrolls inside its container, so the container needs a height. Without one, the table grows to fit all rows.
Reacting to resizes
When a user drags a column border, the table fires a resize event. The detail includes columnWidths, an array of widths in column order, and isUserTriggered, which is false when the table resized itself.
handleResize(event) {
const { columnWidths, isUserTriggered } = event.detail;
if (isUserTriggered) {
this.savedWidths = columnWidths;
}
}You can store these widths, for example in local storage, and feed them back as initialWidth values the next time you build the columns.
Row numbers
The row number column shows each row's position in the current display, starting at 1. After a sort, the numbers stay in order and the records move. They are not record identifiers, so do not use them to find a row. Use the Id from the key-field for that. Set row-number-offset when you load more rows in pages and want the numbering to continue.
Common errors
- Mixing
fixedWidthandinitialWidthon one column. Use one or the other.fixedWidthblocks resizing, soinitialWidthadds nothing. - Expecting
min-column-widthto shrink afixedWidthcolumn. Fixed columns ignore the limit. - Narrow widths that cut off content. A phone or email value that does not fit is truncated. Use
column-widths-mode="auto"or a largerinitialWidth. - Forgetting the container height. The table then has no scroll area, which also breaks infinite loading later.
Exercise
A table shows 20 rows in page after page, and you load 20 more each time. The row number column restarts at 1 for the new batch. Which attribute fixes the numbering, and what value would you give it for the second batch?
Show answer
Use row-number-offset. For the second batch of 20 rows, set it to 20 so the numbers start at 21.