# DataTable Component for Angular 2

travis
    Status travis
    Status Coverage Status npm downloads license alfresco component angular 2 typescript node version

See it live: [DataTable Quickstart](https://embed.plnkr.co/80qr4YFBeHjLMdAV0F6l/) ## Prerequisites Before you start using this development framework, make sure you have installed all required software and done all the necessary configuration, see this [page](https://github.com/Alfresco/alfresco-ng2-components/blob/master/PREREQUISITES.md). ## Install Follow the 3 steps below: 1. Npm ```sh npm install ng2-alfresco-datatable --save ``` 2. Html Include these dependencies in your index.html page: ```html ``` 3. SystemJs Add the following components to your systemjs.config.js file: - ng2-translate - alfresco-js-api - ng2-alfresco-core - ng2-alfresco-datatable Please refer to the following example file: [systemjs.config.js](demo/systemjs.config.js) . ## Basic usage **my.component.ts** ```ts import { NgModule, Component } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { CoreModule } from 'ng2-alfresco-core'; import { DataTableModule, ObjectDataTableAdapter } from 'ng2-alfresco-datatable'; @Component({ selector: 'alfresco-app-demo', template: ` ` }) 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 } ] ); } } @NgModule({ imports: [ BrowserModule, CoreModule.forRoot(), DataTableModule.forRoot() ], declarations: [DataTableDemo], bootstrap: [DataTableDemo] }) export class AppModule {} platformBrowserDynamic().bootstrapModule(AppModule); ``` ![DataTable demo](docs/assets/datatable-demo.png) You can also use HTML-based schema declaration like shown below: ```ts import { NgModule, Component } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { CoreModule } from 'ng2-alfresco-core'; import { DataTableModule, ObjectDataTableAdapter } from 'ng2-alfresco-datatable'; @Component({ selector: 'alfresco-app-demo', template: ` ` }) export class DataTableDemo { data: ObjectDataTableAdapter; constructor() { this.data = new ObjectDataTableAdapter( // data [ { id: 1, name: 'Name 1', createdBy : { name: 'user'}, createdOn: 123, icon: 'http://example.com/img.png' }, { id: 2, name: 'Name 2', createdBy : { name: 'user 2'}, createdOn: 123, icon: 'http://example.com/img.png' } ] ); } } @NgModule({ imports: [ BrowserModule, CoreModule.forRoot(), DataTableModule.forRoot() ], declarations: [DataTableDemo], bootstrap: [DataTableDemo] }) export class AppModule {} platformBrowserDynamic().bootstrapModule(AppModule); ``` ### DataTable Properties | Name | Type | Default | Description | | --- | --- | --- | --- | | `data` | DataTableAdapter | instance of **ObjectDataTableAdapter** | data source | | `rows` | Object[] | [] | The rows that the datatable should show | | `multiselect` | boolean | false | Toggles multiple row selection, renders checkboxes at the beginning of each row | | `actions` | boolean | false | Toggles data actions column | | `actionsPosition` | string (left\|right) | right | Position of the actions dropdown menu. | | `fallbackThumbnail` | string | | Fallback image for row ehre thubnail is missing| | `contextMenu` | boolean | false | Toggles custom context menu for the component | | `allowDropFiles` | boolean | false | Toggle file drop support for rows (see **ng2-alfresco-core/UploadDirective** for more details) | ### DataColumn Properties Here's the list of available properties you can define for a Data Column definition. | Name | Type | Default | Description | | --- | --- | --- | --- | | key | string | | Data source key, can be either column/property key like `title` or property path like `createdBy.name` | | type | string (text\|image\|date) | text | Value type | | format | string | | Value format (if supported by components), for example format of the date | | sortable | boolean | true | Toggles ability to sort by this column, for example by clicking the column header | | title | string | | Display title of the column, typically used for column headers | | template | `TemplateRef` | | Custom column template | | sr-title | string | | Screen reader title, used for accessibility purposes | | class | string | | Additional CSS class to be applied to column (header and cells) | ### DataTable Events | Name | Description | --- | --- | | [rowClick](#rowclick-event) | Emitted when user clicks the row | | [rowDblClick](#rowdblclick-event) | Emitted when user double-clicks the row | | [showRowContextMenu](#showrowcontextmenu-event) | Emitted before context menu is displayed for a row | | [showRowActionsMenu](#showrowactionsmenu-event) | Emitted before actions menu is displayed for a row | | [executeRowAction](#executerowaction-event) | Emitted when row action is executed by user | ### DataTable DOM Events Below are the DOM events raised by DataTable component. | Name | Description | | --- | --- | | row-click | Emitted when user clicks the row | | row-dblclick | Emitted when user double-clicks the row | These events are bubbled up the element tree and can be subscribed to from within parent components. ```html ``` ```ts onRowClick(event) { console.log(event); } ``` ![](docs/assets/datatable-dom-events.png) ### Advanced usage ```html ``` #### Column Templates It is possible assigning a custom column template like the following: ```html ``` Example above shows access to the underlying cell value by binding `value` property to the underlying context `value`: ```html