# DataTable (fw-data-table)
fw-data-table are used for data visualization.
# Basic Usage
# Custom cells
# Crayons provided variants
# Anchor column variant
Row value for this column variant should be an object with the following properties:
- text - Text to be displayed in the cell
- href - url to point to when the text is clicked
# User column variant
Row value for this column variant should be an object with the following properties:
- name - Name of the user
- email - email of the user
- image (optional) - url of the user image to be displayed in the avatar
If image property is not present, user's initials from the name property will be shown inside the avatar.
# Icon column variant
# Paragraph column variant
We can use this column variant when we have a bigger text and we need to trim/show this text. Row value for this column variant should be an object with the following properties:
- text: Paragraph to trim. Only first three lines from this paragraph would be visible initially. User has to expand to see the full text.
# Custom templates
This codeblock shows how to use custom cell function to display HTML content in a cell.
To have custom table headers, we can use 'customHeader' property. This will take in a function with parameters same as customTemplate.
var columns = [{
"key": "bookname",
"text": "Book name",
"customTemplate": (createElement, props) => {
return createElement('b', {}, props.text);
}
}]
// Usage examples for createElement:
// Params should be either (tagName, children) or (tagName, properties, children)
createElement('div', [createElement('h2', 'Hello')]);
createElement('div#foo.bar.baz', [createElement('h2', 'Hello')]);
createElement('div.bar.baz', [createElement('h2', 'Hello')]);
createElement('div', {className: 'greeting'}, [createElement('h2', 'Hello')]);
# Column text alignment
You can set text alignment in the column by passing the textAlign in column configuration. In the below example, column 'Icon' is center aligned.
# Row Actions:
You can easily add an actions column by passing in rowActions prop to the component.
You can also use icons instead of text in buttons. Pass 'iconName' and 'iconLibrary' properties as part of configuration.
# Hide columns
To hide certain columns, we can pass the 'hide' property set to true in the column's configuration.
# Column lock
We can lock column using 'lock' in column's configuration.
# Column width
We can pass width for every column using 'widthProperties' in column's configuration. Every column has a minimum width of 40px and maximum width of 1000px by default. We can override min/max width for every column using the 'widthProperties' too.
# Formatting data
We can format row's data before rendering into a cell by passing 'formatData' in column's configuration.
This option wont work when using this with 'variant' or 'customTemplate' properties in column's configuration.
# Table settings
Table settings help with reordering and hide/show of columns. To enable table settings, pass the 'show-settings' prop to the table.
# Loading table
We can load a table using the 'loadTable' method available on the table.
# Saving column configuration
For auto saving configuration into localStorage, you can add 'autoSaveSettings' prop to the table.
<data-table id="data-table-10" label="data table 10" auto-save-settings="true">
</data-table>
Data table exposes couple of method to get and set column configuration.
let dataTable = document.querySelector('data-table#config');
// getColumnConfig helps retrive configuration in JSON format
let dataTableConfiguration = dataTable.getTableSettings();
// setColumnConfig helps set the configuration.
dataTable.setTableSettings(dataTableConfiguration);
# Properties
Property | Attribute | Description | Type | Default |
---|---|---|---|---|
autoSaveSettings | auto-save-settings | autoSaveSettings to enable auto saving of table settings to localstorage . If set to true , make sure id attribute is also set to the data-table | boolean | false |
columns | -- | Columns Array of objects that provides information regarding the columns in the table. | DataTableColumn[] | [] |
isAllSelectable | is-all-selectable | isAllSelectable Boolean based on which select all option appears in the table header | boolean | false |
isLoading | is-loading | To disable table during async operations | boolean | false |
isSelectable | is-selectable | isSelectable Boolean based on which selectable options appears for rows in the table. | boolean | false |
label | label | Label attribute is not visible on screen. There for accessibility purposes. | string | '' |
rowActions | -- | To enable bulk actions on the table. | DataTableAction[] | [] |
rows | -- | Rows Array of objects to be displayed in the table. | DataTableRow[] | [] |
shimmerCount | shimmer-count | shimmerCount number of shimmer rows to show during initial loading | number | 4 |
showSettings | show-settings | showSettings is used to show the settings button on the table. | boolean | false |
# Events
Event | Description | Type |
---|---|---|
fwSelectAllChange | fwSelectAllChange Emits this event when select all is checked. | CustomEvent<any> |
fwSelectionChange | fwSelectionChange Emits this event when row is selected/unselected. | CustomEvent<any> |
# Methods
# getSelectedIds() => Promise<string[]>
getSelectedIds
# Returns
Type: Promise<string[]>
an array of selected row IDs
# getSelectedRows() => Promise<DataTableRow[]>
getSelectedRows
# Returns
Type: Promise<DataTableRow[]>
selected rows from the data table
# getTableSettings() => Promise<{}>
getTableSettings
# Returns
Type: Promise<{}>
columnConfig object
# loadTable(state?: boolean) => Promise<boolean>
loadTable - Method to call when we want to change table loading state
# Returns
Type: Promise<boolean>
isLoading current state
# selectAllRows(checked?: boolean) => Promise<string[]>
selectAllRows method we can use to select/unselect rows in the table
# Returns
Type: Promise<string[]>
# setTableSettings(columnConfig: any) => Promise<DataTableColumn[]>
setTableSettings
# Returns
Type: Promise<DataTableColumn[]>
# Dependencies
# Depends on
- fw-custom-cell-anchor
- fw-custom-cell-user
- fw-custom-cell-icon
- fw-custom-cell-paragraph
- fw-checkbox
- fw-tooltip
- fw-button
- fw-icon
- fw-input
- fw-drag-container
- fw-skeleton
# Graph
graph TD;
fw-data-table --> fw-custom-cell-anchor
fw-data-table --> fw-custom-cell-user
fw-data-table --> fw-custom-cell-icon
fw-data-table --> fw-custom-cell-paragraph
fw-data-table --> fw-checkbox
fw-data-table --> fw-tooltip
fw-data-table --> fw-button
fw-data-table --> fw-icon
fw-data-table --> fw-input
fw-data-table --> fw-drag-container
fw-data-table --> fw-skeleton
fw-custom-cell-user --> fw-avatar
fw-custom-cell-icon --> fw-icon
fw-icon --> fw-toast-message
fw-toast-message --> fw-spinner
fw-toast-message --> fw-icon
fw-custom-cell-paragraph --> fw-tooltip
fw-tooltip --> fw-popover
fw-checkbox --> fw-icon
fw-button --> fw-spinner
fw-button --> fw-icon
fw-input --> fw-icon
style fw-data-table fill:#f9f,stroke:#333,stroke-width:4px
Built with ❤ at Freshworks