1- import { Component , OnDestroy , OnInit } from '@angular/core' ;
1+ import { Component , OnDestroy , OnInit , ViewChild , AfterViewInit } from '@angular/core' ;
22import { ActionsSubject , select , Store } from '@ngrx/store' ;
33import { ToastrService } from 'ngx-toastr' ;
4- import { Observable , Subscription } from 'rxjs' ;
4+ import { Observable , Subscription , Subject } from 'rxjs' ;
55import { delay , filter } from 'rxjs/operators' ;
66import { ProjectSelectedEvent } from '../../shared/components/details-fields/project-selected-event' ;
77import { SaveEntryEvent } from '../../shared/components/details-fields/save-entry-event' ;
@@ -14,12 +14,13 @@ import { EntryActionTypes } from './../../time-clock/store/entry.actions';
1414import { getActiveTimeEntry , getTimeEntriesDataSource } from './../../time-clock/store/entry.selectors' ;
1515import { CookieService } from 'ngx-cookie-service' ;
1616import { FeatureToggle } from './../../../../environments/enum' ;
17+ import { DataTableDirective } from 'angular-datatables' ;
1718@Component ( {
1819 selector : 'app-time-entries' ,
1920 templateUrl : './time-entries.component.html' ,
2021 styleUrls : [ './time-entries.component.scss' ] ,
2122} )
22- export class TimeEntriesComponent implements OnInit , OnDestroy {
23+ export class TimeEntriesComponent implements OnInit , OnDestroy , AfterViewInit {
2324 entryId : string ;
2425 entry : Entry ;
2526 activeTimeEntry : Entry ;
@@ -38,6 +39,11 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
3839 selectedYear : number ;
3940 selectedMonthAsText : string ;
4041 isActiveEntryOverlapping = false ;
42+ dtOptions : any = { } ;
43+ dtTrigger : Subject < any > = new Subject ( ) ;
44+ @ViewChild ( DataTableDirective , { static : false } )
45+ dtElement : DataTableDirective ;
46+ rerenderTableSubscription : Subscription ;
4147 constructor (
4248 private store : Store < EntryState > ,
4349 private toastrService : ToastrService ,
@@ -49,8 +55,18 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
4955 }
5056 ngOnDestroy ( ) : void {
5157 this . entriesSubscription . unsubscribe ( ) ;
58+ this . rerenderTableSubscription . unsubscribe ( ) ;
59+ this . dtTrigger . unsubscribe ( ) ;
5260 }
5361 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+ } ) ;
5470 this . loadActiveEntry ( ) ;
5571 this . isFeatureToggleCalendarActive = ( this . cookiesService . get ( FeatureToggle . TIME_TRACKER_CALENDAR ) === 'true' ) ;
5672 this . entriesSubscription = this . actionsSubject$ . pipe (
@@ -65,6 +81,9 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
6581 this . store . dispatch ( new entryActions . LoadEntries ( this . selectedMonth , this . selectedYear ) ) ;
6682 } ) ;
6783 }
84+ ngAfterViewInit ( ) : void {
85+ this . rerenderDataTable ( ) ;
86+ }
6887 newEntry ( ) {
6988 if ( this . wasEditingExistingTimeEntry ) {
7089 this . entry = null ;
@@ -216,4 +235,15 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
216235 } ) ;
217236 }
218237 }
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+ }
219249}
0 commit comments