1- import { Component , OnDestroy , OnInit } from '@angular/core' ;
1+ import { AfterViewInit , Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ;
22import { ActionsSubject , select , Store } from '@ngrx/store' ;
3+ import { DataTableDirective } from 'angular-datatables' ;
34import { ToastrService } from 'ngx-toastr' ;
4- import { Observable , Subscription } from 'rxjs' ;
5+ import { Observable , Subscription , Subject } from 'rxjs' ;
56import { delay , filter } from 'rxjs/operators' ;
67import { ProjectSelectedEvent } from '../../shared/components/details-fields/project-selected-event' ;
78import { SaveEntryEvent } from '../../shared/components/details-fields/save-entry-event' ;
@@ -20,7 +21,12 @@ import { CalendarView } from 'angular-calendar';
2021 templateUrl : './time-entries.component.html' ,
2122 styleUrls : [ './time-entries.component.scss' ] ,
2223} )
23- export class TimeEntriesComponent implements OnInit , OnDestroy {
24+ export class TimeEntriesComponent implements OnInit , OnDestroy , AfterViewInit {
25+ dtOptions : any = { } ;
26+ dtTrigger : Subject < any > = new Subject ( ) ;
27+ @ViewChild ( DataTableDirective , { static : false } )
28+ dtElement : DataTableDirective ;
29+ rerenderTableSubscription : Subscription ;
2430 entryId : string ;
2531 entry : Entry ;
2632 activeTimeEntry : Entry ;
@@ -52,9 +58,7 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
5258 this . actualDate = new Date ( ) ;
5359 this . timeEntriesDataSource$ = this . store . pipe ( delay ( 0 ) , select ( getTimeEntriesDataSource ) ) ;
5460 }
55- ngOnDestroy ( ) : void {
56- this . entriesSubscription . unsubscribe ( ) ;
57- }
61+
5862 ngOnInit ( ) : void {
5963 this . loadActiveEntry ( ) ;
6064 this . isFeatureToggleCalendarActive = ( this . cookiesService . get ( FeatureToggle . TIME_TRACKER_CALENDAR ) === 'true' ) ;
@@ -69,7 +73,31 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
6973 this . loadActiveEntry ( ) ;
7074 this . store . dispatch ( new entryActions . LoadEntries ( this . selectedMonth , this . selectedYear ) ) ;
7175 } ) ;
76+ this . rerenderTableSubscription = this . timeEntriesDataSource$ . subscribe ( ( ds ) => {
77+ this . rerenderDataTable ( ) ;
78+ } ) ;
7279 }
80+
81+ ngAfterViewInit ( ) : void {
82+ this . rerenderDataTable ( ) ;
83+ }
84+
85+ ngOnDestroy ( ) : void {
86+ this . rerenderTableSubscription . unsubscribe ( ) ;
87+ this . entriesSubscription . unsubscribe ( ) ;
88+ }
89+
90+ private rerenderDataTable ( ) : void {
91+ if ( this . dtElement && this . dtElement . dtInstance ) {
92+ this . dtElement . dtInstance . then ( ( dtInstance : DataTables . Api ) => {
93+ dtInstance . destroy ( ) ;
94+ this . dtTrigger . next ( ) ;
95+ } ) ;
96+ } else {
97+ this . dtTrigger . next ( ) ;
98+ }
99+ }
100+
73101 newEntry ( ) {
74102 if ( this . wasEditingExistingTimeEntry ) {
75103 this . entry = null ;
0 commit comments