--- Title: DataTable component Added: v2.0.0 Status: Active Last reviewed: 2019-03-20 --- # [DataTable component](../../../lib/core/datatable/components/datatable/datatable.component.ts "Defined in datatable.component.ts") Displays data as a table with customizable columns and presentation. ![DataTable demo](../../docassets/images/datatable-demo.png) See it live: [DataTable Quickstart](https://embed.plnkr.co/80qr4YFBeHjLMdAV0F6l/) ## Contents - [Basic usage](#basic-usage) - [Setting the rows and column schema](#setting-the-rows-and-column-schema) - [Transclusions](#transclusions) - [Class members](#class-members) - [Properties](#properties) - [Events](#events) - [Details](#details) - [Supplying data for the table](#supplying-data-for-the-table) - [Customizing columns](#customizing-columns) - [DataTable DOM Events](#datatable-dom-events) - [Card view](#card-view) - [Using events](#using-events) - [Customizing the component's styles](#customizing-the-components-styles) - [See also](#see-also) ## Basic usage **app.component.html** ```html ``` **app.component.ts** ```ts import { ObjectDataTableAdapter } from '@alfresco/adf-core'; @Component({...}) export class DataTableDemo { data: ObjectDataTableAdapter; constructor() { this.data = new ObjectDataTableAdapter( // data [ {id: 1, name: 'Name 1'}, {id: 2, name: 'Name 2'} ], // schema [ { type: 'text', key: 'id', title: 'Id', sortable: true }, { type: 'text', key: 'name', title: 'Name', cssClass: 'full-width', sortable: true } ] ); } } ``` ### Setting the rows and column schema You can set rows and columns in the [`ObjectDataTableAdapter`](../../../lib/core/datatable/data/object-datatable-adapter.ts) as shown below: ```ts import { ObjectDataTableAdapter } from '@alfresco/adf-core'; @Component({...}) export class DataTableDemo { data: ObjectDataTableAdapter; constructor() { this.data = new ObjectDataTableAdapter( // data [ {id: 1, name: 'Name 1'}, {id: 2, name: 'Name 2'} ], // columns [ { type: 'text', key: 'id', title: 'Id', sortable: true }, { type: 'text', key: 'name', title: 'Name', cssClass: 'full-width', sortable: true } ] ); } } ``` ```html ``` You can also set rows and use an HTML-based schema declaration as shown below: ```ts import { ObjectDataTableAdapter } from '@alfresco/adf-core'; @Component({...}) export class DataTableDemo { data: ObjectDataTableAdapter; constructor() { this.data = new ObjectDataTableAdapter( // data [ {id: 1, name: 'Name 1'}, {id: 2, name: 'Name 2'} ], [] ); } } ``` ```html ``` You can also set rows in the [`ObjectDataTableAdapter`](../../../lib/core/datatable/data/object-datatable-adapter.ts) and set columns as an input as shown below : ```ts import { ObjectDataTableAdapter } from '@alfresco/adf-core'; @Component({...}) export class DataTableDemo { data: ObjectDataTableAdapter; schema: any; constructor() { this.data = new ObjectDataTableAdapter( // data [ {id: 1, name: 'Name 1'}, {id: 2, name: 'Name 2'} ], [] ); // columns this.schema = [ { type: 'text', key: 'id', title: 'Id', sortable: true }, { type: 'text', key: 'name', title: 'Name', sortable: true } ]; } } ``` ```html ``` You can also set rows and columns through inputs as shown below : ```ts import { ObjectDataTableAdapter } from '@alfresco/adf-core'; @Component({...}) export class DataTableDemo { rows: any; schema: any; constructor() { // data this.rows = [ {id: 1, name: 'Name 1'}, {id: 2, name: 'Name 2'} ]; // columns this.schema = [ { type: 'text', key: 'id', title: 'Id', sortable: true }, { type: 'text', key: 'name', title: 'Name', sortable: true } ]; } } ``` ```html ``` ### [Transclusions](../../user-guide/transclusion.md) You can add [Data column component](data-column.component.md) instances to define columns for the table as described in the usage examples and the [Customizing columns](#customizing-columns) section. You can also supply a `` or an [Empty list component](empty-list.component.md) sub-component to show when the table is empty: ```html

