angular2-chapter09

Displaying Data with Pipes

Check code at: https://github.com/wghglory/angular2-fundamental

Using Built-in Pipes

Now in event list page, you can see event title case is not unique, so we can use pipe to make all titles uppercase.

Take event-detail.component.html for instance:

    <h2>{{event?.name | uppercase}} </h2>
    <div><strong>Date:</strong> {{event?.date | date:'shortDate'}}</div>
    <div><strong>Price:</strong> {{event?.price | currency:'USD':true}}</div>

Creating a Custom Pipe

Now a problem is duration shows number instead of readable text in event-detail's session list.

  1. create events/shared/duration.pipe.ts. Register it in barrel, AppModule
import { Pipe, PipeTransform } from '@angular/core'

@Pipe({ name: 'duration' })
export class DurationPipe implements PipeTransform {
    transform(value: number): string {
        switch (value) {
            case 1: return 'Half Hour'
            case 2: return 'One Hour'
            case 3: return 'Half Day'
            case 4: return 'Full Hour'
            default: return value.toString();
        }
    }
}
  1. session-list add duration pipe:
<span>Duration: {{session.duration | duration}}</span>

Sorting and Filtering Overview

Angular 1 has performance issues. One is using filter, sort by pipe. If a object array have thousands of objects and each object has many properties, it will be very costly to filter or sort. Angular 1 checks if each property of all objects has changed...

Angular 2 pipe is no longer responsible for sorting or filtering. By default, angular 2 runs the pipe only when the identity of source has changed. Angular 2 also can use "impure pipes", which is same mechanism as angular 1 with performance issue

Filtering

In event-detail page, session-list has "level", and we want to filter session-list by "level". We put 4 buttons on top.

event-detail.component.ts add "filterBy" property:

...
export class EventDetailComponent implements OnInit {
    constructor(private eventService: EventService, private route: ActivatedRoute) { }

    event: IEvent
    addMode: boolean = false
+    filterBy: string = 'all';
    ...
}

event-detail.component.html

<div class="row" style="margin-bottom:10px;">
    <div class="col-md-2">
        <h3 style="margin:0">Sessions</h3>
    </div>
+    <div class="col-md-7">
+        <button class="btn btn-default" [class.active]="filterBy==='all'" (click)="filterBy='all'">All</button>
+        <button class="btn btn-default" [class.active]="filterBy==='beginner'" (click)="filterBy='beginner'">Beginner</button>
+        <button class="btn btn-default" [class.active]="filterBy==='intermediate'" (click)="filterBy='intermediate'">Intermediate</button>
+        <button class="btn btn-default" [class.active]="filterBy==='advanced'" (click)="filterBy='advanced'">Advanced</button>
+    </div>
    <div class="col-md-2">
        <a (click)="addSession()">Add Session</a>
    </div>
</div>

+ <session-list [filterBy]="filterBy" *ngIf="!addMode" [sessions]="event?.sessions"></session-list>

session-list.component.ts

+ import { Component, Input, OnChanges } from '@angular/core'
import { ISession } from '../shared/index'

@Component({
    selector: 'session-list',
    templateUrl: 'app/events/event-detail/session-list.component.html'
})
export class SessionListComponent implements OnChanges {
    @Input() sessions: ISession[]
+    @Input() filterBy: string
+    visibleSessions: ISession[] = [];

    //whenever the input variable changes
+    ngOnChanges() {
+        if (this.sessions) {
+            this.filterSessions(this.filterBy);
+        }
+    }

+    filterSessions(filter) {
+        if (filter === 'all') {
+            // this.visibleSessions = this.sessions;  //wrong, we want to make a copy
+            this.visibleSessions = this.sessions.slice(0);  //clone the arr
+        } else {
+            this.visibleSessions = this.sessions.filter(s => {
+                return s.level.toLocaleLowerCase() == filter;
+            })
+        }
+    }
}

session-list.component.html: use visiableSessions instead of sessions

<div class="row" *ngFor="let session of visibleSessions">

Sorting

sorting by name, voters

event-detail.component.html

...
    <div class="row" style="margin-bottom:10px;">
        <div class="col-md-2">
            <h3 style="margin:0">Sessions</h3>
        </div>
        <div class="col-md-7">
