Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
iteration 2: search food DONE adding pipe and created model
  • Loading branch information
jhmatienza committed Sep 15, 2018
commit 60e7d87ca9920e70b458dd6b21a8179777c0fa3a
2 changes: 2 additions & 0 deletions starter-code/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { FoodListComponent } from './food-list/food-list.component';
import { FoodFinderPipe } from './pipes/food-finder.pipe';

@NgModule({
declarations: [
AppComponent,
FoodListComponent,
FoodFinderPipe,
],
imports: [
BrowserModule,
Expand Down
14 changes: 9 additions & 5 deletions starter-code/src/app/food-list/food-list.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div *ngFor="let food of foods" class="card" style="width: 18rem;">
<img class="card-img-top" src="{{food.image}}" alt="food">
<div class="card-body">
<h5 class="card-title">{{food.name}}</h5>
<p class="card-text">{{food.calories}} Calories</p>
<div class="container">
<input type="text" class="form-control" placeholder="Search food..." name="searchFood" [(ngModel)]="searchFood" style="width:500px">

<div *ngFor="let food of foods | foodFinder: searchFood" class="card" style="width: 18rem;">
<img class="card-img-top" src="{{food.image}}" alt="food">
<div class="card-body">
<h5 class="card-title" name="name">{{food.name}}</h5>
<p class="card-text">{{food.calories}} Calories</p>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions starter-code/src/app/food-list/food-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ import foods from '../foods';
})
export class FoodListComponent {
foods: Array<Object> = foods;
searchFood: string;
}

8 changes: 8 additions & 0 deletions starter-code/src/app/pipes/food-finder.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FoodFinderPipe } from './food-finder.pipe';

describe('FoodFinderPipe', () => {
it('create an instance', () => {
const pipe = new FoodFinderPipe();
expect(pipe).toBeTruthy();
});
});
18 changes: 18 additions & 0 deletions starter-code/src/app/pipes/food-finder.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Food } from './../shared/models/food.model'

@Pipe({
name: 'foodFinder'
})
export class FoodFinderPipe implements PipeTransform {

transform(foods: Food[], searchFood: string): Food[] {

if (!foods || !searchFood){
return foods;
}

return foods.filter(food =>
food.name.toLowerCase().indexOf(searchFood.toLowerCase()) !== -1);
}
}
6 changes: 6 additions & 0 deletions starter-code/src/app/shared/models/food.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class Food {
name: string;
calories: number;
image: string;
quantity: number;
}