-
Notifications
You must be signed in to change notification settings - Fork 210
/
data-table-column-tmpl.component.ts
executable file
·125 lines (112 loc) · 3.88 KB
/
data-table-column-tmpl.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {
Component,
ChangeDetectionStrategy,
ContentChild,
Input,
OnChanges,
SimpleChanges,
Output,
EventEmitter,
OnDestroy,
TemplateRef
} from '@angular/core';
import { DataTableCellViewTmplComponent } from './data-table-cell-view-tmpl.component';
import { DataTableCellEditTmplComponent } from './data-table-cell-edit-tmpl.component';
import { DataTableCellFilterTmplComponent } from './data-table-cell-filter-tmpl.component';
import { formatDate } from 'ng-devui/utils';
import { DevUIConfig } from 'ng-devui/devui.config';
import { DataTableHeadCellTmplComponent } from './data-table-head-cell-tmpl.component';
import { FilterConfig } from '../data-table.model';
import { Observable } from 'rxjs';
@Component({
selector: 'd-column',
template: '',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DataTableColumnTmplComponent implements OnChanges, OnDestroy {
@Input() advancedHeader: Array<{
header: string;
rowspan: number;
colspan: number;
[prop: string]: any;
}>;
@Input() fieldType = 'text';
@Input() maxWidth: string;
@Input() minWidth: string;
@Input() field: string;
@Input() header: string;
@Input() sortable: boolean;
@Input() editable: boolean;
@Input() filterable: boolean;
@Input() width: string;
@Input() style?: any;
@Input() extraOptions: any;
@Input() order: number = Number.MAX_VALUE;
@Input() nestedColumn = false;
/**
* 传入筛选列表
*/
@Input() filterList: Array<FilterConfig>;
@Output() filterChange = new EventEmitter<FilterConfig[]>();
@Input() filterMultiple = true;
@Input() beforeFilter: (value) => boolean | Promise<boolean> | Observable<boolean>;
@ContentChild(DataTableCellViewTmplComponent) cellCmp: DataTableCellViewTmplComponent;
@ContentChild(DataTableCellEditTmplComponent) cellEditCmp: DataTableCellEditTmplComponent;
@ContentChild(DataTableCellFilterTmplComponent) cellFilterCmp: DataTableCellFilterTmplComponent;
@ContentChild(DataTableHeadCellTmplComponent) headCellTmpl: DataTableHeadCellTmplComponent;
@Input() customFilterTemplate: TemplateRef<any>;
orderChange = new EventEmitter<SimpleChanges>();
_formatter: (item: any, row?: any) => string;
constructor(private devUIConfig: DevUIConfig) {
}
@Input() set formatter(formatter: (item: any, row?: any) => string) {
this._formatter = formatter;
}
get formatter() {
return this._formatter || this.defaultFormatter.bind(this);
}
ngOnChanges(changes: SimpleChanges): void {
if (changes['order']) {
this.orderChange.emit(changes['order'].currentValue);
}
}
ngOnDestroy(): void {
this.orderChange.unsubscribe();
}
// column.extraOptions?.dateFormat
defaultFormatter(item, row?: any) {
if (this.fieldType && this[this.fieldType]) {
return this[this.fieldType](item, row);
}
return item && item.toString();
}
date(item) {
let pattern;
if (this.extraOptions && this.extraOptions.dateFormat) {
pattern = this.extraOptions.dateFormat;
} else {
pattern = this.extraOptions && this.extraOptions.showTime ?
this.devUIConfig.datePickerCN.format.time : this.devUIConfig.datePickerCN.format.date;
}
return item ? formatDate(new Date(item), pattern) : '';
}
emitFilterData(filterData) {
this.filterChange.emit(filterData);
}
canFilter(isOpen) {
let changeResult = Promise.resolve(true);
if (this.beforeFilter) {
const result: any = this.beforeFilter(isOpen);
if (typeof result !== 'undefined') {
if (result.then) {
changeResult = result;
} else if (result.subscribe) {
changeResult = (result as Observable<boolean>).toPromise();
} else {
changeResult = Promise.resolve(result);
}
}
}
return changeResult;
}
}