Sorry, no content

``` ```html "'My custom Header'" "'My custom body'" "'My custom footer'" "'HTML Layout'" ``` Another useful transclusion is the ``, which is shown while the data for the table is loading: ```html ``` ```js isLoading(): boolean { //your custom logic to identify if you are in a loading state } ``` Note that you can use both the `` and the `` together in the same datatable. ## Class members ### Properties | Name | Type | Default value | Description | | ---- | ---- | ------------- | ----------- | | actions | `boolean` | false | Toggles the data actions column. | | actionsPosition | `string` | "right" | Position of the actions dropdown menu. Can be "left" or "right". | | allowDropFiles | `boolean` | false | Toggles file drop support for rows (see [Upload directive](../directives/upload.directive.md) for further details). | | columns | `any[]` | \[] | The columns that the datatable will show. | | contextMenu | `boolean` | false | Toggles custom context menu for the component. | | data | [`DataTableAdapter`](../../../lib/core/datatable/data/datatable-adapter.ts) | | Data source for the table | | display | `string` | DisplayMode.List | Selects the display mode of the table. Can be "list" or "gallery". | | fallbackThumbnail | `string` | | Fallback image for rows where the thumbnail is missing. | | loading | `boolean` | false | Flag that indicates if the datatable is in loading state and needs to show the loading template (see the docs to learn how to configure a loading template). | | multiselect | `boolean` | false | Toggles multiple row selection, which renders checkboxes at the beginning of each row. | | noPermission | `boolean` | false | Flag that indicates if the datatable should show the "no permission" template. | | rowMenuCacheEnabled | `boolean` | true | Should the items for the row actions menu be cached for reuse after they are loaded the first time? | | rowStyle | `string` | | The inline style to apply to every row. See [NgStyle](https://angular.io/docs/ts/latest/api/common/index/NgStyle-directive.html) docs for more details and usage examples. | | rowStyleClass | `string` | "" | The CSS class to apply to every row. | | rows | `any[]` | \[] | The rows that the datatable will show. | | selectionMode | `string` | "single" | Row selection mode. Can be none, `single` or `multiple`. For `multiple` mode, you can use Cmd (macOS) or Ctrl (Win) modifier key to toggle selection for multiple rows. | | showHeader | `boolean` | true | Toggles the header. | | sorting | `any[]` | \[] | Define the sort order of the datatable. Possible values are : [`created`, `desc`], [`created`, `asc`], [`due`, `desc`], [`due`, `asc`] | | stickyHeader | `boolean` | false | Toggles the sticky header mode. | ### Events | Name | Type | Description | | ---- | ---- | ----------- | | executeRowAction | [`EventEmitter`](https://angular.io/api/core/EventEmitter)`<`[`DataRowActionEvent`](../../../lib/core/datatable/components/datatable/data-row-action.event.ts)`>` | Emitted when the user executes a row action. | | rowClick | [`EventEmitter`](https://angular.io/api/core/EventEmitter)`<`[`DataRowEvent`](../../../lib/core/datatable/data/data-row-event.model.ts)`>` | Emitted when the user clicks a row. | | rowDblClick | [`EventEmitter`](https://angular.io/api/core/EventEmitter)`<`[`DataRowEvent`](../../../lib/core/datatable/data/data-row-event.model.ts)`>` | Emitted when the user double-clicks a row. | | showRowActionsMenu | [`EventEmitter`](https://angular.io/api/core/EventEmitter)`<`[`DataCellEvent`](../../../lib/core/datatable/components/datatable/data-cell.event.ts)`>` | Emitted before the actions menu is displayed for a row. | | showRowContextMenu | [`EventEmitter`](https://angular.io/api/core/EventEmitter)`<`[`DataCellEvent`](../../../lib/core/datatable/components/datatable/data-cell.event.ts)`>` | Emitted before the context menu is displayed for a row. | ## Details ### Supplying data for the table The column layout and row data are supplied to the table using an object that implements the [`DataTableAdapter`](../../../lib/core/datatable/data/datatable-adapter.ts) interface. This interface hides the internal details of the class that provides the data, which gives a lot of flexibility in how the data can be stored and accessed. The DataTable library includes a standard adapter class called [`ObjectDataTableAdapter`](../../../lib/core/datatable/data/object-datatable-adapter.ts) that is useful in many common cases. See the [`DataTableAdapter`](../../../lib/core/datatable/data/datatable-adapter.ts) for full details about the interface and the [`ObjectDataTableAdapter`](../../../lib/core/datatable/data/object-datatable-adapter.ts) class. ### Customizing columns You can define custom HTML templates for columns and also add tooltips, automatic column title translation and other features. See the [Data Column component](data-column.component.md) page for more information. ### DataTable DOM Events Below are the DOM events emitted by the DataTable component. These events bubble up the component tree and can be handled by any parent component. | Name | Description | | ---- | ----------- | | row-click | Raised when user clicks a row | | row-dblclick | Raised when user double-clicks a row | | row-select | Raised after user selects a row | | row-unselect | Raised after user unselects a row | | row-keyup | Raised on the 'keyup' event for the focused row. | | sorting-changed | Raised after user clicks the sortable column header. | For example: ```html ``` ```ts onRowClick(event) { console.log(event); } ``` ![](../../docassets/images/datatable-dom-events.png) ### Card view Set the `display` property to "gallery" to enable Card View mode: ```html ``` ```ts import { DataCellEvent, DataRowActionEvent } from '@alfresco/adf-core'; onShowRowActionsMenu(event: DataCellEvent) { let myAction = { title: 'Hello' // your custom metadata needed for onExecuteRowAction }; event.value.actions = [ myAction ]; } onExecuteRowAction(event: DataRowActionEvent) { let args = event.value; console.log(args.row); console.log(args.action); window.alert(`My custom action: ${args.action.title}`); } ``` ![](../../docassets/images/datatable-actions-ui.png) ![](../../docassets/images/datatable-actions-console.png) You can use any payloads for row actions. The only requirement for the objects is that they must have a `title` property. When an action is selected in the dropdown menu, the DataTable invokes the `executeRowAction` event. Use this to handle the response, inspect the action payload (and all custom properties defined earlier), and perform the corresponding actions. ### Customizing the component's styles The datatable component is ready to be used out of the box but you can customize the styling to suit your needs as described in the following sections. #### Truncated text By default, the content of the cells is wrapped so you can see all the data inside, as shown below: ![](../../docassets/images/datatable-wrapped-text.png) However, you can also truncate the text within these cells using the `adf-ellipsis-cell` class in the desired column: ```js { type: 'text', key: 'createdOn', title: 'Created On', sortable: true, cssClass: 'adf-ellipsis-cell' } ``` ![](../../docassets/images/datatable-truncated-text.png) #### Expanded cells This component uses a flexible layout. By default, all cells are assumed to have the same semantic importance, so all of them have the same width. We provide a predefined set of CSS classes that you can use to vary the relative widths of the columns. The classes have names of the form `adf-expand-cell-X`, going from `adf-expand-cell-1` to `adf-expand-cell-5`. The higher the number in the class the wider the column will be. Use these classes to vary the column widths according to your needs: ![](../../docassets/images/datatable-expand-5.png) #### Combining classes You can combine the CSS classes described above to customize the table as needed: ```js { type: 'text', key: 'name', title: 'Name', cssClass: 'adf-ellipsis-cell adf-expand-cell-3' } ``` #### Sticky header If you have a long table with many rows, you might want to fix the header in place so it is always visible. You can do this using the following steps. First, set the `stickyHeader` property of your datatable to `true`: ```html ``` Then, set the height of your datatable in its parent's container so that the datatable adapts to it (i.e. `height: 300px;` in the example below). You must also set the parent's [`overflow-y`](https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-y) attribute to `auto`. This will allow the list to scroll when the total height of all rows exceeds the fixed height of the parent element. ```html
``` Once set up, the sticky header behaves as shown in the image below: ![](../../docassets/images/datatable-sticky-header.png) ## See also - [Data column component](data-column.component.md) - [Pagination component](pagination.component.md) - [Data Table Adapter interface](../interfaces/datatable-adapter.interface.md) - [Document list component](../../content-services/components/document-list.component.md)