From 2511cb4a88e455289ad760a3124ca4baa4267fd9 Mon Sep 17 00:00:00 2001 From: Peter Schneider Date: Tue, 22 Sep 2020 12:08:19 +0200 Subject: [PATCH] #4 Prepare data for Angular Table. --- src/app/app.component.ts | 134 ++++++++++++++++++++++++++++----------- 1 file changed, 96 insertions(+), 38 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 1dbace8..a2f7270 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -4,9 +4,14 @@ import { HttpClient } from '@angular/common/http'; import { HttpService } from './http.service'; import { KeyValuePipe } from '@angular/common'; import { display_data } from './display_data'; +import { MatTableDataSource } from '@angular/material'; +import { DataSource } from '@angular/cdk/collections'; +import { animate, state, style, transition, trigger } from '@angular/animations'; +import { Observable, of } from 'rxjs'; -let ELEMENT_DATA: display_data[] = []; + +const ELEMENT_DATA: display_data[] = []; @Component({ selector: 'app-root', @@ -40,62 +45,106 @@ let ELEMENT_DATA: display_data[] = []; + + + + + + + name + {{element.name}} + + + + + + id + {{element.id}} + + + + + path + {{element.path}} 333 + + + + + serviceUrl + {{element.serviceUrl}} + + + + + + + Maaz + + + + + + + + + + + ` , + animations: [ + trigger('detailExpand', [ + state('collapsed', style({ height: '0px', minHeight: '0', visibility: 'hidden' })), + state('expanded', style({ height: '*', visibility: 'visible' })), + transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')), + ]), + ] +}) +export class AppComponent { + observ: any; + displayedColumns = ['name', 'id', 'path', 'serviceUrl']; + dataSource = new ExampleDataSource(); - - - - - - + elements: apis; + displayData: apis[]; - - - - - + isExpansionDetailRow = (i: number, row: Object) => row.hasOwnProperty('detailRow'); + expandedElement: any; - - - - - - - - - - + cellClicked(element) { + console.log(element.name + ' cell clicked'); + } - - -
No. {{element.id}} Name {{element.name}} Weight {{element.properties.path}} Symbol {{element.properties.serviceUrl}}
- ` , - styleUrls: ['./app.component.scss'] -}) + formatData() { -export class AppComponent { - elements: apis; - displayedColumns: string[]; - displayData: apis[]; + } + constructor(private service: HttpService) { } // tslint:disable-next-line: use-life-cycle-interface ngOnInit() { this.service.getCharacters().subscribe(data => { - this.writeValueToArray(data); + this.elements = data; + this.writeValueToArray(); }); // this.displayedColumns = ['id', 'displayName', 'description', 'path']; } - writeValueToArray(data: apis) { - const mapped = Object.keys(data).map(key => ({ type: key, value: data[key] })); + writeValueToArray() { + const mapped = Object.keys(this.elements).map(key => ({ type: key, value: this.elements[key] })); this.displayData = mapped[0].value; // for (let index = 0; index < this.displayData.length; index++) { @@ -104,16 +153,25 @@ export class AppComponent { for (const iterator of this.displayData) { const element = iterator; console.log(element); - const obj = new display_data(iterator.name, iterator.id, iterator.properties.path, - iterator.properties.serviceUrl); + const obj = new display_data(iterator.name, iterator.id, iterator.properties.path, iterator.properties.serviceUrl); ELEMENT_DATA.push(obj); } - + this.observ = new ExampleDataSource(); console.log(ELEMENT_DATA); } } +export class ExampleDataSource extends DataSource { + connect(): Observable { + const rows = []; + ELEMENT_DATA.forEach(element => rows.push(element, { detailRow: true, element })); + console.log(rows); + return of(rows); + } + + disconnect() { } +}