Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Observable} from 'rxjs';
  3. import {Customers} from './ICustomer';
  4.  
  5. @Injectable({
  6. providedIn: 'root'
  7. })
  8. export class DataServiceService {
  9.  
  10. customers: Customers[] = [
  11. {
  12. firstName: 'Matt',
  13. lastName: 'Osman'
  14. },
  15. {firstName: 'Josh',
  16. lastName: 'Allen'}
  17. ];
  18.  
  19. // get customers from the customer array
  20. public getCustomers(): any {
  21. const customersObservable = new Observable(observer => {
  22. setTimeout(() => {
  23. observer.next(this.customers);
  24. }, 1000);
  25. });
  26. return customersObservable;
  27. }
  28. // add customers to the customer array
  29. public addCustomers(data: any): any {
  30. this.customers.push(data);
  31. this.customers = [...this.customers];
  32. }
  33. constructor() {}
  34. }
  35.  
  36. export interface Customers {
  37. firstName: string;
  38. lastName: string;
  39. }
  40.  
  41. import { Component, OnInit, ViewChild } from '@angular/core';
  42. import {MatTable} from '@angular/material';
  43. import {DataServiceService} from '../data-service.service';
  44. import {Customers} from '../ICustomer';
  45. import {MatDialog} from '@angular/material/dialog';
  46. import { AddCustomerModalComponent } from '../add-customer-modal/add-customer-modal.component';
  47.  
  48. @Component({
  49. selector: 'app-customer-data-table',
  50. templateUrl: './customer-data-table.component.html',
  51. styleUrls: ['./customer-data-table.component.css']
  52. })
  53. export class CustomerDataTableComponent implements OnInit {
  54.  
  55. displayedColumns: string[] = ['firstName', 'lastName'];
  56. dataSource: Customers[] = [];
  57.  
  58. firstName: string;
  59. lastName: string;
  60.  
  61. // needed to update the data in the table after adding
  62. @ViewChild(MatTable, {static: false}) table: MatTable<any>;
  63.  
  64. constructor(private customerService: DataServiceService, public dialog: MatDialog) { }
  65.  
  66. numEmployees() {
  67. return this.dataSource.length;
  68. }
  69. // this handles the dialog open/close events
  70. openDialog(): void {
  71. const dialogRef = this.dialog.open(AddCustomerModalComponent, {
  72. width: '75%',
  73. data: {firstName: this.firstName, lastName: this.lastName}
  74. });
  75.  
  76. dialogRef.afterClosed().subscribe(result => {
  77. console.log('the dialog was closed');
  78. this.customerService.addCustomers(result); **<-- the customers[] continues updating but NOT the datasource observing it**
  79. this.table.renderRows();
  80. });
  81. }
  82. // subscribe to the dataService to connect to the customer list.
  83. ngOnInit() {
  84. const customersObservable = this.customerService.getCustomers();
  85. customersObservable.subscribe((customersData: Customers[]) => {
  86. this.dataSource = customersData; **<--- THIS UPDATES ONLY THE FIRST TIME THEN STOPS UPDATING WHEN NEW CUSTOMERS ARE ADDED**
  87. });
  88. }
  89. }
  90.  
  91. import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
  92. import { Component, Inject } from '@angular/core';
  93. import {Customers} from '../ICustomer';
  94.  
  95. @Component({
  96. selector: 'app-add-customer-modal',
  97. templateUrl: './add-customer-modal.component.html',
  98. styleUrls: ['./add-customer-modal.component.css']
  99. })
  100. export class AddCustomerModalComponent {
  101.  
  102.  
  103. constructor( public dialogRef: MatDialogRef<AddCustomerModalComponent>,
  104. @Inject(MAT_DIALOG_DATA) public data: Customers) { }
  105.  
  106. onCancel(): void {
  107. this.dialogRef.close();
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement