Skip to content

Commit a20b2df

Browse files
committed
As of Module 6
1 parent 2636723 commit a20b2df

File tree

6 files changed

+61
-8
lines changed

6 files changed

+61
-8
lines changed

APM-CLI-Final/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { FormsModule } from '@angular/forms';
44

55
import { AppComponent } from './app.component';
66
import { ProductListComponent } from './products/product-list.component';
7+
import { ConvertToSpacesPipe } from './shared/convert-to-spaces.pipe';
78

89
@NgModule({
910
declarations: [
1011
AppComponent,
11-
ProductListComponent
12+
ProductListComponent,
13+
ConvertToSpacesPipe
1214
],
1315
imports: [
1416
BrowserModule,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thead {
2+
color: #337AB7
3+
}

APM-CLI-Final/src/app/products/product-list.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h3>Filtered by: {{listFilter}} </h3>
3333
</tr>
3434
</thead>
3535
<tbody>
36-
<tr *ngFor='let product of products'>
36+
<tr *ngFor='let product of filteredProducts'>
3737
<td>
3838
<img *ngIf='showImage'
3939
[src]='product.imageUrl'
@@ -42,7 +42,7 @@ <h3>Filtered by: {{listFilter}} </h3>
4242
[style.margin.px]='imageMargin'>
4343
</td>
4444
<td>{{ product.productName }}</td>
45-
<td>{{ product.productCode | lowercase }}</td>
45+
<td>{{ product.productCode | lowercase | convertToSpaces: '-' }}</td>
4646
<td>{{ product.releaseDate }}</td>
4747
<td>{{ product.price | currency:'USD':true:'1.2-2'}}</td>
4848
<td>{{ product.starRating }}</td>

APM-CLI-Final/src/app/products/product-list.component.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
2+
import { IProduct } from './product';
23

34
@Component({
45
selector: 'pm-products',
5-
templateUrl: './product-list.component.html'
6+
templateUrl: './product-list.component.html',
7+
styleUrls: ['./product-list.component.css']
68
})
7-
export class ProductListComponent {
9+
export class ProductListComponent implements OnInit {
810
pageTitle: string = 'Product List';
911
imageWidth: number = 50;
1012
imageMargin: number = 2;
1113
showImage: boolean = false;
12-
listFilter: string = 'cart';
13-
products: any[] = [
14+
15+
_listFilter: string;
16+
get listFilter(): string {
17+
return this._listFilter;
18+
}
19+
set listFilter(value: string) {
20+
this._listFilter = value;
21+
this.filteredProducts = this.listFilter ? this.performFilter(this.listFilter) : this.products;
22+
}
23+
24+
filteredProducts: IProduct[];
25+
products: IProduct[] = [
1426
{
1527
"productId": 2,
1628
"productName": "Garden Cart",
@@ -33,7 +45,22 @@ export class ProductListComponent {
3345
}
3446
];
3547

48+
constructor() {
49+
this.filteredProducts = this.products;
50+
this.listFilter = 'cart';
51+
}
52+
53+
performFilter(filterBy: string): IProduct[] {
54+
filterBy = filterBy.toLocaleLowerCase();
55+
return this.products.filter((product: IProduct) =>
56+
product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1);
57+
}
58+
3659
toggleImage(): void {
3760
this.showImage = !this.showImage;
3861
}
62+
63+
ngOnInit(): void {
64+
console.log('In OnInit');
65+
}
3966
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface IProduct {
2+
productId: number;
3+
productName: string;
4+
productCode: string;
5+
releaseDate: string;
6+
price: number;
7+
description: string;
8+
starRating: number;
9+
imageUrl: string;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'convertToSpaces'
5+
})
6+
export class ConvertToSpacesPipe implements PipeTransform {
7+
8+
transform(value: string, character: string): string {
9+
return value.replace(character, ' ');
10+
}
11+
}

0 commit comments

Comments
 (0)