td-data-table
element generates a data driven table layout sorting events.
Use [tdDataTableTemplate] directive for template support which gives context access to [value], [row] and [column].
Import the [CovalentDataTableModule] in your NgModule:
import { CovalentDataTableModule } from '@covalent/core/data-table'; @NgModule({ imports: [ CovalentDataTableModule, ... ], ... }) export class MyModule {}
The ITdDataTableColumn
interface for columns has the following contract:
export interface ITdDataTableColumn { name: string; label: string; tooltip?: string; // used to add a tooltip into the column header numeric?: boolean; // used to right align elements if they are numeric format?: (value: any) => any; // used to format the cell values nested?: boolean; // when this is true, the `.` characters will be taken as key separators for nested values sortable?: boolean; // used to make a particular column sortable hidden?: boolean; // used to hide the column in the data table on the fly filter?: boolean; width?: ITdDataTableColumnWidth | number; // used to configure the widths of the columns, if omitted it will fill the rest of the space } export interface ITdDataTableColumnWidth { min?: number; max?: number; }
Example for HTML usage:
<td-data-table [data]="data" [columns]="columns" [selectable]="true|false" [multiple]="true|false" [sortable]="true|false" [sortBy]="sortBy" [sortOrder]="'ASC'|'DESC'" [compareWith]="compareWith" (sortChange)="sortEvent($event)" (rowSelect)="selectEvent($event)" (selectAll)="selectAllEvent($event)"> <ng-template tdDataTableTemplate="columnName" let-value="value" let-row="row" let-column="column"> <div layout="row"> <span flex>{{value}}</span> // or <span flex>{{row[column]}}</span> <mat-icon>star</mat-icon> </div> </ng-template> </td-data-table>
import { ITdDataTableColumn } from '@covalent/core/data-table'; ... }) export class Demo { private data: any[] = [ { sku: '1452-2', item: 'Pork Chops', price: 32.11 }, { sku: '1421-0', item: 'Prime Rib', price: 41.15 }, ]; private columns: ITdDataTableColumn[] = [ { name: 'sku', label: 'SKU #', tooltip: 'Stock Keeping Unit', sortable: true }, { name: 'item', label: 'Item name', width: 200 }, { name: 'price', label: 'Price (US$)', numeric: true, format: v => v.toFixed(2), width: { min: 100, max: 400 } }, ]; compareWith(row: any: model: any): boolean { return row.id === model.id; // or any property you want to compare by. } }