Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. import { MatTableDataSource } from '@angular/material/table';
  3. import { Subscription } from 'rxjs';
  4. import { Book } from './models/book';
  5. import { Page } from './models/page';
  6. import { BookService } from './services/book.service';
  7.  
  8. interface TableData {
  9. isbn: string;
  10. name: string;
  11. authors: string;
  12. year: string;
  13. pages: number;
  14. }
  15.  
  16. @Component({
  17. selector: 'app-root',
  18. templateUrl: './app.component.html',
  19. styleUrls: ['./app.component.scss']
  20. })
  21. export class AppComponent implements OnInit, OnDestroy {
  22. private subs = new Subscription();
  23.  
  24. displayedColumns = [
  25. 'isbn',
  26. 'name',
  27. 'authors',
  28. 'year',
  29. 'pages'
  30. ];
  31. dataSource = new MatTableDataSource<TableData>();
  32.  
  33. constructor(
  34. private service: BookService,
  35. ) { }
  36.  
  37. ngOnInit() {
  38.  
  39. this.subs.add(
  40. this.service.findbyPage(0, 5).subscribe((page: Page<Book>) => {
  41. this.dataSource.data = this.toTableData(page.content);
  42. })
  43. );
  44. }
  45.  
  46. private toTableData(books: Book[]): TableData[] {
  47. return books.map(b => {
  48. return {
  49. isbn: b.isbn,
  50. name: b.name,
  51. authors: b.authorName,
  52. year: b.year,
  53. pages: b.totalPage,
  54. };
  55. });
  56. }
  57.  
  58. ngOnDestroy() {
  59. this.subs.unsubscribe();
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement