Table

API for table

API reference for Angular CDK table

import {CdkTableModule} from '@angular/cdk/table';

A data table that can render a header row, data rows, and a footer row. Uses the dataSource input to determine the data to be rendered. The data can be provided either as a data array, an Observable stream that emits the data array to render, or a DataSource with a connect function that will return an Observable stream that emits the data array to render.

Selector: cdk-table table[cdk-table]

Exported as: cdkTable
Properties
Name Description
@Input()

dataSource: CdkTableDataSourceInput<T>

The table's source of data, which can be provided in three ways (in order of complexity):

  • Simple data array (each object represents one table row)
  • Stream that emits a data array each time the array changes
  • DataSource object that implements the connect/disconnect interface.

If a data array is provided, the table must be notified when the array's objects are added, removed, or moved. This can be done by calling the renderRows() function which will render the diff since the last table render. If the data array reference is changed, the table will automatically trigger an update to the rows.

When providing an Observable stream, the table will trigger an update automatically when the stream emits a new array of data.

Finally, when providing a DataSource object, the table will use the Observable stream provided by the connect function and trigger updates when that stream emits new data array values. During the table's ngOnDestroy or when the data source is removed from the table, the table will call the DataSource's disconnect function (may be useful for cleaning up any subscriptions registered during the connect process).

@Input()

multiTemplateDataRows: boolean

Whether to allow multiple rows per data object by evaluating which rows evaluate their 'when' predicate to true. If multiTemplateDataRows is false, which is the default value, then each dataobject will render the first row that evaluates its when predicate to true, in the order defined in the table, or otherwise the default row which does not have a when predicate.

@Input()

trackBy: TrackByFunction<T>

Tracking function that will be used to check the differences in data changes. Used similarly to ngFor trackBy function. Optimize row operations by identifying a row based on its data relative to the function to know if a row should be added/removed/moved. Accepts a function that takes two parameters, index and item.

stickyCssClass: string

CSS class added to any row or cell that has sticky positioning applied. May be overriden by table subclasses.

Methods
addColumnDef

Adds a column definition that was not included as part of the content children.

Parameters

columnDef

CdkColumnDef

addFooterRowDef

Adds a footer row definition that was not included as part of the content children.

Parameters

footerRowDef

CdkFooterRowDef

addHeaderRowDef

Adds a header row definition that was not included as part of the content children.

Parameters

headerRowDef

CdkHeaderRowDef

addRowDef

Adds a row definition that was not included as part of the content children.

Parameters

rowDef

CdkRowDef<T>

removeColumnDef

Removes a column definition that was not included as part of the content children.

Parameters

columnDef

CdkColumnDef

removeFooterRowDef

Removes a footer row definition that was not included as part of the content children.

Parameters

footerRowDef

CdkFooterRowDef

removeHeaderRowDef

Removes a header row definition that was not included as part of the content children.

Parameters

headerRowDef

CdkHeaderRowDef

removeRowDef

Removes a row definition that was not included as part of the content children.

Parameters

rowDef

CdkRowDef<T>

renderRows

Renders rows based on the table's latest set of data, which was either provided directly as an input or retrieved through an Observable stream (directly or from a DataSource). Checks for differences in the data since the last diff to perform only the necessary changes (add/remove/move rows).

If the table's data source is a DataSource or Observable, this will be invoked automatically each time the provided Observable stream emits a new data array. Otherwise if your data is an array, this function will need to be called to render any changes.

updateStickyColumnStyles

Updates the column sticky styles. First resets all applied styles with respect to the cells sticking to the left and right. Then sticky styles are added for the left and right according to the column definitions for each cell in each row. This is automatically called when the data source provides a new set of data or when a column definition changes its sticky input. May be called manually for cases where the cell content changes outside of these events.

updateStickyFooterRowStyles

Updates the footer sticky styles. First resets all applied styles with respect to the cells sticking to the bottom. Then, evaluating which cells need to be stuck to the bottom. This is automatically called when the footer row changes its displayed set of columns, or if its sticky input changes. May be called manually for cases where the cell content changes outside of these events.

updateStickyHeaderRowStyles

Updates the header sticky styles. First resets all applied styles with respect to the cells sticking to the top. Then, evaluating which cells need to be stuck to the top. This is automatically called when the header row changes its displayed set of columns, or if its sticky input changes. May be called manually for cases where the cell content changes outside of these events.

Cell definition for a CDK table. Captures the template of a column's data row cell as well as cell-specific properties.

Selector: [cdkCellDef]

Header cell definition for a CDK table. Captures the template of a column's header cell and as well as cell-specific properties.

Selector: [cdkHeaderCellDef]

Footer cell definition for a CDK table. Captures the template of a column's footer cell and as well as cell-specific properties.

Selector: [cdkFooterCellDef]

Column definition for the CDK table. Defines a set of cells available for a table column.

Selector: [cdkColumnDef]

Properties
Name Description
@Input('cdkColumnDef')

name: string

Unique name for this column.

@Input()

sticky: boolean

Whether sticky positioning should be applied.

@Input('stickyEnd')

stickyEnd: boolean

Whether this column should be sticky positioned on the end of the row. Should make sure that it mimics the CanStick mixin such that _hasStickyChanged is set to true if the value has been changed.

cssClassFriendlyName: string

Transformed version of the column name that can be used as part of a CSS classname. Excludes all non-alphanumeric characters and the special characters '-' and '_'. Any characters that do not match are replaced by the '-' character.

Methods
hasStickyChanged

Whether the sticky value has changed since this was last called.

Returns
boolean

resetStickyChanged

Resets the dirty check for cases where the sticky state has been used without checking.

Header cell template container that adds the right classes and role.

Selector: cdk-header-cell th[cdk-header-cell]

Footer cell template container that adds the right classes and role.

Selector: cdk-footer-cell td[cdk-footer-cell]

Cell template container that adds the right classes and role.

Selector: cdk-cell td[cdk-cell]

Header row definition for the CDK table. Captures the header row's template and other header properties such as the columns to display.

Selector: [cdkHeaderRowDef]

Properties
Name Description
@Input( cdkHeaderRowDefSticky)

sticky: boolean

Whether sticky positioning should be applied.

Methods
hasStickyChanged

Whether the sticky value has changed since this was last called.

Returns
boolean

resetStickyChanged

Resets the dirty check for cases where the sticky state has been used without checking.

Footer row definition for the CDK table. Captures the footer row's template and other footer properties such as the columns to display.

Selector: [cdkFooterRowDef]

Properties
Name Description
@Input( cdkFooterRowDefSticky)

sticky: boolean

Whether sticky positioning should be applied.

Methods
hasStickyChanged

Whether the sticky value has changed since this was last called.

Returns
boolean

resetStickyChanged

Resets the dirty check for cases where the sticky state has been used without checking.

Data row definition for the CDK table. Captures the header row's template and other row properties such as the columns to display and a when predicate that describes when this row should be used.

Selector: [cdkRowDef]

Properties
Name Description
@Input( cdkRowDefColumns)

columns: Iterable<string>

The columns to be displayed on this row.

@Input( cdkRowDefWhen)

when: (index: number, rowData: T) => boolean

Function that should return true if this row template should be used for the provided index and row data. If left undefined, this row will be considered the default row template to use when no other when functions return true for the data. For every row, there must be at least one when function that passes or an undefined to default.

Methods
extractCellTemplate

Gets this row def's relevant cell template from the provided column def.

Parameters

column

CdkColumnDef

Returns
TemplateRef<any>

getColumnsDiff

Returns the difference between the current columns and the columns from the last diff, or null if there is no difference.

Returns
IterableChanges<any> | null

Header template container that contains the cell outlet. Adds the right class and role.

Selector: cdk-header-row tr[cdk-header-row]

Footer template container that contains the cell outlet. Adds the right class and role.

Selector: cdk-footer-row tr[cdk-footer-row]

Data row template container that contains the cell outlet. Adds the right class and role.

Selector: cdk-row tr[cdk-row]

Base class for the cells. Adds a CSS classname that identifies the column it renders in.

Base class for the CdkHeaderRowDef and CdkRowDef that handles checking their columns inputs for changes and notifying the table.

Properties
Name Description

columns: Iterable<string>

The columns to be displayed on this row.

Methods
extractCellTemplate

Gets this row def's relevant cell template from the provided column def.

Parameters

column

CdkColumnDef

Returns
TemplateRef<any>

getColumnsDiff

Returns the difference between the current columns and the columns from the last diff, or null if there is no difference.

Returns
IterableChanges<any> | null

Interface used to provide an outlet for rows to be inserted into.

Properties
Name Description

viewContainer: ViewContainerRef

Base interface for a cell definition. Captures a column's cell template definition.

Properties
Name Description

template: TemplateRef<any>

Context provided to the row cells when multiTemplateDataRows is false

Properties
Name Description

$implicit: T

Data for the row that this cell is located within.

count: number

Length of the number of total rows.

even: boolean

True if this cell is contained in a row with an even-numbered index.

first: boolean

True if this cell is contained in the first row.

index: number

Index of the data object in the provided data array.

last: boolean

True if this cell is contained in the last row.

odd: boolean

True if this cell is contained in a row with an odd-numbered index.

Context provided to the row cells when multiTemplateDataRows is true. This context is the same as CdkCellOutletRowContext except that the single index value is replaced by dataIndex and renderIndex.

Properties
Name Description

$implicit: T

Data for the row that this cell is located within.

count: number

Length of the number of total rows.

dataIndex: number

Index of the data object in the provided data array.

even: boolean

True if this cell is contained in a row with an even-numbered index.

first: boolean

True if this cell is contained in the first row.

last: boolean

True if this cell is contained in the last row.

odd: boolean

True if this cell is contained in a row with an odd-numbered index.

renderIndex: number

Index location of the rendered row that this cell is located within.

type StickyDirection = 'top' | 'bottom' | 'left' | 'right';

The row template that can be used by the mat-table. Should not be used outside of the material library.

const CDK_ROW_TEMPLATE: "<ng-container cdkCellOutlet></ng-container>";