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
Appending an SLDS icon to column data
Hiding the table header
Hiding the table borders
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.
Managing Selected Rows
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.
1getSelectedEmail(event){2 // Display the Contact of the selected rows3 event.detail.selectedRows.forEach((selectedRow)=>{4 alert('Selected email addresses: ' + selectedRow.Contact);5});6}
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.
1selectedData = [];2currentlySelectedData = [];34handleRowSelection(event){5 switch(event.detail.config.action){6 case 'selectAllRows':7 for(let i = 0; i<event.detail.selectedRows.length; i++){8 selectedData.push(event.detail.selectedRows[i]);9 currentlySelectedData.push(event.detail.selectedRows[i]);10}11 break;12 case 'deselectAllRows':13 currentlySelectedData = [];14 break;15 case 'rowSelect':16 selectedData.push(event.detail.config.value);17 break;18 case 'rowDeselect':19 index = currentlySelectedData.indexOf(event.detail.config.value);20 if(index !== -1){21 array.splice(index, 1);22}23 break;24 default:25 break;26}27}
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.
1import{LightningElement}from "lwc";23// Define columns (COLS) here4// Define or retrieve data (DATA) here56export default class DatatableExample extends LightningElement{7 data = DATA;8 columns = COLS;9 selected;1011 handleGetSelected(){12 this.selected = this.refs.dt.getSelectedRows();13}1415 get selectedStr(){16 return this.selected ? JSON.stringify(this.selected) : "";17}18}
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.
1public with sharing class ContactController{23 @AuraEnabled(cacheable=true)4 public static List<Contact>getContactList(){5 return[SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact LIMIT 10];6}7}
The template wires up the datatable to the contact records using the id field.
Use the following column properties to customize the behavior and visual
aspects of your columns.
Property
Type
Description
actions
object
Appends a dropdown menu of actions to a column. You must pass in a list of label-name pairs.
cellAttributes
object
Provides 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.
editable
boolean
Specifies whether a column supports inline editing. The default is false.
fieldName
string
Required. The name that binds the columns attributes to the associated data. Each columns attribute must correspond to an item in the data array.
fixedWidth
integer
Specifies the width of a column in pixels and makes the column non-resizable. If both fixedWidth and initialWidth values are provided, initialWidth is ignored.
hideDefaultActions
boolean
Specifies whether to hide the default header actions on a column. The default is false. For more information, see Creating Header Actions.
The 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.
imgSrc
string
The 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.
initialWidth
integer
The 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.
label
string
Required. 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.
sortable
boolean
Specifies whether the column can be sorted. The default is false.
type
string
Required. The data type to be used for data formatting. For more information, see Formatting with Data Types.
typeAttributes
object
Provides 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.
wrapText
boolean
Specifies 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.
Formatting with Data Types
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:
Type
Description
Supported Type Attributes
action
Displays 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)
boolean
Displays the icon utility if the value is true, and a blank value otherwise.
Displays 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.
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.
1get columns(){2 return[3 // Column using 'fieldName' as identifier4{5 label: 'Lead',6 fieldName: 'leadId',7 ...8},9 // Column using 'columnKey' as identifier10{11 label: 'Company',12 columnKey: 'leadCompany',13 fieldName: 'leadId',14 ...15}16]17}
Custom Formatting Examples
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.
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.
Displaying Date and Time Using Type Attributes
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 Type
Datatable Data Type
Description
DateTime
date
Expects date and time as input, and formats it according to the user's locale.
Date
date-local
Expects 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.
The locale set in your Salesforce preferences determines the formatting.
For more information, see the lightning-formatted-date-time documentation.
Aligning Content in A Column
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.
Displaying an Icon Instead of a Column Label
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.
Appending an Icon to Column Data
To append an icon to your data output, use cellAttributes and pass in these
attributes.
Attribute
Description
iconName
Required. The Lightning Design System name of the icon, for example, utility.
iconLabel
The label for the icon to be displayed on the right of the icon.
iconPosition
The position of the icon relative to the data. Valid options include left and right. This value defaults to left.
iconAlternativeText
Descriptive 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.
Displaying Indicators for Read-Only Fields
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.
1import{LightningElement, api}from "lwc";23export default class DatatableExample extends LightningElement{4 data = [];5 columns = columnsDefs;6 loadMoreStatus;7 @api totalNumberOfRows;89 loadMoreData(event){10 //Display a spinner to signal that data is being loaded11 event.target.isLoading = true;12 //Display "Loading" when more data is being loaded13 this.loadMoreStatus = "Loading";14 fetchData(50).then((data)=>{15 if(data.length>= this.totalNumberOfRows){16 event.target.enableInfiniteLoading = false;17 this.loadMoreStatus = "No more data to load";18}else{19 const currentData = this.data;20 //Appends new data to the end of the table21 const newData = currentData.concat(data);22 this.data = newData;23 this.loadMoreStatus = "";24}25 event.target.isLoading = false;26});27}28}
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.
1SELECT COUNT(Id) FROM Contact
Scrolling Back to the Top
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.
1import{LightningElement}from "lwc";23const columns = [4 // column data here5];67export default class DatatableScrollExample extends LightningElement{8 data = [];9 columns = columns;1011 handleScroll(){12 this.refs.datatable.scrollToTop();13}14}
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.
Creating Header Actions
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 Name
Description
Wrap text
Expands 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 text
Truncates 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.
Attribute
Description
label
Required. The label that's displayed for the action.
name
Required. The name of the action, which identifies the selected action.
checked
Specifies 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.
disabled
Specifies whether the action can be selected. If true, the action item is shown as disabled. This value defaults to false.
iconName
The 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.
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.
Attribute
Description
label
Required. The label that's displayed for the action.
name
Required. The name of the action, which identifies the selected action.
disabled
Specifies whether the action can be selected. If true, the action item is shown as disabled. This value defaults to false.
iconName
The 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).
1import{LightningElement}from 'lwc';23const actions = [4{label: 'Show details', name: 'show_details'},5{label: 'Delete', name: 'delete'}6];78const columns = [9 // Your column data10];1112export default class DatatableExample extends LightningElement{13 data = [];14 columns = columns;1516 constructor(){17 super();18 this.columns = [19 // Other column data here20{type: 'action', typeAttributes:{rowActions: this.getRowActions}},21]22}2324 getRowActions(row, doneCallback){25 const actions = [];26 if(row['isActive']){27 actions.push({28 'label': 'Deactivate',29 'iconName': 'utility:block_visitor',30 'name': 'deactivate'31});32}else{33 actions.push({34 'label': 'Activate',35 'iconName': 'utility:adduser',36 'name': 'activate'37});38}39 // simulate a trip to the server40 setTimeout(()=>{41 doneCallback(actions);42}), 200);43}44}
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.
Resizing Tables and Columns
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.
1<div style="height: 300px;">2<!-- lightning-datatable goes here -->3</div>
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.
Working with Column Widths
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.
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
Implementing Fixed Width Mode
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.
Implementing Auto Width Mode
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.
Specifying Maximum Number of Selected Rows
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".
To select a row programmatically, pass in the row key-field value.
1// Load data via init handler first2 // then handle programmatic selection3 handleSelect(){4 const rows = ['a'];5 this.selectedRows = rows;6}
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.
Disabling Rows Programmatically
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.
Sorting Data By Column
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.
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.
Working with Inline Editing
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.
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.
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.
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.
Display Errors on a Row
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.
1triggerError(event){2 this.errors = {3 rows:{4 1:{5 title: 'We found 2 errors.',6 messages:[7 'Enter a valid amount.',8 'Verify the email address and try again.'9],10 fieldNames:['amount', 'contact']11}12},13};14}
Display Custom Error Messages for Cells
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:
1triggerError(event){2 this.errors = {3 rows:{4 1:{5 title: 'We found 2 errors.',6 messages:[7 'Enter a valid amount.',8 'Verify the email address and try again.'9],10 fieldNames:{11 'amount': "Enter an amount greater than 100"12 'contact': "Specify a valid domain name"13}1415}16},17};18}
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.
Display Errors for the Table
To display error messages at the bottom of the table, use the table property in the error object like this:
1triggerError(event){2 this.errors = {3 table:{4 title: 'Your entry cannot be saved. Fix the errors and try again.',5 messages:[6 'Row 1 amount must be number',7 'Row 1 email is invalid'8]9}10};11}
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.
1triggerError(event){2 this.errors = {3 rows:{4 1:{5 title: 'We found 2 errors.',6 messages:[7 'Enter a valid amount.',8 'Verify the email address and try again.'9],10 fieldNames:{11 'amount': "Enter an amount greater than 100"12 'contact': "Specify a valid domain name"13}14}15},16 table:{17 title: 'Your entry cannot be saved. Fix the errors and try again.',18 messages:[19 'Row 1 amount must be number',20 'Row 1 email is invalid'21]22}23}24},25};26}
Text Wrapping and Clipping
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.
Text Wrapping in Column Headers
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.
Accessibility
lightning-datatable renders with a grid role and a polite live region that announces whether the table is in navigation mode or action mode. To toggle action mode, tab into the table and then press the Enter key or Space Bar. To toggle back to navigation mode, press the Esc key to return focus to the cell. For more information about the different modes, see Toggle Between Navigation and Action Modes.
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.
Attribute
Type
Description
aria-label
string
Provides an assistive label to identify a table from other tables on a page.
aria-describedby
ID reference list
Specifies the ID or space-separated list of IDs that describes the current element.
aria-labelledby
ID reference list
Specifies 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.
Provide an Accessible Label for the Table
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.
1<lightning-datatable aria-label="Account Details by Year for EMEA Region">2</lightning-datatable>
Change the ARIA label dynamically using the ariaLabel property.
1const myLightningDataTableElement = this.template.querySelector("lightning-datatable");2myLightningDataTableElement.ariaLabel = "Account Details by Revenue for EMEA Region";
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.
Provide an Accessible Caption for the Table
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.
Toggle Between Navigation and Action Modes
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.
Resize Columns Using Arrow Keys
Columns can be resized in action mode. The component announces the column width during a resize. 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.
Apply Focus on an Active Cell
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.
1import{LightningElement}from "lwc";23// Define columns (COLS) here4// Define or retrieve data (DATA) here56export default class DatatableExample extends LightningElement{7 data = DATA;8 columns = COLS;9 selected;1011 handleFocus(){12 this.refs.dt.focus();13}14}
The 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.
Property
Value
Description
bubbles
false
This event does not bubble.
cancelable
false
This event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composed
false
This 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.
Parameter
Type
Description
enableInfiniteLoading
boolean
Specifies whether infite loading is available on the table.
isLoading
boolean
Specifies that data is loading and displays a spinner on the table.
loadMoreOffset
integer
The 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.
Property
Value
Description
bubbles
false
This event does not bubble.
cancelable
false
This event has no default behavior that can be canceled. You cannot call preventDefault() on this event.
composed
false
This 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
For a recipe that uses lightning-datatable, see the c-datatable-* components in the LWC Recipes repo.
Attributes
Name
Description
Type
Default
Required
aria-label
Public property for passing `aria-label` down to the child table element.
aria-labelled-by
Public property for passing `aria-labelledby` down to the child table element.
columns
Array of the columns object that's used to define the data types. Required properties include 'label', 'fieldName', and 'type'. The default type is 'text'. See the Documentation tab for more information.
Array
column-widths-mode
Specifies how column widths are calculated. Set to 'fixed' for columns with equal widths. Set to 'auto' for column widths that are based on the width of the column content and the table width. The default is 'fixed'.
String
fixed
data
The array of data to be displayed.
Array
default-sort-direction
Specifies the default sorting direction on an unsorted column. Valid options include 'asc' and 'desc'. The default is 'asc' for sorting in ascending order.
String
asc
disabled-rows
Enables programmatic row disabling with a list of key-field values.
list
draft-values
The current values per row that are provided during inline edit.
Object
enable-infinite-loading
If present, you can load a subset of data and then display more when users scroll to the end of the table. Use with the onloadmore event handler to retrieve more data.
Boolean
false
errors
Specifies an object containing information about cell level, row level, and table level errors. When it's set, error messages are displayed on the table accordingly.
Object
hide-borders
If present, the table borders are hidden. Only valid when hide-table-header is true.
Boolean
false
hide-checkbox-column
If present, the checkbox or radio button column for row selection is hidden.
Boolean
false
hide-table-header
If present, the table header is hidden.
Boolean
false
is-loading
If present, a spinner is shown to indicate that more data is loading.
Boolean
false
key-field
Required for better performance. Associates each row with a unique ID. key-field is case sensitive and must match the value you provide in the data array.
String
load-more-offset
Determines when to trigger infinite loading based on how many pixels the table's scroll position is from the bottom of the table. The default is 20.
Number
20
max-column-width
The maximum width for all columns. The default is 1000px.
Number
1000px
max-edit-limit
Reserved for internal use.
max-row-selection
The maximum number of rows that can be selected. Value should be a positive integer Checkboxes are used for selection by default, and radio buttons are used when maxRowSelection is 1 unless overridden using the singleRowSelectionMode property.
Number
min-column-width
The minimum width for all columns. The default is 50px.
Number
50px
render-config
Reserved for internal use.
render-mode
The `role-based` option renders <div> and is reserved for internal use. / /** Opts-in to a more performant 'inline' table. Valid options are 'default' and 'inline'. 'default' renders the traditional DOM structure. 'inline' renders a simplified DOM structure that improves performance but may break some custom styling.
resize-column-disabled
If present, column resizing is disabled.
Boolean
false
resize-step
The width to resize the column when a user presses left or right arrow. The default is 10px.
Number
10px
row-number-offset
Determines where to start counting the row number. The default is 0.
Number
0
row-toggle-icon
Reserved for internal use.
Object
selected-rows
Enables programmatic row selection with a list of key-field values.
list
show-actions-menu
If present, the actions menu is displayed to enable users to do advanced sorting.
Boolean
false
show-row-number-column
If present, the row numbers are shown in the first column.
Boolean
false
single-row-selection-mode
Specifies whether to render checkboxes instead of radio buttons. Use with max-row-selection. When max-row-selection is 1, radio buttons are used for selection by default. Valid values are 'radio' and 'checkbox'. The default value is 'radio'.
String
sorted-by
The column key or fieldName(s) that controls the sorting order. Sort the data using the onsort event handler.
String|String[]
sorted-direction
Specifies the sorting direction. Sort the data using the onsort event handler. Valid options include a single value of 'asc' or 'desc' or an array of such values.
String|String[]
suppress-bottom-bar
If present, the footer that displays the Save and Cancel buttons is hidden during inline editing.
Boolean
false
wrap-table-header
Specifies how the table header is wrapped. Set to 'all' to wrap all column headers. Set to 'none' to clip all column headers. Set to 'by-column' to wrap/clip column headers based on the wrap/clip setting for that individual column. The default is 'none'.
String
none
wrap-text-max-lines
This value specifies the number of lines after which the content will be cut off and hidden. It must be at least 1 or more. The text in the last line is truncated and shown with an ellipsis.
Integer
Methods
Name
Description
Argument Name
Argument Type
Argument Description
focus
Focuses the current active cell in the datatable.
getSelectedRows
Returns data in each selected row.
openInlineEdit
Opens the inline edit panel for the datatable's currently active cell. If the active cell is not editable, then the panel is instead opened for the first editable cell in the table. Given two distinct cells, C_x and C_y, C_x is considered "first" in the cell ordering if the following condition evaluates to true: (C_x.rowIndex < C_y.rowIndex) || (C_x.rowIndex === C_y.rowIndex && C_x.columnIndex < C_y.columnIndex) If there is no data in the table or there are no editable cells in the table then calling this function results in a no-op.