This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BrowserModule } from '@angular/platform-browser'; | |
import { NgModule } from '@angular/core'; | |
import { CdkTableModule } from '@angular/cdk/table'; | |
import { AppComponent } from './app.component'; | |
import { TableComponent } from './table/table.component'; | |
@NgModule({ | |
declarations: [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tableDataSource$ = new BehaviorSubject<any[]>([ | |
{ | |
name: 'Hammerer Maccabeus', | |
types: 'Holy/Fire', | |
attack: 1, | |
defense: 1, | |
speed: 1, | |
healing: 1, | |
recovery: 1, | |
health: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table cdk-table [dataSource]="tableDataSource$"> | |
<!-- Hero Name Column --> | |
<ng-container cdkColumnDef="name"> | |
<th cdk-header-cell *cdkHeaderCellDef> Hero Name </th> | |
<td cdk-cell *cdkCellDef="let row"> {{row.name}} </td> | |
<!-- no footer --> | |
</ng-container> | |
<!-- ... --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Level Up Column --> | |
<ng-container cdkColumnDef="levelUp"> | |
<th cdk-header-cell *cdkHeaderCellDef></th> | |
<td cdk-cell *cdkCellDef="let row"> | |
<button (click)="levelUp(row.name)">Level Up!!!</button> | |
</td> | |
</ng-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
heroes$ = new BehaviorSubject<{[name: string]: any}>({ | |
'Hammerer Maccabeus': { | |
name: 'Hammerer Maccabeus', | |
types: 'Holy/Fire', | |
attack: 1, | |
defense: 1, | |
speed: 1, | |
healing: 1, | |
recovery: 1, | |
health: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ngOnInit() { | |
this.heroes$.subscribe(changedHeroData => { | |
this.tableDataSource$.next(Object.values(changedHeroData)); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
levelUp(heroName: string) { | |
const updatedHero = { ... this.heroes$.value[heroName] }; | |
updatedHero.attack++; | |
updatedHero.defense++; | |
updatedHero.speed++; | |
updatedHero.recovery++; | |
updatedHero.healing++; | |
updatedHero.health++; | |
const newHeroData = { ... this.heroes$.value }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>SimpleTable</title> | |
<base href="/"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="icon" type="image/x-icon" href="favicon.ico"> | |
<link rel="stylesheet" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ngOnInit() { | |
this.heroes$.subscribe(changedHeroData => { | |
this.tableDataSource$.next(Object.values(changedHeroData)); | |
const superlatives = { | |
'highest-attack': null, | |
'lowest-attack': null, | |
'highest-defense': null, | |
'lowest-defense': null, | |
'highest-speed': null, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Hero Name Column --> | |
<ng-container cdkColumnDef="attack"> | |
<th cdk-header-cell *cdkHeaderCellDef> Attack </th> | |
<td cdk-cell | |
*cdkCellDef="let row" | |
[ngClass]="{ | |
'table-success': superlatives$.value['highest-attack'] === row.name, | |
'table-danger': superlatives$.value['lowest-attack'] === row.name | |
}"> | |
{{row.attack}} |
OlderNewer