Skip to content

Instantly share code, notes, and snippets.

@ZackDeRose
ZackDeRose / app.module.ts
Created July 16, 2018 23:04
Angular App Module file for "Angular CDK Tables" article
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: [
@ZackDeRose
ZackDeRose / snippet.table.component.ts
Last active July 16, 2018 23:19
Snippet of table.component.ts for "Angular CDK Tables"
tableDataSource$ = new BehaviorSubject<any[]>([
{
name: 'Hammerer Maccabeus',
types: 'Holy/Fire',
attack: 1,
defense: 1,
speed: 1,
healing: 1,
recovery: 1,
health: 5
<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>
<!-- ... -->
@ZackDeRose
ZackDeRose / snippet.level-up.table.component.html
Created July 16, 2018 23:54
Snippet for "Level Up" column of "Angular CDK Tables" article
<!-- 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>
@ZackDeRose
ZackDeRose / snippet.heroes-init.table.component.ts
Created July 16, 2018 23:57
Snippet for heroes BehaviorSubject initialization for "Angular CDK Tables" article
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
@ZackDeRose
ZackDeRose / snippet.init-table-data-source.table.component.ts
Created July 16, 2018 23:59
Snippet for Initializing tableDataSource$ for "Angular CDK Tables" article
ngOnInit() {
this.heroes$.subscribe(changedHeroData => {
this.tableDataSource$.next(Object.values(changedHeroData));
});
}
@ZackDeRose
ZackDeRose / snippet.level-up.table.component.ts
Created July 17, 2018 00:02
Snippet for initial Level Up Logic for "AngularCDK Tables" article
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 };
@ZackDeRose
ZackDeRose / index.html
Created July 17, 2018 00:38
Snippet of adding bootstrap cdk for "Angular CDK Tables"
<!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"
@ZackDeRose
ZackDeRose / snippet.superlatives.table.component.ts
Created July 17, 2018 01:19
Snippet of creating superlatives dictionary for "Angular CDK Tables"
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,
@ZackDeRose
ZackDeRose / snippet.conditional-classes.table.html
Created July 17, 2018 01:25
Snippet for conditional classes for "Angular CDK Tables" article
<!-- 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}}