Skip to content

Commit

Permalink
added filter search
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias.wright committed Jun 9, 2023
1 parent 6f59d8f commit 948955f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { DataService } from './services/data.service';
import { SwatchComponent } from './swatch/swatch.component';
import { FilterComponent } from './filter/filter.component';
import { IntroComponent } from './intro/intro.component';
import { FilterPipe } from './filter.pipe';
import { FormsModule } from '@angular/forms';

@NgModule({
declarations: [
Expand All @@ -17,11 +19,13 @@ import { IntroComponent } from './intro/intro.component';
ColorListComponent,
SwatchComponent,
FilterComponent,
IntroComponent
IntroComponent,
FilterPipe
],
imports: [
BrowserModule,
AppRoutingModule
AppRoutingModule,
FormsModule
],
providers: [DataService],
bootstrap: [AppComponent]
Expand Down
4 changes: 3 additions & 1 deletion src/app/color-list/color-list.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

<div style="margin-bottom: 20px;" *ngFor ="let item of list">
<input class="form-control form-control-lg" type="text" placeholder="Search causes" aria-label=".form-control-lg example" [(ngModel)]="filterBy">

<div style="margin-bottom: 20px;" *ngFor ="let item of list | filter: filterBy:'cause'">
<app-swatch [colorData]="item.colorData"></app-swatch>
<app-color-card [cause]="item.cause" [displayName]="item.colorData.displayName"></app-color-card>
</div>
1 change: 1 addition & 0 deletions src/app/color-list/color-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface colorShapeo {

export class ColorListComponent implements OnInit {
list: any[] = [];
filterBy: any;


constructor(private data: DataService) { }
Expand Down
8 changes: 8 additions & 0 deletions src/app/filter.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FilterPipe } from './filter.pipe';

describe('FilterPipe', () => {
it('create an instance', () => {
const pipe = new FilterPipe();
expect(pipe).toBeTruthy();
});
});
28 changes: 28 additions & 0 deletions src/app/filter.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {

transform(value: any[], filterString: string, property: string) {

if (value.length === 0 || !filterString) {
return value;
}



let filteredCause: any[] = [];
for (let item of value) {
if (item[property].toLowerCase().includes(filterString.toLowerCase())) {
filteredCause.push(item);
}
}

console.log(filteredCause)
return filteredCause;
// return null;
}

}
1 change: 0 additions & 1 deletion src/app/filter/filter.component.html
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
<input class="form-control form-control-lg" type="text" placeholder="Search causes" aria-label=".form-control-lg example">
2 changes: 1 addition & 1 deletion src/app/filter/filter.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Component, OnInit } from '@angular/core';
styleUrls: ['./filter.component.css']
})
export class FilterComponent implements OnInit {

filterBy: any = "";
constructor() { }

ngOnInit(): void {
Expand Down

0 comments on commit 948955f

Please sign in to comment.