1
- import { Component , OnDestroy , OnInit } from '@angular/core' ;
1
+ import { Component , OnDestroy , OnInit , ViewChild , AfterViewInit } from '@angular/core' ;
2
2
import { ActionsSubject , select , Store } from '@ngrx/store' ;
3
3
import { ToastrService } from 'ngx-toastr' ;
4
- import { Observable , Subscription } from 'rxjs' ;
4
+ import { Observable , Subscription , Subject } from 'rxjs' ;
5
5
import { delay , filter } from 'rxjs/operators' ;
6
6
import { ProjectSelectedEvent } from '../../shared/components/details-fields/project-selected-event' ;
7
7
import { SaveEntryEvent } from '../../shared/components/details-fields/save-entry-event' ;
@@ -14,12 +14,13 @@ import { EntryActionTypes } from './../../time-clock/store/entry.actions';
14
14
import { getActiveTimeEntry , getTimeEntriesDataSource } from './../../time-clock/store/entry.selectors' ;
15
15
import { CookieService } from 'ngx-cookie-service' ;
16
16
import { FeatureToggle } from './../../../../environments/enum' ;
17
+ import { DataTableDirective } from 'angular-datatables' ;
17
18
@Component ( {
18
19
selector : 'app-time-entries' ,
19
20
templateUrl : './time-entries.component.html' ,
20
21
styleUrls : [ './time-entries.component.scss' ] ,
21
22
} )
22
- export class TimeEntriesComponent implements OnInit , OnDestroy {
23
+ export class TimeEntriesComponent implements OnInit , OnDestroy , AfterViewInit {
23
24
entryId : string ;
24
25
entry : Entry ;
25
26
activeTimeEntry : Entry ;
@@ -38,6 +39,11 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
38
39
selectedYear : number ;
39
40
selectedMonthAsText : string ;
40
41
isActiveEntryOverlapping = false ;
42
+ dtOptions : any = { } ;
43
+ dtTrigger : Subject < any > = new Subject ( ) ;
44
+ @ViewChild ( DataTableDirective , { static : false } )
45
+ dtElement : DataTableDirective ;
46
+ rerenderTableSubscription : Subscription ;
41
47
constructor (
42
48
private store : Store < EntryState > ,
43
49
private toastrService : ToastrService ,
@@ -49,8 +55,18 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
49
55
}
50
56
ngOnDestroy ( ) : void {
51
57
this . entriesSubscription . unsubscribe ( ) ;
58
+ this . rerenderTableSubscription . unsubscribe ( ) ;
59
+ this . dtTrigger . unsubscribe ( ) ;
52
60
}
53
61
ngOnInit ( ) : void {
62
+ this . dtOptions = {
63
+ scrollY : '325px' ,
64
+ paging : false ,
65
+ responsive : true ,
66
+ } ;
67
+ this . rerenderTableSubscription = this . timeEntriesDataSource$ . subscribe ( ( ds ) => {
68
+ this . rerenderDataTable ( ) ;
69
+ } ) ;
54
70
this . loadActiveEntry ( ) ;
55
71
this . isFeatureToggleCalendarActive = ( this . cookiesService . get ( FeatureToggle . TIME_TRACKER_CALENDAR ) === 'true' ) ;
56
72
this . entriesSubscription = this . actionsSubject$ . pipe (
@@ -65,6 +81,9 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
65
81
this . store . dispatch ( new entryActions . LoadEntries ( this . selectedMonth , this . selectedYear ) ) ;
66
82
} ) ;
67
83
}
84
+ ngAfterViewInit ( ) : void {
85
+ this . rerenderDataTable ( ) ;
86
+ }
68
87
newEntry ( ) {
69
88
if ( this . wasEditingExistingTimeEntry ) {
70
89
this . entry = null ;
@@ -216,4 +235,15 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
216
235
} ) ;
217
236
}
218
237
}
238
+
239
+ private rerenderDataTable ( ) : void {
240
+ if ( this . dtElement && this . dtElement . dtInstance ) {
241
+ this . dtElement . dtInstance . then ( ( dtInstance : DataTables . Api ) => {
242
+ dtInstance . destroy ( ) ;
243
+ this . dtTrigger . next ( ) ;
244
+ } ) ;
245
+ } else {
246
+ this . dtTrigger . next ( ) ;
247
+ }
248
+ }
219
249
}
0 commit comments