1.9.0 • Published 6 years ago

ng2-alfresco-datatable v1.9.0

Weekly downloads
17
License
Apache-2.0
Repository
github
Last release
6 years ago

DataTable Component

See it live: DataTable Quickstart

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.

If you plan using this component with projects generated by Angular CLI, please refer to the following article: Using ADF with Angular CLI

Install

npm install ng2-alfresco-datatable

Basic usage

app.component.html

<alfresco-datatable 
    [data]="data">
</alfresco-datatable>

app.component.ts

import { ObjectDataTableAdapter }  from 'ng2-alfresco-datatable';

@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
                }
            ]
        );
    }
}

DataTable demo

You can also use HTML-based schema declaration like shown below:

<alfresco-datatable [data]="data">
    <data-columns>
        <data-column key="icon" type="image" [sortable]="false"></data-column>
        <data-column key="id" title="Id"></data-column>
        <data-column key="createdOn" title="Created"></data-column>
        <data-column key="name" title="Name" class="full-width name-column"></data-column>
        <data-column key="createdBy.name" title="Created By"></data-column>
    </data-columns>
</alfresco-datatable>
import { ObjectDataTableAdapter } from 'ng2-alfresco-datatable';

@Component({...})
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'
                }
            ]
        );
    }
}

DataTable Properties

NameTypeDefaultDescription
selectionModestring'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.
rowStylestringThe inline style to apply to every row, see NgStyle docs for more details and usage examples
rowStyleClassstringThe CSS class to apply to every row
dataDataTableAdapterinstance of ObjectDataTableAdapterdata source
rowsObject[][]The rows that the datatable should show
multiselectbooleanfalseToggles multiple row selection, renders checkboxes at the beginning of each row
actionsbooleanfalseToggles data actions column
actionsPositionstring (left|right)rightPosition of the actions dropdown menu.
fallbackThumbnailstringFallback image for row ehre thubnail is missing
contextMenubooleanfalseToggles custom context menu for the component
allowDropFilesbooleanfalseToggle file drop support for rows (see ng2-alfresco-core/UploadDirective for more details)
loadingbooleanfalseFlag that indicate if the datable is in loading state and need to show the loading template. Read the documentation above to know how to configure a loading template

DataColumn Properties

Here's the list of available properties you can define for a Data Column definition.

NameTypeDefaultDescription
keystringData source key, can be either column/property key like title or property path like createdBy.name
typestring (text|image|date)textValue type
formatstringValue format (if supported by components), for example format of the date
sortablebooleantrueToggles ability to sort by this column, for example by clicking the column header
titlestringDisplay title of the column, typically used for column headers
templateTemplateRefCustom column template
sr-titlestringScreen reader title, used for accessibility purposes
classstringAdditional CSS class to be applied to column (header and cells)

DataTable Events

NameDescription
rowClickEmitted when user clicks the row
rowDblClickEmitted when user double-clicks the row
showRowContextMenuEmitted before context menu is displayed for a row
showRowActionsMenuEmitted before actions menu is displayed for a row
executeRowActionEmitted when row action is executed by user

DataTable DOM Events

Below are the DOM events raised by DataTable component.

NameDescription
row-clickEmitted when user clicks the row
row-dblclickEmitted when user double-clicks the row

These events are bubbled up the element tree and can be subscribed to from within parent components.

<root-component (row-click)="onRowClick($event)">
    <child-component>
        <alfresco-datatable></alfresco-datatable>
    </child-component>
</root-component>
onRowClick(event) {
    console.log(event);
}

npm.io

Empty content template

You can add a template that will be showed when there are no result in your datatable:

<alfresco-datatable
    [data]="data"
    [actions]="contentActions"
    [multiselect]="multiselect"
    (showRowContextMenu)="onShowRowContextMenu($event)"
    (showRowActionsMenu)="onShowRowActionsMenu($event)"
    (executeRowAction)="onExecuteRowAction($event)"
    (rowClick)="onRowClick($event)"
    (rowDblClick)="onRowDblClick($event)">
    
    
        <no-content-template>
            <!--Add your custom empty template here-->
            <ng-template>
                <h1>Sorry, no content</h1>
            </ng-template>
        </no-content-template>
        
</alfresco-datatable>

Loading content template

You can add a template that will be showed during the loading of your data:

