datatable improvements (#1822)

- move ngSwitch outside of the ngFor directive (to avoid issues)
- improved template maintainance (without ngFor template hack)
- readme updates to reflect new features
This commit is contained in:
Denys Vuika
2017-04-19 15:41:47 +01:00
committed by Mario Romano
parent 6c2e56296f
commit 87b479e720
3 changed files with 94 additions and 22 deletions

View File

@@ -27,12 +27,21 @@
title="{{'DOCUMENT_LIST.COLUMNS.DISPLAY_NAME' | translate}}" title="{{'DOCUMENT_LIST.COLUMNS.DISPLAY_NAME' | translate}}"
key="name" key="name"
class="full-width ellipsis-cell"> class="full-width ellipsis-cell">
<!-- Example of using custom column template -->
<!-- Example #1: using custom template with implicit access to data context -->
<!-- <!--
<template let-entry="$implicit"> <template let-entry="$implicit">
<span>Hi! {{entry.data.getValue(entry.row, entry.col)}}</span> <span>Hi! {{entry.data.getValue(entry.row, entry.col)}}</span>
</template> </template>
--> -->
<!-- Example #2: using custom template with value access -->
<!--
<template let-value="value">
<span>Hi! {{value}}</span>
</template>
-->
</data-column> </data-column>
<data-column <data-column
title="{{'DOCUMENT_LIST.COLUMNS.TAG' | translate}}" title="{{'DOCUMENT_LIST.COLUMNS.TAG' | translate}}"

View File

@@ -190,8 +190,20 @@ export class DataTableDemo {
this.data = new ObjectDataTableAdapter( this.data = new ObjectDataTableAdapter(
// data // 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'} 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'
}
] ]
); );
} }
@@ -295,6 +307,52 @@ onRowClick(event) {
</alfresco-datatable> </alfresco-datatable>
``` ```
#### Column Templates
It is possible assigning a custom column template like the following:
```html
<alfresco-datatable ...>
<data-columns>
<data-column title="Version" key="properties.cm:versionLabel">
<template let-value="value">
<span>V. {{value}}</span>
</template>
</data-column>
</data-columns>
</alfresco-datatable>
```
Example above shows access to the underlying cell value by binding `value` property to the underlying context `value`:
```html
<template let-value="value">
```
Alternatively you can get access to the entire data context using the following syntax:
```html
<template let-entry="$implicit">
```
That means you are going to create local variable `entry` that is bound to the data context via Angular's special `$implicit` keyword.
```html
<template let-entry="$implicit">
<span>V. {{entry.data.getValue(entry.row, entry.col)}}</span>
</template>
```
In the second case `entry` variable is holding a reference to the following data context:
```ts
{
data: DataTableAdapter,
row: DataRow,
col: DataColumn
}
```
#### rowClick event #### rowClick event
_This event is emitted when user clicks the row._ _This event is emitted when user clicks the row._

View File

@@ -53,33 +53,38 @@
<td *ngIf="multiselect"> <td *ngIf="multiselect">
<md-checkbox [(ngModel)]="row.isSelected"></md-checkbox> <md-checkbox [(ngModel)]="row.isSelected"></md-checkbox>
</td> </td>
<td *ngFor="let col of data.getColumns()" [ngSwitch]="col.type" <td *ngFor="let col of data.getColumns()"
class="mdl-data-table__cell--non-numeric non-selectable data-cell {{col.cssClass}}" class="mdl-data-table__cell--non-numeric non-selectable data-cell {{col.cssClass}}"
(click)="onRowClick(row, $event)" (click)="onRowClick(row, $event)"
(dblclick)="onRowDblClick(row, $event)" (dblclick)="onRowDblClick(row, $event)"
[context-menu]="getContextMenuActions(row, col)" [context-menu]="getContextMenuActions(row, col)"
[context-menu-enabled]="contextMenu"> [context-menu-enabled]="contextMenu">
<div *ngIf="!col.template" class="cell-container"> <div *ngIf="!col.template" class="cell-container">
<div *ngSwitchCase="'image'" class="cell-value"> <ng-container [ngSwitch]="col.type">
<i *ngIf="isIconValue(row, col)" class="material-icons icon-cell">{{asIconValue(row, col)}}</i> <div *ngSwitchCase="'image'" class="cell-value">
<img *ngIf="!isIconValue(row, col)" <i *ngIf="isIconValue(row, col)" class="material-icons icon-cell">{{asIconValue(row, col)}}</i>
class="image-cell" <img *ngIf="!isIconValue(row, col)"
alt="{{iconAltTextKey(data.getValue(row, col))|translate}}" class="image-cell"
src="{{data.getValue(row, col)}}" alt="{{iconAltTextKey(data.getValue(row, col))|translate}}"
(error)="onImageLoadingError($event)"> src="{{data.getValue(row, col)}}"
</div> (error)="onImageLoadingError($event)">
<div *ngSwitchCase="'date'" class="cell-value" [attr.data-automation-id]="'date_' + data.getValue(row, col)"> </div>
{{data.getValue(row, col)}} <div *ngSwitchCase="'date'" class="cell-value" [attr.data-automation-id]="'date_' + data.getValue(row, col)">
</div> {{data.getValue(row, col)}}
<div *ngSwitchCase="'text'" class="cell-value" [attr.data-automation-id]="'text_' + data.getValue(row, col)"> </div>
{{data.getValue(row, col)}} <div *ngSwitchCase="'text'" class="cell-value" [attr.data-automation-id]="'text_' + data.getValue(row, col)">
</div> {{data.getValue(row, col)}}
<span *ngSwitchDefault class="cell-value"> </div>
<!-- empty cell for unknown column type --> <span *ngSwitchDefault class="cell-value">
</span> <!-- empty cell for unknown column type -->
</span>
</ng-container>
</div> </div>
<div *ngIf="col.template" class="cell-container"> <div *ngIf="col.template" class="cell-container">
<template ngFor [ngForOf]="[{ data: data, row: row, col: col }]" [ngForTemplate]="col.template"></template> <ng-container
[ngTemplateOutlet]="col.template"
[ngOutletContext]="{ $implicit: { data: data, row: row, col: col }, value: data.getValue(row, col) }">
</ng-container>
</div> </div>
</td> </td>