Skip to content

Commit b0ff656

Browse files
macrisguncayJuan Gabriel Guzman
authored andcommitted
fix: #228 table-component
1 parent a43f676 commit b0ff656

File tree

7 files changed

+61
-20
lines changed

7 files changed

+61
-20
lines changed

angular.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
"src/assets"
2525
],
2626
"styles": [
27+
"node_modules/datatables.net-dt/css/jquery.dataTables.css",
2728
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
2829
"src/styles.scss",
2930
"node_modules/ngx-toastr/toastr.css"
3031
],
3132
"scripts": [
32-
"./node_modules/jquery/dist/jquery.min.js",
33-
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
33+
"node_modules/jquery/dist/jquery.js",
34+
"./node_modules/bootstrap/dist/js/bootstrap.min.js",
35+
"node_modules/datatables.net/js/jquery.dataTables.js"
3436
]
3537
},
3638
"configurations": {

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
"@ngrx/effects": "^9.0.0",
2424
"@ngrx/store": "^9.0.0",
2525
"@ngrx/store-devtools": "^9.0.0",
26+
"angular-datatables": "^9.0.2",
2627
"angular-ng-autocomplete": "^2.0.1",
2728
"bootstrap": "^4.4.1",
28-
"jquery": "^3.5.0",
29+
"datatables.net": "^1.10.21",
30+
"datatables.net-dt": "^1.10.21",
31+
"jquery": "^3.5.1",
2932
"minimist": "^1.2.5",
3033
"moment": "^2.25.3",
3134
"msal": "^1.2.1",
@@ -47,8 +50,10 @@
4750
"@stryker-mutator/core": "^3.1.0",
4851
"@stryker-mutator/karma-runner": "^3.1.0",
4952
"@stryker-mutator/typescript": "^3.1.0",
53+
"@types/datatables.net": "^1.10.19",
5054
"@types/jasmine": "~3.5.0",
5155
"@types/jasminewd2": "~2.0.3",
56+
"@types/jquery": "^3.3.38",
5257
"@types/node": "^12.11.1",
5358
"codelyzer": "^5.1.2",
5459
"commit-message-validator": "^0.1.11",

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BrowserModule } from '@angular/platform-browser';
55
import { NgModule } from '@angular/core';
66
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
77
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
8+
import { DataTablesModule } from 'angular-datatables';
89
import { StoreModule } from '@ngrx/store';
910
import { EffectsModule } from '@ngrx/effects';
1011
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
@@ -106,6 +107,7 @@ import { TimeDetailsPipe } from './modules/time-clock/pipes/time-details.pipe';
106107
ReactiveFormsModule,
107108
HttpClientModule,
108109
NgxPaginationModule,
110+
DataTablesModule,
109111
AutocompleteLibModule,
110112
StoreModule.forRoot(reducers, {
111113
metaReducers,
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<table class="table table-sm table-bordered table-striped mb-0">
1+
<table *ngIf="customers" class="table table-sm table-bordered table-striped mb-0" datatable [dtTrigger]="dtTrigger" [dtOptions]="dtOptions">
22
<thead class="thead-orange">
33
<tr class="d-flex">
44
<th class="col-9">Name</th>
@@ -8,7 +8,7 @@
88
<tbody>
99
<tr
1010
class="d-flex"
11-
*ngFor="let customer of customers | paginate: { itemsPerPage: itemsPerPage, currentPage: initPage1, id: 'first' }"
11+
*ngFor="let customer of customers"
1212
>
1313
<td class="col-sm-9">{{ customer.name }}</td>
1414
<td class="col-sm-3 text-center">
@@ -22,12 +22,3 @@
2222
</tr>
2323
</tbody>
2424
</table>
25-
<div class="d-flex align-items-end flex-column">
26-
<pagination-controls
27-
class="mt-auto p-2 custom-pagination"
28-
(pageChange)="initPage1 = $event"
29-
id="first"
30-
previousLabel=""
31-
nextLabel=""
32-
></pagination-controls>
33-
</div>
Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,58 @@
1-
import { Component, OnInit, Input, Output, EventEmitter, OnDestroy } from '@angular/core';
1+
import { Component, OnInit, Input, Output, EventEmitter, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
22
import { Store, select } from '@ngrx/store';
3+
import { DataTableDirective } from 'angular-datatables';
34

4-
import { Subscription } from 'rxjs';
5+
import { Subscription, Subject } from 'rxjs';
56
import { allCustomers } from './../../../../store/customer-management.selectors';
67
import { LoadCustomers, DeleteCustomer, SetCustomerToEdit } from './../../../../store/customer-management.actions';
78
import { Customer } from './../../../../../shared/models/customer.model';
89
import { 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
}

src/styles.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ body {
1212
padding: 0;
1313
overflow-x: hidden;
1414
}
15+
16+
.table.dataTable th, .table.dataTable td{
17+
box-sizing: border-box;
18+
}

tsconfig.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
"lib": [
1818
"es2018",
1919
"dom"
20-
]
20+
],
21+
"paths": {
22+
"@angular/*": [
23+
"../node_modules/@angular/*"
24+
]
25+
}
2126
},
2227
"angularCompilerOptions": {
2328
"fullTemplateTypeCheck": true,

0 commit comments

Comments
 (0)