<alfresco-datatable
    [data]="data"
    [actions]="contentActions"
    [multiselect]="multiselect"
    [loading]=isLoading()"
    (showRowContextMenu)="onShowRowContextMenu($event)"
    (showRowActionsMenu)="onShowRowActionsMenu($event)"
    (executeRowAction)="onExecuteRowAction($event)"
    (rowClick)="onRowClick($event)"
    (rowDblClick)="onRowDblClick($event)">
    
        <loading-content-template>
            <ng-template>
               <!--Add your custom loading template here-->
                <md-progress-spinner
                    class="adf-document-list-loading-margin"
                    [color]="'primary'"
                    [mode]="'indeterminate'">
                </md-progress-spinner>
            </ng-template>
        </loading-content-template>
        
</alfresco-datatable>
    isLoading(): boolean {
        //your custom logic to identify if you are in a loading state 
    }

Note: the <loading-content-template> and <no-content-template> can be used together

Column Templates

It is possible assigning a custom column template like the following:

<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:

<template let-value="value">

Alternatively you can get access to the entire data context using the following syntax:

<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.

<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:

{
    data: DataTableAdapter,
    row: DataRow,
    col: DataColumn
}

Events

rowClick event

This event is emitted when user clicks the row.

Event properties:

sender: any     // DataTable instance 
value: DataRow, // row clicked
event: Event    // original HTML DOM event

Handler example:

onRowClicked(event: DataRowEvent) {
    console.log(event.value);
}

This event is cancellable, you can use event.preventDefault() to prevent default behaviour.

rowDblClick event

This event is emitted when user double-clicks the row.

Event properties:

sender: any     // DataTable instance 
value: DataRow, // row clicked
event: Event    // original HTML DOM event

Handler example:

onRowDblClicked(event: DataRowEvent) {
    console.log(event.value);
}

This event is cancellable, you can use event.preventDefault() to prevent default behaviour.

showRowContextMenu event

Emitted before context menu is displayed for a row.

Note that DataTable itself does not populate context menu items, you can provide all necessary content via handler.

Event properties:

value: {
    row: DataRow,
    col: DataColumn,
    actions: []
}

Handler example:

onShowRowContextMenu(event: DataCellEvent) {
    event.value.actions = [
        { ... },
        { ... }
    ]
}

This event is cancellable, you can use event.preventDefault() to prevent default behaviour.

DataTable will automatically render provided menu items.

Please refer to ContextMenu documentation for more details on context actions format and behavior.

showRowActionsMenu event

Emitted before actions menu is displayed for a row. Requires actions property to be set to true.

Event properties:

value: {
    row: DataRow,
    action: any
}

Note that DataTable itself does not populate action menu items, you can provide all necessary content via handler.

This event is cancellable, you can use event.preventDefault() to prevent default behaviour.

executeRowAction event

Emitted when row action is executed by user.

Usually accompanies showRowActionsMenu event. DataTable itself does not execute actions but provides support for external integration. If there were actions provided with showRowActionsMenu event then executeRowAction will be automatically executed when user clicks corresponding menu item.

<alfresco-datatable
    [data]="data"
    [multiselect]="multiselect"
    [actions]="true"
    (showRowActionsMenu)="onShowRowActionsMenu($event)"
    (executeRowAction)="onExecuteRowAction($event)">
</alfresco-datatable>
import { DataCellEvent, DataRowActionEvent } from 'ng2-alfresco-datatable';

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}`);
}

npm.io

npm.io

Developers are allowed putting any payloads as row actions. The only requirement for the objects is having title property.

Once corresponding action is clicked in the dropdown menu DataTable invokes executeRowAction event where you can handle the process, inspect the action payload and all custom properties defined earlier, and do corresponding actions.

Data sources

DataTable component gets data by means of data adapter. It is possible having data retrieved from different kinds of sources by implementing a custom DataTableAdapter using the following interfaces:

interface DataTableAdapter {
    selectedRow: DataRow;
    getRows(): Array<DataRow>;
    setRows(rows: Array<DataRow>): void;
    getColumns(): Array<DataColumn>;
    setColumns(columns: Array<DataColumn>): void;
    getValue(row: DataRow, col: DataColumn): any;
    getSorting(): DataSorting;
    setSorting(sorting: DataSorting): void;
    sort(key?: string, direction?: string): void;
}

interface DataRow {
    isSelected: boolean;
    hasValue(key: string): boolean;
    getValue(key: string): any;
}

interface DataColumn {
    key: string;
    type: string; // text|image|date
    format?: string;
    sortable?: boolean;
    title?: string;
    srTitle?: string;
    cssClass?: string;
    template?: TemplateRef<any>;
}

DataTable provides ObjectDataTableAdapter out-of-box. This is a simple data adapter implementation that binds to object arrays and turns object fields into columns:

let 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', 
            sortable: true
        }
    ]
);

Generate schema

Is possible to auto generate your schema if you have only the data row

let data =  [
    { id: 2, name: 'abs' },
    { id: 1, name: 'xyz' }
];

let schema = ObjectDataTableAdapter.generateSchema(data);

/*Auto generated schema value:
[
    { 
        type: 'text', 
        key: 'id', 
        title: 'Id', 
        sortable: false 
    },
    {
        type: 'text', 
        key: 'name', 
        title: 'Name', 
        sortable: false
    }
] 
*/

Pagination Component

The pagination object is a generic component to paginate component. The Alfresco API are paginated and returns a Pagination object. You can use the pagination object to feed the pagination component and then listen to the event which return the current pagination and query again the API with the options choose by the user.

DataTable demo

Properties

NameTypeDefaultDescription
supportedPageSizesnumer[]5, 10, 20, 50, 100This array describe the set of options showed in the pick list
maxItemsbooleanfalseMax number of element showed per page. If you pick another size from the pick list this option will be overwritten
paginationPagination{count: 0, totalItems: 0, skipCount: 0, maxItems: 20 , hasMoreItems: true}The Alfresco Api return a pagination object, you can use it to feed the pagination component, or create your own.

Events

NameDescription
changePageSizeEmitted when user picks one of the options from the pick list
nextPageEmitted when user clicks next page button
prevPageEmitted when user clicks prev page button

All the events carry with them the current pagination object.

Build from sources

You can build component from sources with the following commands:

npm install
npm run build

The build task rebuilds all the code, runs tslint, license checks and other quality check tools before performing unit testing.

NPM scripts

CommandDescription
npm run buildBuild component
npm run testRun unit tests in the console
npm run test-browserRun unit tests in the browser
npm run coverageRun unit tests and display code coverage report

Demo

Please check the demo folder for a demo project

cd demo
npm install
npm start

License

Apache Version 2.0

1.10.0-beta7

6 years ago

1.10.0-beta6

7 years ago

1.10.0-beta5

7 years ago

1.10.0-beta4

7 years ago

1.10.0-beta3

7 years ago

1.10.0-beta2

7 years ago

1.10.0-beta1

7 years ago

1.9.0

7 years ago

1.9.0-beta8

7 years ago

1.9.0-beta7

7 years ago

1.9.0-beta6

7 years ago

1.9.0-beta5

7 years ago

1.9.0-beta4

7 years ago

1.9.0-beta3

7 years ago

1.9.0-beta1

7 years ago

1.8.0

7 years ago

1.8.0-beta7

7 years ago

1.8.0-beta6

7 years ago

1.8.0-beta5

7 years ago

1.8.0-beta4

7 years ago

1.8.0-beta3

7 years ago

1.8.0-beta1

7 years ago

1.7.0

7 years ago

1.7.0-beta5

7 years ago

1.7.0-beta4

7 years ago

1.7.0-beta3

7 years ago

1.7.0-beta2

7 years ago

1.7.0-beta1

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.6.0-beta14

7 years ago

1.6.0-alpha7

7 years ago

1.6.0-alpha6

7 years ago

1.6.0-alpha5

7 years ago

1.6.0-alpha3

7 years ago

1.6.0-alpha2

7 years ago

1.6.0-beta13

7 years ago

1.6.0-beta12

7 years ago

1.6.0-beta11

7 years ago

1.6.0-beta10

7 years ago

1.6.0-beta9

7 years ago

1.6.0-beta8

7 years ago

1.6.0-beta7

7 years ago

1.6.0-beta6

7 years ago

1.6.0-beta5

7 years ago

1.6.0-beta4

7 years ago

1.6.0-beta3

7 years ago

1.6.0-beta2

7 years ago

1.6.0-beta1

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.17

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago