The Material Components Web data table component
npm install @limetech/mdc-data-tableData tables display information in a grid-like format of rows and columns. They organize information in a way that's
easy to scan, so that users can look for patterns and insights.
```
npm install @limetech/mdc-data-table
`scss`
@import "@limetech/mdc-checkbox/mdc-checkbox"; // Required only for data table with row selection.
@import "@limetech/mdc-data-table/mdc-data-table";
> NOTE: Styles for any components you intend to include within data-table (e.g. Checkboxes, Buttons etc.) must be
> imported.
`js`
import {MDCDataTable} from '@limetech/mdc-data-table';
const dataTable = new MDCDataTable(document.querySelector('.mdc-data-table'));
> See Importing the JS component for more information on how to import JavaScript.
> Instantiating MDCDataTable component is only required to add interactions for example, row selection.
MDC Data Table component auto instantiates MDCCheckbox for header row checkbox and all row checkboxes. Make sure to set required class names to instantiate checkbox component. We suggest to use layout API when rows are added or removed from data table to register new checkbox components.
`html`
Dessert
Carbs (g)
Protein (g)
Comments
Frozen yogurt
24
4.0
Super tasty
Ice cream sandwich
37
4.33333333333
I like ice cream more
Eclair
24
6.0
New filing flavor
Variants
`html`
Status
Signal name
Severity
Stage
Time
Roles
Online
Arcus watch slowdown
Medium
Triaged
0:33
Allison Brie
Offline
monarch: prod shared ares-managed-features-provider-heavy
Huge
Triaged
0:33
Brie Larson
Online
monarch: prod shared ares-managed-features-provider-heavy
Minor
Not triaged
0:33
Jeremy Lake
Online
Arcus watch slowdown
Negligible
Triaged
0:33
Angelina Cheng
CSS Class | Description
--- | ---
mdc-data-table | Mandatory. The root DOM element containing table and other supporting elements.mdc-data-table__table | Mandatory. Table element. Added to table HTML tag.mdc-data-table__header-row | Mandatory. Table header row element. Added to thead > tr HTML tag.mdc-data-table__header-cell | Mandatory. Table header cell element. Added to thead > th > td HTML tag.mdc-data-table__header-cell--checkbox | Optional. Table header cell element that contains mdc-checkbox. Added to thead> th > td:first-child HTML tag.mdc-data-table__header-cell--numeric | Optional. Table header cell element that maps to numeric cells. Added to thead > th > td HTML tag.mdc-data-table__content | Mandatory. Table body element. Added to tbody HTML tag.mdc-data-table__row | Mandatory. Table row element. Added to tbody > tr HTML tag.mdc-data-table__cell | Mandatory. Table cell element. Added to tbody > tr > td HTML tag.mdc-data-table__cell--numeric | Optional. Table cell element that contains numeric data. Added to tbody > tr > td HTML tag.mdc-data-table__cell--checkbox | Optional. Table cell element that contains mdc-checkbox. Added to thead> th > td:first-child HTML tag.mdc-data-table__header-row-checkbox | Optional. Checkbox element rendered inside table header row element. Add this class name to mdc-checkbox element to override styles required for data-table.mdc-data-table__row-checkbox | Optional. Checkbox element rendered inside table row element. Add this class name to mdc-checkbox element to override styles required for data-table.mdc-data-table__row--selected | Optional. Modifier class added to mdc-data-table__row when table row is selected.
Mixin | Description
--- | ---
mdc-data-table-fill-color($color) | Sets the background color of data-table surface.mdc-data-table-row-fill-color($color) | Sets the background color of table row container.mdc-data-table-header-row-fill-color($color) | Sets the background color of table header row container.mdc-data-table-selected-row-fill-color($color) | Sets the background color of selected row container.mdc-data-table-checked-icon-color($color) | Sets the checked icon color.mdc-data-table-divider-color($color) | Sets the table rows divider color.mdc-data-table-divider-size($size) | Sets the table rows divider size.mdc-data-table-row-hover-fill-color($color) | Sets the background color of table row on hover.mdc-data-table-header-row-text-color($color) | Sets the header row text color.mdc-data-table-row-text-color($color) | Sets the row text color.mdc-data-table-shape-radius($radius) | Sets the rounded shape with given radius size. $radius can be single radius or list radius values up to 4 list size.mdc-data-table-stroke-size($size) | Sets the border size of data-table.mdc-data-table-stroke-color($color) | Sets the border color of data-table.mdc-data-table-header-cell-height($height) | Sets table header cell height.mdc-data-table-cell-height($height) | Sets table cell height.mdc-data-table-cell-padding($leading-padding, $trailing-padding) | Sets leading & trailing padding for all cells.mdc-data-table-column-widths($width-list) | Sets the custom widths for each table column.mdc-data-table-density($density-scale) | Sets density scale to data table. Supported density scale values -4, -3, -2, -1, 0. Use corresponding density mixins of child components (such as Checkbox) to apply density scales which will be rendered inside data table as content.
Please refer WAI-ARIA Authoring Practices for table for ARIA recommended role, states & properties required for table element.
Please use MDCDataTable's constants file to access these event constants.
`tsevents.ROW_SELECTION_CHANGED
const {events} from '@limetech/mdc-data-table/constants';
// to access event constant.`
Event constant | Event name | Description
-- | -- | --
ROW_SELECTION_CHANGED | MDCDataTable:changed | Event emitted when row checkbox is checked or unchecked.SELECTED_ALL | MDCDataTable:selectedAll | Event emitted when header row checkbox is checked.UNSELECTED_ALL | MDCDataTable:unselectedAll | Event emitted when header row checkbox is unchecked.
Properties and MethodsMethod Signature | Description
--- | ---
layout() => void | Registers new row checkboxes, header row checkbox and updates the state of header row checkbox. Use this when rows are added / removed from data table.getRows() => HTMLElement[] | Returns array of row elements.getSelectedRowIds() => Array | Returns array of selected row ids.setSelectedRowIds(rowIds: string[]) | Sets selected row ids. Overwrites previously selected rows.
If you are using a JavaScript framework, such as React or Angular, you can create a Data Table for your framework. Depending on your needs, you can use the _Simple Approach: Wrapping MDC Web Vanilla Components_, or the _Advanced Approach: Using Foundations and Adapters_. Please follow the instructions here.
Method Signature | Description
--- | ---
addClassAtRowIndex(rowIndex: number, cssClasses: string) => void | Adds a class name to row element at given row index excluding header row.getRowCount() => number | Returns row count excluding header row.getRowElements() => HTMLElement[] | Returns array of row elements excluding header row.getRowIdAtIndex(rowIndex: number) => string \| null | Returns row id of row element at given row index based on data-row-id attribute on row element tr.getRowIndexByChildElement(el: Element) => number | Returns index of row element that contains give child element.getSelectedRowCount() => number | Returns selected row count.isCheckboxAtRowIndexChecked(rowIndex: number) => boolean; | Returns True if row checkbox at given row index is checked.isHeaderRowCheckboxChecked() => boolean | Returns true if header row checkbox is checked.isRowsSelectable() => boolean | Returns true if table rows are selectable.notifyRowSelectionChanged(data: MDCDataTableRowSelectionChangedEventDetail) => void | Notifies when row selection is changed.notifySelectedAll() => void | Notifies when header row is checked.notifyUnselectedAll() => void | Notifies when header row is unchecked.registerHeaderRowCheckbox() => Promise | Initializes header row checkbox. Destroys previous header row checkbox instance if any. Can return Promise only if registering checkbox is asynchronous.registerRowCheckboxes() => Promise | Initializes all row checkboxes. Destroys all previous row checkbox instances if any. This is usually called when row checkboxes are added or removed from table. Can return Promise only if registering checkbox is asynchronous.removeClassAtRowIndex(rowIndex: number, cssClasses: string) => void | Removes class name from row element at give row index.setAttributeAtRowIndex(rowIndex: number, attr: string, value: string) => void | Sets attribute to row element at given row index.setHeaderRowCheckboxChecked(checked: boolean) => void | Sets header row checkbox checked or unchecked.setHeaderRowCheckboxIndeterminate(indeterminate: boolean) => void | Sets header row checkbox to indeterminate.setRowCheckboxCheckedAtIndex(rowIndex: number, checked: boolean) => void | Sets row checkbox to checked or unchecked at given row index.
Method Signature | Description
--- | ---
layout() => void | Re-initializes header row checkbox and row checkboxes when selectable rows are added or removed from table. Use this if registering checkbox is synchronous.layoutAsync() => Promise | Re-initializes header row checkbox and row checkboxes when selectable rows are added or removed from table. Use this only if registerRowCheckboxes and registerHeaderRowCheckboxe are asynchronous.getRows() => HTMLElement[] | Returns array of row elements.setSelectedRowIds(rowIds: string[]) => void | Sets selected row ids. Overwrites previously selected rows.getSelectedRowIds() => Array | Returns array of selected row ids.handleHeaderRowCheckboxChange() => void | Handles header row checkbox change event.handleRowCheckboxChange(event: Event) => void` | Handles change event originated from row checkboxes.