angular2-data-table
Version:
angular2-data-table is a Angular2 component for presenting large and complex data.
136 lines (113 loc) • 3.2 kB
text/typescript
import {
Component, Input, EventEmitter, Output, HostBinding
} from '@angular/core';
import { SortDirection, SortType, SelectionType } from '../../types';
import { nextSortDir } from '../../utils';
({
selector: 'datatable-header-cell',
template: `
<div>
<label
*ngIf="column.checkboxable && column.headerCheckboxable && selectionType === 'checkbox'"
class="datatable-checkbox">
<input
type="checkbox"
[attr.checked]="allRowsSelected"
(change)="select.emit(!allRowsSelected)"
/>
</label>
<span
class="datatable-header-cell-label draggable"
*ngIf="!column.headerTemplate"
(click)="onSort()"
[innerHTML]="name">
</span>
<template
*ngIf="column.headerTemplate"
[ngTemplateOutlet]="column.headerTemplate"
[ngOutletContext]="{
column: column,
sortDir: sortDir
}">
</template>
<span
class="sort-btn"
[class]="sortClass">
</span>
</div>
`
})
export class DataTableHeaderCellComponent {
() sortType: SortType;
() column: any;
() sortAscendingIcon: string;
() sortDescendingIcon: string;
() allRowsSelected: boolean;
() selectionType: SelectionType;
('style.height.px')
() headerHeight: number;
() set sorts(val: any[]) {
this._sorts = val;
this.sortDir = this.calcSortDir(val);
this.sortClass = this.calcSortClass(this.sortDir);
}
get sorts(): any[] {
return this._sorts;
}
() sort: EventEmitter<any> = new EventEmitter();
() select: EventEmitter<any> = new EventEmitter();
('class')
get columnCssClasses(): any {
let cls = 'datatable-header-cell';
if(this.column.sortable) cls += ' sortable';
if(this.column.resizeable) cls += ' resizeable';
const sortDir = this.sortDir;
if(sortDir) {
cls += ` sort-active sort-${sortDir}`;
}
return cls;
}
('attr.title')
get name(): string {
return this.column.name || this.column.prop;
}
('style.minWidth.px')
get minWidth(): number {
return this.column.minWidth;
}
('style.maxWidth.px')
get maxWidth(): number {
return this.column.maxWidth;
}
('style.width.px')
get width(): number {
return this.column.width;
}
sortClass: string;
sortDir: SortDirection;
_sorts: any[];
calcSortDir(sorts: any[]): any {
if(sorts && this.column) {
const sort = sorts.find((s: any) => {
return s.prop === this.column.prop;
});
if(sort) return sort.dir;
}
}
onSort(): void {
if(!this.column.sortable) return;
const newValue = nextSortDir(this.sortType, this.sortDir);
this.sort.emit({
column: this.column,
prevValue: this.sortDir,
newValue
});
}
calcSortClass(sortDir): string {
if(sortDir === SortDirection.asc) {
return `sort-asc ${this.sortAscendingIcon}`;
} else if(sortDir === SortDirection.desc) {
return `sort-desc ${this.sortDescendingIcon}`;
}
}
}