1
- import { Component , OnInit , Input , Output , EventEmitter , OnDestroy } from '@angular/core' ;
1
+ import { Component , OnInit , Input , Output , EventEmitter , OnDestroy , ViewChild , AfterViewInit } from '@angular/core' ;
2
2
import { Store , select } from '@ngrx/store' ;
3
+ import { DataTableDirective } from 'angular-datatables' ;
3
4
4
- import { Subscription } from 'rxjs' ;
5
+ import { Subscription , Subject } from 'rxjs' ;
5
6
import { allCustomers } from './../../../../store/customer-management.selectors' ;
6
7
import { LoadCustomers , DeleteCustomer , SetCustomerToEdit } from './../../../../store/customer-management.actions' ;
7
8
import { Customer } from './../../../../../shared/models/customer.model' ;
8
9
import { ITEMS_PER_PAGE } from 'src/environments/environment' ;
10
+ import { ToastrService } from 'ngx-toastr' ;
9
11
10
12
@Component ( {
11
13
selector : 'app-customer-list' ,
12
14
templateUrl : './customer-list.component.html' ,
13
15
styleUrls : [ './customer-list.component.scss' ] ,
14
16
} )
15
- export class CustomerListComponent implements OnInit , OnDestroy {
17
+ export class CustomerListComponent implements OnInit , OnDestroy , AfterViewInit {
16
18
initPage1 = 1 ;
17
19
itemsPerPage = ITEMS_PER_PAGE ;
18
20
@Input ( ) showCustomerForm : boolean ;
19
21
@Output ( ) changeValueShowCustomerForm = new EventEmitter < boolean > ( ) ;
20
22
21
23
customers : Customer [ ] = [ ] ;
22
24
customerSubscription : Subscription ;
25
+ dtOptions : DataTables . Settings = { } ;
26
+ dtTrigger : Subject < any > = new Subject ( ) ;
27
+ @ViewChild ( DataTableDirective , { static : false } )
28
+ dtElement : DataTableDirective ;
29
+ isDtInitialized = false ;
23
30
24
- constructor ( private store : Store < Customer > ) { }
31
+ constructor ( private store : Store < Customer > , private toastrService : ToastrService ) {
32
+
33
+ }
25
34
26
35
ngOnInit ( ) : void {
36
+ this . dtOptions = {
37
+ scrollY : '250px' ,
38
+ paging : false ,
39
+ } ;
40
+
27
41
this . store . dispatch ( new LoadCustomers ( ) ) ;
28
42
const customers$ = this . store . pipe ( select ( allCustomers ) ) ;
29
43
this . customerSubscription = customers$ . subscribe ( ( response ) => {
30
- this . customers = response ;
44
+ this . customers = response ? response : [ ] ;
45
+ this . rerender ( ) ;
31
46
} ) ;
32
47
}
33
48
49
+ ngAfterViewInit ( ) : void {
50
+ this . dtTrigger . next ( ) ;
51
+ }
52
+
34
53
ngOnDestroy ( ) {
35
54
this . customerSubscription . unsubscribe ( ) ;
55
+ this . dtTrigger . unsubscribe ( ) ;
36
56
}
37
57
38
58
editCustomer ( customerId : string ) {
@@ -44,4 +64,16 @@ export class CustomerListComponent implements OnInit, OnDestroy {
44
64
deleteCustomer ( customerId : string ) {
45
65
this . store . dispatch ( new DeleteCustomer ( customerId ) ) ;
46
66
}
67
+
68
+ rerender ( ) : void {
69
+ if ( this . isDtInitialized ) {
70
+ this . dtElement . dtInstance . then ( ( dtInstance : DataTables . Api ) => {
71
+ dtInstance . destroy ( ) ;
72
+ this . dtTrigger . next ( ) ;
73
+ } ) ;
74
+ } else {
75
+ this . isDtInitialized = true ;
76
+ this . dtTrigger . next ( ) ;
77
+ }
78
+ }
47
79
}
0 commit comments