Skip to content

Instantly share code, notes, and snippets.

@ZackDeRose
ZackDeRose / snippet.upgraded-level-up.table.component.ts
Created July 17, 2018 01:33
Snippet of upgraded levelUp() for "Angular CDK Tables"
levelUp(heroName: string) {
const updatedHero = { ... this.heroes$.value[heroName] };
updatedHero.attack = Math.round(updatedHero.attack * (1 + (Math.random() / 8)));
updatedHero.defense = Math.round(updatedHero.defense * (1 + (Math.random() / 8)));
updatedHero.speed = Math.round(updatedHero.speed * (1 + (Math.random() / 8)));
updatedHero.recovery = Math.round(updatedHero.recovery * (1 + (Math.random() / 8)));
updatedHero.healing = Math.round(updatedHero.healing * (1 + (Math.random() / 8)));
updatedHero.health = Math.round(updatedHero.health * (1 + (Math.random() / 8)));
@ZackDeRose
ZackDeRose / snippet.header-row-declarations.table.component.html
Last active July 17, 2018 11:54
Snippet for Header and Row Declarations for "Angular CDK Tables" article
<!-- Header and Row Declarations -->
<tr cdk-header-row *cdkHeaderRowDef="displayedColumns$.value"></tr>
<tr cdk-row *cdkRowDef="let row; columns: displayedColumns$.value"></tr>
@ZackDeRose
ZackDeRose / app.module.ts
Created July 18, 2018 03:22
App module for ng-bootstrap for "Angular CDK Tables"
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { TableComponent } from './table/table.component';
import {CdkTableModule} from '@angular/cdk/table';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
@NgModule({
@ZackDeRose
ZackDeRose / snippet.new-subject.table.component.ts
Created July 18, 2018 03:25
New BehaviorSubjects for "Angular CDK Tables" article
currentPage$ = new BehaviorSubject<number>(1);
pageSize$ = new BehaviorSubject<number>(5);
dataOnPage$ = new BehaviorSubject<any[]>([]);
@ZackDeRose
ZackDeRose / snippet.data-on-page.table.component.ts
Created July 18, 2018 03:29
Data on Page initialization for "Angular CDK Tables" article
combineLatest(this.tableDataSource$, this.currentPage$, this.pageSize$)
.subscribe(([allSources, currentPage, pageSize]) => {
const startingIndex = (currentPage - 1) * pageSize;
const onPage = allSources.slice(startingIndex, startingIndex + pageSize);
this.dataOnPage$.next(onPage);
});
@ZackDeRose
ZackDeRose / snippet.pagination.table.component.html
Created July 18, 2018 03:32
Pagination template snippet for "Angular CDK Tables" article
<ngb-pagination
[collectionSize]="tableDataSource$.value.length"
[pageSize]="pageSize$.value"
[page]="currentPage$.value"
(pageChange)="currentPage$.next($event)">
</ngb-pagination>
<table cdk-table [dataSource]="dataOnPage$" class="table table-hover">
<!-- ... ->
@ZackDeRose
ZackDeRose / app.module.ts
Created July 18, 2018 03:38
App.Module for reactive forms for "Angular CDK Tables" article
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {CdkTableModule} from '@angular/cdk/table';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import { AppComponent } from './app.component';
import { TableComponent } from './table/table.component';
@ZackDeRose
ZackDeRose / snippet.initializing-for-searching.tables.components.ts
Created July 18, 2018 03:45
Initializing Subjects for Searching from "Angular CDK Tables" article
combineLatest(this.heroes$, this.searchFormControl.valueChanges)
.subscribe(([changedHeroData, searchTerm]) => {
const heroesArray = Object.values(changedHeroData);
if (!searchTerm) {
this.tableDataSource$.next(heroesArray);
return;
}
const filteredResults = heroesArray.filter(hero => {
@ZackDeRose
ZackDeRose / snippet.search-and-pagination.table.component.html
Created July 18, 2018 03:55
Template for Search and Pagination for "Angular CDK Tables" article
<div class="row">
<div class="col-6">
<div class="form-inline">
<div class="form-group">
<div class="input-group mr-3">
<input
type="text"
class="form-control"
placeholder="Search…"
[formControl]="searchFormControl">
@ZackDeRose
ZackDeRose / snippet.searching-sorting.table.component.ts
Created July 18, 2018 04:02
Searching then sorting for "Angular CDK Tables" article
combineLatest(this.heroes$, this.searchFormControl.valueChanges, this.sortKey$, this.sortDirection$)
.subscribe(([changedHeroData, searchTerm, sortKey, sortDirection]) => {
const heroesArray = Object.values(changedHeroData);
let filteredHeroes: any[];
if (!searchTerm) {
filteredHeroes = heroesArray;
} else {
const filteredResults = heroesArray.filter(hero => {
return Object.values(hero).reduce((prev, curr) => {