+            <div class="btn-group btn-group-sm" style="margin:0 20px;">
+                <button class="btn btn-default" [class.active]="sortBy==='name'" (click)="sortBy='name'">By Name</button>
+                <button class="btn btn-default" [class.active]="sortBy==='votes'" (click)="sortBy='votes'">By Votes</button>
+            </div>

            <div class="btn-group btn-group-sm">
                <button class="btn btn-default" [class.active]="filterBy==='all'" (click)="filterBy='all'">All</button>
                <button class="btn btn-default" [class.active]="filterBy==='beginner'" (click)="filterBy='beginner'">Beginner</button>
                <button class="btn btn-default" [class.active]="filterBy==='intermediate'" (click)="filterBy='intermediate'">Intermediate</button>
                <button class="btn btn-default" [class.active]="filterBy==='advanced'" (click)="filterBy='advanced'">Advanced</button>
            </div>
        </div>
        <div class="col-md-2">
            <a (click)="addSession()">Add Session</a>
        </div>
    </div>

+    <session-list [sortBy]="sortBy" [filterBy]="filterBy" *ngIf="!addMode" [sessions]="event?.sessions"></session-list>
    <create-session *ngIf="addMode" (saveNewSession)="saveNewSession($event)" (cancelAddSession)="cancelAddSession()"></create-session>
</div>

event-detail.component.ts add sortBy property:

sortBy: string = 'votes';  //default votes

session-list.component.ts

import { Component, Input, OnChanges } from '@angular/core'
import { ISession } from '../shared/index'

@Component({
    selector: 'session-list',
    templateUrl: 'app/events/event-detail/session-list.component.html'
})
export class SessionListComponent implements OnChanges {
    @Input() sessions: ISession[]
    @Input() filterBy: string
    visibleSessions: ISession[] = [];
+    @Input() sortBy: string

    //whenever the input variable changes
    ngOnChanges() {
        if (this.sessions) {
            this.filterSessions(this.filterBy);
+           this.sortBy === 'name' ? this.visibleSessions.sort(sortByNameAsc) : this.visibleSessions.sort(sortByVotesDesc)
        }
    }

    filterSessions(filter) {
        if (filter === 'all') {
            // this.visibleSessions = this.sessions;  //wrong, we want to make a copy
            this.visibleSessions = this.sessions.slice(0);  //clone the arr
        } else {
            this.visibleSessions = this.sessions.filter(s => {
                return s.level.toLocaleLowerCase() == filter;
            })
        }
    }
}

+ function sortByNameAsc(s1: ISession, s2: ISession) {
+    if (s1.name > s2.name) return 1;
+    else if (s1.name === s2.name) return 0;
+    else return -1;
+ }

+ function sortByVotesDesc(s1: ISession, s2: ISession) {
+    return (s2.voters.length - s1.voters.length);
+ }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Routing and Navigation Check code at: https://github.com/...
    wghglory閱讀 321評(píng)論 0 0
  • 天涼了,種速生蔬菜貌似來(lái)不及了,所以視線轉(zhuǎn)移到種子吧!本期推薦小麥草,原因不光可以種草還可以作為貓草哦!之前有說(shuō)小...
    植物迷的松鼠君閱讀 578評(píng)論 0 1
  • 當(dāng)你建立了多元思維模型也就意味著你已經(jīng)開(kāi)啟了真人這段旅程的入門之旅。那么你想不想從入門到精通?想不想從初學(xué)乍練到神...
    散翎閱讀 782評(píng)論 0 1
  • 文/晨屹 【1】 終其一生,你孤獨(dú)嗎? 把孤獨(dú)說(shuō)出口,我們就不再辜負(fù)孤獨(dú)了。 一個(gè)人嗎? 恩,我也是。 每一個(gè)獨(dú)行...
    晨屹閱讀 2,168評(píng)論 60 46
  • 文/季余星 在時(shí)尚職場(chǎng)中,單品的運(yùn)用,可以多元化。而且時(shí)尚時(shí)尚元素比較強(qiáng)烈。如何多元化呢,比如毛呢外套,襯衫、風(fēng)衣...
    木烏閱讀 349評(píng)論 0 5