angular-4-data-table
Version:
An Angular 4 data table, with pagination, sorting, expandable rows etc.
54 lines (43 loc) • 1.52 kB
text/typescript
import { Directive, Input, ContentChild, OnInit } from '@angular/core';
import { DataTableRow } from './row.component';
import { CellCallback } from '../types/cell-callback.type';
export class DataTableColumn implements OnInit {
// init:
header: string;
sortable = false;
resizable = false;
property: string;
styleClass: string;
cellColors: CellCallback;
// init and state:
width: number | string;
visible = true;
cellTemplate;
headerTemplate;
getCellColor(row: DataTableRow, index: number) {
if (this.cellColors !== undefined) {
return (<CellCallback>this.cellColors)(row.item, row, this, index);
}
}
private styleClassObject = {}; // for [ngClass]
ngOnInit() {
this._initCellClass();
}
private _initCellClass() {
if (!this.styleClass && this.property) {
if (/^[a-zA-Z0-9_]+$/.test(this.property)) {
this.styleClass = 'column-' + this.property;
} else {
this.styleClass = 'column-' + this.property.replace(/[^a-zA-Z0-9_]/g, '');
}
}
if (this.styleClass != null) {
this.styleClassObject = {
[this.styleClass]: true
};
}
}
}