Datatable

lightning-datatable

A table that displays rows and columns of data.

For Use In

Lightning Experience, Experience Builder Sites, Lightning Out (Beta), Standalone Lightning App

lightning-datatable displays tabular data where each column renders the content based on the data type. For example, an email address is displayed as a hyperlink with the mailto: URL scheme by specifying the email type. The default data type on a column is text.

This component implements styling from the data tables blueprint in the Salesforce Lightning Design System.

lightning-datatable is not supported on mobile devices. Supported features include:

  • Displaying and formatting of columns with appropriate data types
  • Infinite scrolling of rows
  • Inline editing for some data types
  • Header actions
  • Header wrapping
  • Row-level actions
  • Resizing of columns
  • Selecting of rows
  • Sorting of columns by ascending and descending order
  • Text wrapping and clipping
  • Row numbering column
  • Cell content alignment

Tables can be populated during initialization using the data, columns, and key-field attributes. The key-field attribute is required for correct table behavior. It associates each row with a unique identifier. The value you use for key-field is case-sensitive and must match what's in your data array. If you use key-field="Id", make sure the Id you provide in the data array uses Id and not id.

This example creates a table whose first column displays a checkbox for row selection. This column is displayed by default, and you can hide it by adding hide-checkbox-column in your markup. Selecting the checkbox selects the entire row of data and triggers the onrowselection event handler.

Here's the JavaScript that creates selectable rows and the columns object to their corresponding column data. The Probability column displays percentages with an icon that denotes the increasing or decreasing confidence trend.

This example defines five columns by setting properties and attributes for the columns object. The Probability column displays percentages and an icon that denotes the increasing or decreasing confidence trend. The icon is specified with the cellAttributes property. See Working with Column Properties for more information about the properties for columns.

The JavaScript also loads two rows of data in the table. The Id for each table row is used as the key-field.

When the data table is rendered, each row displays a checkbox in the first column. The first row shows columns with the following data: Cloudhub, 20%, $25,000.00, jrogers@cloudhub.com, and (235) 223-5235. The last two columns are displayed as hyperlinks to represent an email address and telephone number.

Selecting any number of rows via checkboxes or a radio button in the selection column emits the onrowselection event. The event.detail.selectedRows property contains a list of all selected rows after any checkbox or radio button interaction. Alternatively, the event.detail.config.value contains a single id value of the row that was toggled.

This JavaScript function displays the email addresses of all rows as they're selected.

Use the event.detail.config.action property to keep a running list of rows that have been selected or deselected. event.detail.config.action has four possible values:

  • selectAllRows: all rows are selected via the checkbox in the column header
  • deselectAllRows: all rows are deselected via the checkbox in the column header
  • rowSelect: single row is selected via the row checkbox
  • rowDeselect: single row is deselected via the row checkbox

This JavaScript example keeps two running lists of row objects: a list of rows that have ever been selected and a list of row objects that are currently selected.

To return the data in each selected row, use the getSelectedRows() method. This example displays the data of the selected row(s) in a paragraph.

To query your datatable, use the lwc:ref directive. This example uses lwc:ref="dt" where dt is the unique reference. Call the reference using this.refs.dt.

For example, if two rows are selected and you click the Get Selected Row button, the paragraph displays:

To return a simple list of records, use the getListUi wire adapter. To select certain accounts using SOQL, use an Apex method. See Call Apex Methods.

This example loads the first 10 contacts in the datatable using a SOQL query.

The template wires up the datatable to the contact records using the id field.

Load the columns using JavaScript. This example wires the getContactList Apex method to the contacts property.

Use the following column properties to customize the behavior and visual aspects of your columns.

PropertyTypeDescription
actionsobjectAppends a dropdown menu of actions to a column. You must pass in a list of label-name pairs.
cellAttributesobjectProvides additional customization using alignment and class properties, in addition to icon* properties such as for appending an icon to the output. For more information, see Appending an Icon to Column Data.
editablebooleanSpecifies whether a column supports inline editing. The default is false.
fieldNamestringRequired. The name that binds the columns attributes to the associated data. Each columns attribute must correspond to an item in the data array.
fixedWidthintegerSpecifies the width of a column in pixels and makes the column non-resizable. If both fixedWidth and initialWidth values are provided, initialWidth is ignored.
hideDefaultActionsbooleanSpecifies whether to hide the default header actions on a column. The default is false. For more information, see Creating Header Actions.
hideLabelbooleanSpecifies whether to hide the label on a column. The default is false. For more information, see Displaying an Icon Instead of a Column Label.
iconNamestringThe Lightning Design System name of the icon. Names are written in the format standard. The icon is appended to the left of the header label. If hideLabel is provided, only the icon displays in the column header.
imgSrcstringThe URI for a custom icon. The icon is appended to the left of the header label. If hideLabel is provided, only the icon displays in the column header. If there's an error loading the URI, iconName is used as a fallback.
initialWidthintegerThe width of the column when it's initialized, which must be within the min-column-width andmax-column-width values, or within 50px and 1000px if they are not provided.
labelstringRequired. The text label displayed in the column header. If hideLabel and iconName are set, the label is used as hover text for the icon and as the value of aria-label.
sortablebooleanSpecifies whether the column can be sorted. The default is false.
typestringRequired. The data type to be used for data formatting. For more information, see Formatting with Data Types.
typeAttributesobjectProvides custom formatting with component attributes for the data type. For example, currency-code for the currency type. For more information, see Formatting with Data Types.
wrapTextbooleanSpecifies whether text in a column is wrapped when the table renders. Wrapped text vertically expands a row to reveal its full content. Use with wrap-text-max-lines to display a number of lines before hiding the rest. For more information, see Text Wrapping and Clipping.

The data table formats the data cells of a column based on the type you specify for the column. To get correct formatting, specify a type that matches the field type you pass in to a column.

The default data type for a column is text. Object values passed into a text type column are displayed as an empty string.

For number and string values like percent, currency, date, email, and phone numbers, the text type column displays the unformatted value of the string. For example, specify type: 'date' in the column definition if you're passing in a date to the column. The date is then formatted according to the user's Salesforce locale. If you don't specify the date type, the retrieved date string is formatted as text.

Additionally, specify type: 'boolean' if you're passing in a boolean value to a column to display a checkmark for a true value. Passing in a boolean value to a column of text type displays the value as true or false.

Each data type is associated with a base Lightning component. For example, the text type renders the associated data using a lightning-formatted-text component. The phone type renders the associated data using a lightning-formatted-phone component, and the phone number is formatted for the user's locale.

To customize your output, pass in the attributes of the base component via the typeAttributes property. Not all attributes on a base component are supported. For more information, see the table on supported type attributes below.

The properties you pass with typeAttributes must be specified using the format shown here, not the format that's used for Lightning web component attributes in your HTML template. For example, although lightning-formatted-number recognizes a currency-code attribute, you must specify it as currencyCode with the typeAttributes property. For supported attribute values, refer to the component's documentation.

Valid data types and their supported attributes include:

TypeDescriptionSupported Type Attributes
actionDisplays a dropdown menu using lightning-button-menu with actions as menu items. The default dropdown menu alignment, denoted by menuAlignment, is right. Valid options for menuAlignment are right, left, auto, center, bottom-left, bottom-center, and bottom-right. See Creating Static Row-Level Actions.rowActions (required), menuAlignment (defaults to right)
booleanDisplays the icon utility if the value is true, and a blank value otherwise.N/A
buttonDisplays a button using lightning-buttondisabled, iconName, iconPosition, label, name, title, variant
button-iconDisplays a button icon using lightning-button-iconalternativeText, class, disabled, iconClass, iconName, name, title, variant
currencyDisplays a currency using lightning-formatted-number. See Displaying Currency and Percentages.currencyCode, currencyDisplayAs, minimumIntegerDigits, minimumFractionDigits, maximumFractionDigits, minimumSignificantDigits, maximumSignificantDigits, step
dateDisplays a date and time based on the locale using lightning-formatted-date-time. See Displaying Date and Time Using Type Attributes.day, era, hour, hour12, minute, month, second, timeZone, timeZoneName, weekday, year
date-localDisplays a date that is formatted based on the locale using lightning-formatted-date-time. To include a time value, use the date type instead. The value passed is assumed to be in the browser local time zone and there is no time zone transformation. See Displaying Date and Time Using Type Attributes.day, month, year
emailDisplays an email address using lightning-formatted-emailN/A
locationDisplays a latitude and longitude of a location using lightning-formatted-locationlatitude, longitude
numberDisplays a number using lightning-formatted-numberminimumIntegerDigits, minimumFractionDigits, maximumFractionDigits, minimumSignificantDigits, maximumSignificantDigits
percentDisplays a percentage using lightning-formatted-number. See Displaying Currency and Percentages.minimumIntegerDigits, minimumFractionDigits, maximumFractionDigits, minimumSignificantDigits, maximumSignificantDigits, step
phoneDisplays a phone number using lightning-formatted-phoneN/A
textDisplays text using lightning-formatted-text. This is the default data type.linkify
urlDisplays a URL using lightning-formatted-urllabel, target, tooltip

In some situations, multiple columns reference the same fieldName but have different fieldApiNames and different ways of working with the field information. To give a column a unique ID when using the same field name for two columns, use the attribute columnKey instead of fieldName on one of the column definitions.

To customize the formatting based on the data type, pass in the attributes for the corresponding base Lightning component. For example, to display the URL value as a tooltip on a URL, pass in the tooltip value.

For more information on attributes and their supported values, see the corresponding base component documentation.

For more information on creating your own data types, see Create a Custom Data Type.

Currency type displays a value based on the org currency and your Salesforce locale by default.

To override the default currency code, pass in a custom currencyCode value.

To specify the granularity on a currency or percentage for inline editing, pass in the step attribute. For example, specify step: 0.01 to allow numbers with two decimal places, or step: 0.001 permits three decimal places. Specify step: 1 to require whole numbers. The default is 0.01. You can preserve the number of fraction digits for display using minimumFractionDigits and maximumFractionDigits.

For example, if you pass in probability: 0.21234 in your column data, the display value is 21.234%. When you inline edit, the step value is used to determine if your input is valid. If you pass in probability: 0.78, the display value is 78.00% because minimumFractionDigits is set to 2.

The locale set in the Salesforce user preferences determines the default formatting for date and time types.

The data table supports the date object and dates provided as ISO8601 formatted strings. Timestamps are not supported. These date formats can be displayed differently in a column. The default format is September 26, 2020, which corresponds to type: "date" or type: "date-local" and an empty typeAttributes property.

While the date type can be used to display date and time, the date-local type displays only the date. Here's how you can display Salesforce date and time data types in lightning-datatable.

Salesforce Data TypeDatatable Data TypeDescription
DateTimedateExpects date and time as input, and formats it according to the user's locale.
Datedate-localExpects date as input, and formats it according to the user's locale. Does not include time conversion.

Here are several ways to display date and time in a column.

09/26/2018

Wednesday, September 26, 2018

September 26, 2018, 12:00 PM

Wednesday, September 26, 2018, 12:00 PM

The locale set in your Salesforce preferences determines the formatting. For more information, see the lightning-formatted-date-time documentation.

To horizontally align content in a column, use the cellAttributes property to pass in the alignment attribute and its setting, which can be left, right, or center.

By default, number types align to the right. Number types include the currency, number, percent types.

The action type aligns to the center and cannot be overridden by the alignment attribute. All other types align to the left.

To override the alignment of the action type, consider using custom types and provide your own markup. See Create a Custom Data Type.

To display an icon instead of a label in the column header, specify the properties iconName and hideLabel in the column definition. Specify iconName as a utility icon.

To display a custom image in the column header, use the imgSrc property to specify a link to an image. To use an image in your org, upload the image as a static resource in the Static Resources setup page and then import the image from the @salesforce/resourceUrl module.

When both iconName and imgSrc are provided, iconName is used as a fallback if there is an error when attempting to load the image provided by imgSrc.

To support assistive technologies, set the label property for the column even though the label is hidden. Use standard header text that you'd use without an icon. The label text is used to set values for the icon's title and alternativeText attributes. The text is displayed as hover text. If the column has header controls the label text is used in aria-label for the header.

To append an icon to your data output, use cellAttributes and pass in these attributes.

AttributeDescription
iconNameRequired. The Lightning Design System name of the icon, for example, utility.
iconLabelThe label for the icon to be displayed on the right of the icon.
iconPositionThe position of the icon relative to the data. Valid options include left and right. This value defaults to left.
iconAlternativeTextDescriptive text for the icon.

You can add an icon with or without a label. This example defines two columns with icons. The first column specifies the utility:event icon for all rows using the iconName cell attribute, and the icon displays to the left of the data without a label. The second column uses computed values for the iconName and iconLabel and displays the icon to the right of the data.

Pass Lightning Design System (SLDS) classes to the class cell attribute when using standard data types. The classes you add are applied to all rows in a column.

Custom classes are currently not supported. To apply custom styling on your datatable cells, create a custom data type and then apply your custom CSS classes. See Custom Data Type Layout and Styles.

You can display a lock icon on read-only fields to specify they're not editable. To specify that a column's field as read-only, set the column attribute editable to false and the column attribute displayReadOnlyIcon to true in the associated column definition. This example displays a lock icon on each field in a column called "Website."

Infinite scrolling enables you to load a subset of data and then display more when users scroll to the end of the table. To enable infinite scrolling, specify enable-infinite-loading and provide an event handler using onloadmore. By default, data loading is triggered when you scroll down to 20px from the bottom of the table, but the offset can be changed using the load-more-offset attribute. Place the table in a container with a defined height to establish the bottom of the table. This limit enables the datatable to calculate when to load more and prevents infinite looping.

This example places the datatable in a container with a height of 500px. The table loads 50 more rows from the database when you reach the end of the table until there are no more data to load.

The onloadmore event handler retrieves more data when you scroll to the bottom of the table until there are no more data to load. To display a spinner while data is being loaded, set the isLoading property to true.

While this example uses a fixed number to denote the total number of rows, you can also use the SOQL SELECT syntax with the COUNT() function to return the number of rows in the object in your Apex controller. Then, set the result on the totalNumberOfRows attribute during initialization.

Scrolling to the top of the datatable programmatically is useful if there are more rows than can fit the datatable's height. To quickly return to the first row, use the scrollToTop() method.

To call the scrollToTop() method, use the lwc:ref directive. This example uses lwc:ref="datatable" where datatable is the unique reference. Call the reference using this.refs.datatable.

The scrollToTop() method applies to the datatable's scrollbar only. It doesn't apply to the browser's scrollbar or an outer container scrollbar.

When you nest the datatable in a div container that specifies a height, the datatable header stays fixed at the top when you scroll through the rows. When the datatable is not nested in a div container with a height, the datatable header isn't fixed and can scroll. In the latter case, scrollToTop() doesn't scroll back to the top.

Header actions refer to tasks you can perform on a column of data, such as displaying only rows that meet a criteria provided by the column. You can perform actions on a column and handle them using the onheaderaction event handler.

Header actions are available in the dropdown menu in the column header. The default header actions available on each column are as follows. For more information, see Text Wrapping and Clipping.

Action NameDescription
Wrap textExpands the row vertically to reveal more content if content is wider than the column width. Use wrap-text-max-lines to determine the number of lines to show when content is wrapped.
Clip textTruncates content to a single line with an ellipsis if content is wider than the column width. Content is clipped by default.

To hide the dropdown menu with the default header actions on a column, pass in the hideDefaultActions property.

If hideDefaultActions is set to true on a column that has custom header actions, the "Clip text" and "Wrap text" actions are removed from the actions dropdown menu, and the content is clipped by default. To wrap text when the default actions are hidden, set wrapText: true in the column definition.

To create a custom header action, use the attributes as follows.

AttributeDescription
labelRequired. The label that's displayed for the action.
nameRequired. The name of the action, which identifies the selected action.
checkedSpecifies whether a check mark is shown to the left of the action label. If true, a check mark is shown to the left of the menu item. If false, a check mark is not shown but there is space to accommodate one.
disabledSpecifies whether the action can be selected. If true, the action item is shown as disabled. This value defaults to false.
iconNameThe name of the icon to be displayed to the right of the action item.

For example, suppose you want to create a filter that displays only rows where the Publishing State column matches either the Published or Unpublished state.

Bind the header actions to the actions column attribute, which can be done during initialization.

Finally, display the rows to match the selected filter, which is performed by this.updateBooks().

Row-level actions refer to tasks you can perform on a row of data, such as updating or deleting the row. Static actions apply to all rows on the table. You can perform actions on each row and handle them using the onrowaction event handler.

Supported attributes for the row actions are as follows.

AttributeDescription
labelRequired. The label that's displayed for the action.
nameRequired. The name of the action, which identifies the selected action.
disabledSpecifies whether the action can be selected. If true, the action item is shown as disabled. This value defaults to false.
iconNameThe name of the icon to be displayed to the right of the action item.

You must provide a list of actions to the columns data, which can be done during initialization. This JavaScript initializes the actions column and handles the actions on each row, displaying the row details and deleting the row when the action is clicked.

Dynamic actions are created based on the content of each row. When you click the dropdown menu, an asynchronous call is made to determine which actions to display for the particular row. The logic that determines which action to display can be created on initialization. In this example, the action and its label is evaluated when the dropdown menu is activated. Assume that we have an active column that displays the status of a contact (Active or Inactive), which determines which action to display (Deactivate or Activate).

The previous example illustrates how to create and handle dynamic actions on the client-side only. You can make server calls and persist your record data changes via an Apex controller.

The width and height of the datatable is determined by the container element. A scroller is appended to the table body if there are more rows to display. For example, you can restrict the height to 300px by applying CSS styling to the container element.

By default, columns are resizable. Users can click and drag the width to a minimum of 50px and a maximum of 1000px. Users can also resize the column width using the keyboard. For more information, see the Accessibility section.

You can customize the column widths in many ways. To specify your own width and disable resizing for a specific column, pass in fixedWidth to the column property. To specify an initial width and enable resizing for a specific column, pass in initialWidth to the column property.

Columns have a default minimum width of 50px and maximum width of 1000px. To change the minimum and maximum width of columns, use the min-column-width and max-column-width attributes. For example, if you want a user to be able to resize a column to a minimum of 80px, set min-column-width="80".

To prevent users from resizing columns, specify resize-column-disabled in your markup. The table can still adjust its column widths when you resize the browser window or the width of the parent container changes. The resize-column-disabled attribute allows the table to resize columns when column-widths-mode is set to auto. See the next section for more information.

Resizing a column fires the resize event. For more information, See the Custom Events section.

To return the column widths, use the event.detail property on the onresize event handler.

The column-widths-mode attribute accepts values of fixed (default) or auto. To provide granular control on your column widths, use this attribute with the fixedWidth and initialWidth column properties. See Implementing Fixed Width Mode and Implementing Auto Width Mode for more information.

Widths for the following columns are fixed and cannot be changed.

  • Row Number column
  • Selection (checkbox) column
  • Action column

Render columns with equal widths using column-widths-mode="fixed", which is the default setting. Any content that's too long to be displayed is clipped and appears with a trailing ellipsis. The column width is calculated by taking total available width and dividing equally among the columns.

You can specify your own widths using fixedWidth or initialWidth. The widths of the remaining columns without a specified fixedWidth or initialWidth value are equal.

Setting new data on the columns doesn't trigger resizing for columns, unless the new column definition specifies a change in fixedWidth or initialWidth values. In fixed mode, the columns automatically resizes and maintain equal widths when:

  • The browser window is resized
  • The parent container width for the datatable is changed
  • The row-number-offset value is changed
  • More or less data is passed in

When you manually resize a column to a larger width, the other columns maintain their widths, displaying a scrollbar to enable scrolling to the end of the table columns. When you manually resize to a smaller width, the other columns also maintain their widths.

You can resize manually using a mouse or a keyboard. On a keyboard, press enter in the header cell, tab to reach the resizer (column divider), and press the left or right arrow keys. On a touchscreen device, tap on the desired column resizer area, move to the desired width, and then release.

To trigger resizing of columns according to the length or size of data in a column, set column-widths-mode="auto". In auto width mode, the columns automatically resize when:

  • Data changes in at least one row and the number of rows stays the same
  • The column definition changes, such as a change in a column property or the number of columns

Pass a new reference of columns with changes for resize to take effect. The columns don't resize if there's only a change in the number of records in the data.

Column widths are calculated based on the width of the content displayed in the column and the total width of the table. Specify your own widths for particular columns using the fixedWidth or initialWidth properties. The widths of the columns without these properties are calculated based on the width of the content in the column and the remaining table width. If the columns definition is passed but no data is set yet, the columns are rendered based on the width of the column labels.

A column's width is limited by the max-column-width value, or 1000px by default. If a column width is calculated to be wider than the max-column-width value, the content is truncated and displayed with an ellipsis. If the column also specifies wrapText: true, the column results in a narrower width than if the column has clipped text.

A column's width is also limited by the min-column-width value, or 50px by default. If a column width is calculated to be narrower than the min-column-width value, the width is set to minimum column width and may have extra white space.

When you manually resize a column, the other columns maintain their widths. This behavior also occurs when a column is manually resized in fixed mode.

The columns keep their width ratios while adjusting the column widths when:

  • The browser window is resized
  • The parent container width for the datatable is changed
  • The rowNumberOffset value is changed

Auto width mode is supported for containers with block display, which corresponds to the display: block CSS property. lightning-datatable doesn't fully support containers with display:inline-block or flex properties.

You can specify how many rows can be selected. When the maximum number of rows have been selected, the datatable disables the checkboxes that aren't selected. You can select another checkbox only after you deselect one of the selected checkboxes.

To enable single row selection, set max-row-selection="1". For single row selection, the datatable displays a radio button by default. Selecting the radio button selects the entire row of data and triggers the onrowselection event handler.

To render a checkbox for single row selection, set single-row-selection-mode="checkbox".

The selected-rows attribute enables you to preselect rows.

To select a row programmatically, pass in the row key-field value.

If max-row-selection is set to a value less than the number of selected rows, only the specified number of rows is selected. For example, if you set max-row-selection to 2 and pass in ['a', 'b', 'c'] to selected-rows, only rows a and b are selected.

Use the disabled-rows attribute to prevent users from changing the selection status of specified rows. Pass row identifiers into disabled-rows to prevent the rows from being selected. Pass row identifiers into both disabled-rows and selected-rows to prevent the rows from being deselected.

To disable a row programmatically, pass in the row key-field value to the disabled-rows attribute. For example, disabledRows = ['a', 'b']; where a and b correspond to the id values of the rows to be disabled. Rows that are both disabled and selected count towards the max-row-selection. For example, if you set max-row-selection to 2, and pass in ['a', 'b'] to both disabled-rows and selected-rows, the user can't select additional rows.

To enable sorting of row data by a column label, set sortable to true for the column on which you want to enable sorting. Clicking a column header sorts rows by ascending order, and clicking it subsequently reverses the order. Handle the onsort event to update the table with the new column index and sort direction.

Here's an example of an onsort event handler.

The handler assigns fieldName to sortedByField, which stores the name of the field that is currently being sorted. It also retrieves sortDirection from the sort event. On first sort, sortDirection is asc.

To keep the original data intact, cloneData creates a shallow copy of the data array. The handler passes in the comparison function sortBy and pass it to the array sort() method. Then it updates the data property with the sorted data, sortDirection with the new sort direction, and sortedBy with the sorted column.

For more information, see the Sortable Column example in the Example tab.

When you make a column editable, a pencil icon appears when you hover over the cells in that column. Clicking the icon or pressing the Enter key triggers inline editing. Inline editing is not supported for date and location fields.

Make a column editable by setting editable to true when you are defining your columns.

You can handle the oncancel, oncellchange, and onsave actions when the cell value changes or is saved. When the onsave action is used, the Cancel and Save button appears after a value cell changes and you press the Enter or Tab key, or move away from the cell.

Retrieve the new value using event.detail.draftValues.

You can also program an external element, such as a button, to open an inline edit panel on the currently active cell or next editable cell in the datatable. Invoke the openInlineEdit() method in the external element that should direct a user to the first editable cell in your datatable component.

This example opens a datatable cell for inline edit when the user clicks an "Edit" button.

For more information, see Display Data in a Table with Inline Editing.

When invalid data is entered, lightning-datatable can display client-side validation error messages at the row level and the table level.

To display errors for a column, you must make the column editable. When there's an editable column, lightning-datatable sets the show-row-number-column attribute to true to display the row errors in the number column. You can't override this setting.

For cells with standard types, the datatable uses the lightning-input component to handle editing of the cell content, and also to aid in handling error messages. To display error messages with custom data types, you can use lightning-input with the attribute data-inputable in your edit template. Then use the same techniques described here for displaying errors.

To display error messages for any editable columns, specify the errors attribute in your lightning-datatable component.

The errors attribute represents an object that specifies a rows property to provide information for the rows that contain errors and a table property to provide error information to display for the table.

The errors attribute represents an object that accepts one or both of these properties.

  • rows-Provides information to display errors at the row level.
  • table-Provides information to display errors at the bottom of the table. See Display Errors for the Table.

The rows object keys are the datatable’s key-field values, which provide a unique identifier for each row. Using this key-field value, you can provide custom errors on specific rows.

Customize the error messages using these properties in the rows object.

  • title-Title for the error tooltip.
  • messages-Text of error messages, listed in an array.
  • fieldNames-Fields that triggered errors. Order the field names in a simple array to match the messages in the messages array.

To display custom messages under the fields with errors, you can specify fieldNames as an object instead of a simple array. See Display Custom Error Messages for Cells.

When an error is triggered, the datatable highlights in red the cell that corresponds to the specified fieldName value, and displays an error icon in the row number column. When you click the icon, a tooltip displays the text that's specified in title and messages.

For all types of error display, you must add the errors attribute to your component. This attribute's values can be modified in events that handle error conditions.

Your lightning-datatable component looks something like this.

In this example, you can click a button to trigger the errors to display.

For row-level errors, define the error messages and field names in the errors object. To map the messages to fieldNames, specify an array of field names in the same order in which you specify the messages. In the example, when the error is triggered, the borders for the amount and contact cells in the row with the key-field 1 turn red to represent the error state. The messages are displayed in the tooltip in the number column, not on the cells themselves.

To display custom error messages under each field that has an error, you can specify the fieldNames property as an object of key-value pairs specifying the field name and the message like this:

If a cell is in the error state, the error message appears below the field when you click the edit icon to correct the entry. If you want to display the error icon and tooltip in the number column in addition to the field level message, also include the title and messages properties.

To display error messages at the bottom of the table, use the table property in the error object like this:

