1- import { Component , OnInit , Input , Output , EventEmitter , OnDestroy } from '@angular/core' ;
1+ import { Component , OnInit , Input , Output , EventEmitter , OnDestroy , ViewChild , AfterViewInit } from '@angular/core' ;
22import { Store , select } from '@ngrx/store' ;
3+ import { DataTableDirective } from 'angular-datatables' ;
34
4- import { Subscription } from 'rxjs' ;
5+ import { Subscription , Subject } from 'rxjs' ;
56import { allCustomers } from './../../../../store/customer-management.selectors' ;
67import { LoadCustomers , DeleteCustomer , SetCustomerToEdit } from './../../../../store/customer-management.actions' ;
78import { Customer } from './../../../../../shared/models/customer.model' ;
89import { ITEMS_PER_PAGE } from 'src/environments/environment' ;
10+ import { ToastrService } from 'ngx-toastr' ;
911
1012@Component ( {
1113 selector : 'app-customer-list' ,
1214 templateUrl : './customer-list.component.html' ,
1315 styleUrls : [ './customer-list.component.scss' ] ,
1416} )
15- export class CustomerListComponent implements OnInit , OnDestroy {
17+ export class CustomerListComponent implements OnInit , OnDestroy , AfterViewInit {
1618 initPage1 = 1 ;
1719 itemsPerPage = ITEMS_PER_PAGE ;
1820 @Input ( ) showCustomerForm : boolean ;
1921 @Output ( ) changeValueShowCustomerForm = new EventEmitter < boolean > ( ) ;
2022
2123 customers : Customer [ ] = [ ] ;
2224 customerSubscription : Subscription ;
25+ dtOptions : DataTables . Settings = { } ;
26+ dtTrigger : Subject < any > = new Subject ( ) ;
27+ @ViewChild ( DataTableDirective , { static : false } )
28+ dtElement : DataTableDirective ;
29+ isDtInitialized = false ;
2330
24- constructor ( private store : Store < Customer > ) { }
31+ constructor ( private store : Store < Customer > , private toastrService : ToastrService ) {
32+
33+ }
2534
2635 ngOnInit ( ) : void {
36+ this . dtOptions = {
37+ scrollY : '250px' ,
38+ paging : false ,
39+ } ;
40+
2741 this . store . dispatch ( new LoadCustomers ( ) ) ;
2842 const customers$ = this . store . pipe ( select ( allCustomers ) ) ;
2943 this . customerSubscription = customers$ . subscribe ( ( response ) => {
30- this . customers = response ;
44+ this . customers = response ? response : [ ] ;
45+ this . rerender ( ) ;
3146 } ) ;
3247 }
3348
49+ ngAfterViewInit ( ) : void {
50+ this . dtTrigger . next ( ) ;
51+ }
52+
3453 ngOnDestroy ( ) {
3554 this . customerSubscription . unsubscribe ( ) ;
55+ this . dtTrigger . unsubscribe ( ) ;
3656 }
3757
3858 editCustomer ( customerId : string ) {
@@ -44,4 +64,16 @@ export class CustomerListComponent implements OnInit, OnDestroy {
4464 deleteCustomer ( customerId : string ) {
4565 this . store . dispatch ( new DeleteCustomer ( customerId ) ) ;
4666 }
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+ }
4779}
0 commit comments