Skip to content

Commit e164b96

Browse files
authored
Merge pull request #12 from UMM-CSci-3601/angular-pipes
Add demo of pipe for filtering users
2 parents dcd317f + e4becfa commit e164b96

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@angular/platform-browser": "^2.4.6",
1212
"@angular/platform-browser-dynamic": "^2.4.6",
1313
"@angular/router": "^3.4.6",
14+
"@angular/forms": "^2.4.7",
1415
"bootstrap": "^3.3.7",
1516
"css-loader": "^0.26.1",
1617
"es6-shim": "^0.35.3",

client/src/app.module.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@ import { HomeComponent} from './app/home/home.component';
88
import { KittensComponent } from './app/kittens/kittens.component';
99
import { UserListComponent } from './app/users/user-list.component';
1010
import { routing } from './app/app.routes';
11+
import {FormsModule} from '@angular/forms';
12+
import {FilterBy} from './app/users/filter.pipe';
1113

1214
@NgModule({
1315
imports: [
1416
BrowserModule,
1517
HttpModule,
1618
JsonpModule,
17-
routing
19+
routing,
20+
FormsModule
1821
],
1922
declarations: [
2023
AppComponent,
2124
KittensComponent,
2225
HomeComponent,
2326
NavbarComponent,
24-
UserListComponent
27+
UserListComponent,
28+
FilterBy
2529
],
2630
bootstrap: [ AppComponent ]
2731
})
2832

29-
export class AppModule {}
33+
export class AppModule {}

client/src/app/users/filter.pipe.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {Pipe, PipeTransform} from "@angular/core";
2+
3+
@Pipe({
4+
name: 'filterBy'
5+
})
6+
export class FilterBy implements PipeTransform {
7+
private filterByString(filter){
8+
filter = filter.toLowerCase();
9+
return value => {
10+
return !filter || value.toLowerCase().indexOf(filter) !== -1;
11+
}
12+
}
13+
14+
private filterByObject(filter) {
15+
return value => {
16+
for (let key in filter) {
17+
if (!value.hasOwnProperty(key)) {
18+
return false;
19+
}
20+
if (filter[key] == null){
21+
return true;
22+
}
23+
24+
const type = typeof value[key];
25+
let isMatching;
26+
27+
if (type === 'string') {
28+
isMatching = this.filterByString(filter[key])(value[key]);
29+
} else if (type === 'object') {
30+
isMatching = this.filterByObject(filter[key])(value[key]);
31+
} else {
32+
isMatching = this.filterDefault(filter[key])(value[key]);
33+
}
34+
35+
if (!isMatching) {
36+
return false;
37+
}
38+
}
39+
40+
return true;
41+
}
42+
}
43+
44+
/**
45+
* Defatul filterDefault function
46+
*
47+
* @param filter
48+
* @returns {(value:any)=>boolean}
49+
*/
50+
private filterDefault(filter) {
51+
return value => {
52+
return !filter || filter == value;
53+
}
54+
}
55+
56+
transform(array: any[], filter: string): any {
57+
if(array==null){
58+
return null;
59+
}
60+
const type = typeof filter;
61+
62+
if(type === 'string'){
63+
console.log("IMA STRING! WUTTTTT!");
64+
return array.filter(this.filterByString(filter));
65+
}
66+
67+
if(type == 'object'){
68+
return array.filter(this.filterByObject(filter));
69+
} else {
70+
console.log("Unknown filter type???");
71+
console.log(type);
72+
console.log(filter);
73+
return array;
74+
}
75+
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<ul class="list-group">
2+
<li class="list-group-item list-group-item-info">
3+
<input #input type="text" placeholder="Filter" [(ngModel)]="search">
4+
</li>
25
<li class="list-group-item list-group-item-info">User Name</li>
3-
<li *ngFor="let user of users | async" class="list-group-item">
6+
<li *ngFor="let user of users | async | filterBy: {name: search}" class="list-group-item">
47
{{ user.name }}
58
</li>
69
</ul>
+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Component } from '@angular/core';
22
import { UserListService } from "./user-list.service";
3+
import { FormsModule } from '@angular/forms';
4+
import { FilterBy } from "./filter.pipe";
35

46
@Component({
57
selector: 'user-list-component',
68
providers: [UserListService],
7-
templateUrl: 'user-list.component.html'
9+
templateUrl: 'user-list.component.html',
810
})
911

1012
export class UserListComponent {
@@ -13,4 +15,6 @@ export class UserListComponent {
1315
constructor(private _userListService: UserListService) {
1416
this.users = _userListService.getUsers();
1517
}
16-
}
18+
}
19+
20+

0 commit comments

Comments
 (0)