When you use the table property, if an error occurs in an edited cell, the datatable displays an error icon in the bottom bar next to the Cancel and Save buttons. Click the icon to display a tooltip with the values of the title and messages properties that are set for the table property. The table-level errors require the table bottom bar to be present. Don’t include the attribute suppress-bottom-bar in the lightning-datatable component.

To display errors on rows and at the bottom of the table, declare both the rows and table properties in the errors object.

You can wrap or clip text within columns. Text wrapping expands the rows vertically to reveal more content. Text clipping truncates the content to a single line within the column.

To toggle between the two views, select Wrap text or Clip text from the dropdown menu on the column header.

If the number of characters is more than what the column width can hold, content is clipped by default.

Text wrapping and clipping are not supported for row number columns and the following data types:

  • action
  • boolean
  • button
  • button-icon
  • date-local

For text data type, text clipping converts newline characters to spaces and condenses multiple spaces or tabs to one space. Text clipping suppresses line breaks, truncates content to fit a single line in the column, and adds a trailing ellipsis. Text wrapping breaks lines and hyphenates words as needed to fit the column.

To enable text wrapping by default, set wrapText to true on the columns property.

To display a number of lines of text in the column and hide the remaining lines, use wrap-text-max-lines. This example displays three lines of text and hides the rest; the text on line 3 is truncated and displayed with an ellipsis.

Handle the selection of Wrap text or Clip text on the column header using the onheaderaction handler. To return the name of the action, use the event.target.action.name property.

You can also wrap or clip the text in the column header with the wrap-table-header attribute. The property has three possible values:

  • none (default)
  • all
  • by-column

none causes clipping on all column headers and all causes wrapping on all column headers. by-column wraps the header for columns that have the Wrap text setting enabled and clips the header for columns that have the Clip text setting enabled.

lightning-datatable renders with a grid role and a polite live region that announces whether the table is in navigation mode or action mode. The label toggles the action mode when you press the Enter key or Space Bar. It toggles back to navigation mode when you press the Esc key twice to return focus to the cell. The component also announces the column width during a resize.

Each row header renders with an aria-label attribute with the labels you provide for the column definition. By default, the row number column renders with aria-label="Row Number" and cannot be changed. When row selection is enabled, each row renders with aria-selected set to true or false depending on whether the row is selected. Each cell renders with a gridcell role.

Use the following aria attributes on lightning-datatable to provide a caption or description on your table for assistive technologies. These attributes are rendered on the <table> element.

AttributeTypeDescription
aria-labelstringProvides an assistive label to identify a table from other tables on a page.
aria-describedbyID reference listSpecifies the ID or space-separated list of IDs that describes the current element.
aria-labelledbyID reference listSpecifies the ID or space-separated list of IDs of the element or elements that contain visible descriptive text to caption or describe the table.

When using aria-label, consider using aria-describedby to provide supplemental information. We don't recommend using aria-label together with aria-labelledby. When there's visible text that labels an element, consider using aria-labelledby instead of aria-label.

If you use aria-describedby, the element that provides the description doesn't need to be visible. For more information, see MDN web docs: aria-describedby.

Use the aria-label attribute to provide a more descriptive label for the datatable for assistive technology. The aria-label attribute and its value are passed down to the rendered table element. On pages with multiple tables, aria-label helps users identify which table they're accessing.

Set the attribute in lightning-datatable in your template.

Change the ARIA label dynamically using the ariaLabel property.

The aria-label attribute doesn't support empty strings. If you set aria-label="" in the HTML or .ariaLabel = "" in JavaScript, the table's aria-label attribute is hidden, not rendered with an empty string. An empty label string can confuse screen readers.

If you have descriptions on an element or on multiple elements for the table, set the aria-labelledby value with the ID or list of IDs of the elements.

lightning-datatable generates a unique string for the element and aria-labelledby value to prevent any naming conflicts with other <table> elements on the page. Always verify that your rendered table correctly matches the IDs of the descriptive text in the DOM with a screen reader like JAWS or VoiceOver.

You can use data tables in navigation mode and action mode using the keyboard. To enter navigation mode, tab into the data table, which triggers focus on the first data cell in the table body on initial load. Use the arrow keys to move around the table. If you use the arrow key to move to another cell and then click away from the datatable, tab back into the datatable to place focus on the last cell you were on.

To enter action mode, press Enter or the Spacebar. You can navigate to each actionable element in the table using the Tab key.

When focus is on a cell that contains a link, press Enter to activate the link. If the link is a URL, the browser directs you to the website. If the link is a phone number or email, your browser prompts you to open the appropriate app for the link.

When using custom types, add additional attributes to make all elements of the component accessible. To make elements focusable, pass tabindex={internalTabIndex} all the way down to every component or element in the custom type. All focusable elements also must include data-navigation="enable". If you don't want the element to be focusable, use tabindex=-1. For more information, see Datatable Accessibility in the Lightning Web Components Developer Guide.

Columns can be resized in action mode. First, navigate to the column header using the arrow keys. Then, press the Tab key to activate the column divider to resize a column. You can resize a column by increasing or decreasing its width using one of the following key combinations.

  • Right and Left Arrow keys
  • Up and Down Arrow keys
  • Page Up and Page Down keys

When you resize a column, the new column width is announced by assistive technology. To finish resizing the column and return to navigation mode, press the Esc key.

To programmatically apply focus to the active cell, use the focus() method. For example, you can click a button to move focus back to the last active cell on the datatable.

To query your datatable, use the lwc:ref directive. This example uses lwc:ref="dt" where dt is the unique reference. Call the reference using this.refs.dt.

This component has usage differences from its Aura counterpart. See Base Components: Aura Vs Lightning Web Components in the Lightning Web Components Developer Guide.

cancel

The event fired when the cancel button is pressed during inline editing.

The cancel event does not return any parameter.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelabletrueThis event can be canceled. You can call preventDefault() on this event.
composedfalseThis event does not propagate outside the template in which it was dispatched.

headeraction

The event fired when a header action is selected, such as text wrapping, text clipping, or a custom header action.

The headeraction event returns the following parameters.

ParameterTypeDescription
actionobjectThe action definition described in Creating Header Actions.
columnDefinitionobjectThe column definition specified in the columns property, for example, the key-value pairs for label, fieldName, type, typeAttributes, and wrapText. See Working with Column Properties.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelablefalseThis event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composedfalseThis event does not propagate outside the template in which it was dispatched.

loadmore

The event fired when you scroll to the bottom of the table to load more data, until there are no more data to load.

The loadmore event returns the following parameters.

ParameterTypeDescription
enableInfiniteLoadingbooleanSpecifies whether infite loading is available on the table.
isLoadingbooleanSpecifies that data is loading and displays a spinner on the table.
loadMoreOffsetintegerThe number of pixels between the bottom of the table and the current scroll position, used to trigger more data loading.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelablefalseThis event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composedfalseThis event does not propagate outside the template in which it was dispatched.

resize

The event fired when the a table column is resized, which depends on which width mode you're using.

In the default fixed width mode, the resize event is fired when:

  • The table renders initially
  • You manually resize a column
  • The number of columns changes on a subsequent rerender

In auto width mode, the event is fired only when you manually resize a column. See Implementing Fixed Width Mode and Implementing Auto Width Mode for more information.

The resize event is not fired in the following cases.

  • You resize the browser or viewport, causing adjustments in the column widths
  • You change the column definition, but not the number of columns, causing adjustments in the column widths.

The resize event returns the following parameters.

ParameterTypeDescription
columnWidthsobjectThe width of all columns, in pixels. For example, a table with 5 columns of 205px width each at initial render returns [205, 205, 205, 205, 205].
isUserTriggeredbooleanSpecifies whether the column resize is caused by a user action.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelablefalseThis event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composedfalseThis event does not propagate outside the template in which it was dispatched.

rowselection

The event fired when the row is selected.

The rowselection event returns the following parameter.

ParameterTypeDescription
selectedRowsobjectThe data in the rows that are selected.
config.actionstringThe type of selection event. Valid options include 'selectAllRows', 'deselectAllRows', 'rowSelect', and 'rowDeselect'.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelablefalseThis event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composedfalseThis event does not propagate outside the template in which it was dispatched.

save

The event fired when data is saved during inline editing.

The save event returns the following parameter.

ParameterTypeDescription
draftValuesobjectThe current value that's provided during inline editing.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelablefalseThis event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composedfalseThis event does not propagate outside the template in which it was dispatched.

sort

The event fired when a column is sorted.

The sort event returns the following parameters.

ParameterTypeDescription
fieldNamestringThe fieldName that controls the sorting.
isMultiColumnSortbooleanReserved for internal use. Defaults to false.
sortDirectionstringThe sorting direction. Valid options include 'asc' and 'desc'.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThis event does not bubble.
cancelablefalseThis event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composedfalseThis event does not propagate outside the template in which it was dispatched.

The LWC Recipes GitHub repository contains code examples for Lightning Web Components that you can test in an org.

For a recipe that uses lightning-datatable, see the c-datatable-* components in the LWC Recipes repo.