WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 58010b8

Browse files
authored
refactor: update search functionality to reset page and filter items based on search term (#117)
1 parent a8c3797 commit 58010b8

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/app/home/home.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class="form-control"
1414
placeholder="Search an item in the catalogue"
1515
[(ngModel)]="search"
16-
(ngModelChange)="refresh()"
16+
(ngModelChange)="onSearchChange($event)"
1717
aria-label="Search"
1818
aria-describedby="search"
1919
/>
@@ -46,7 +46,7 @@
4646

4747
<mat-paginator
4848
[pageIndex]="page"
49-
[length]="items.length"
49+
[length]="filteredLength(items)"
5050
[pageSize]="catalogueService.CONF.pageSize"
5151
(page)="onPageChange($event)"
5252
[hidePageSize]="true"

src/app/home/home.component.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export class HomeComponent {
2323
this.cdRef.detectChanges();
2424
}
2525

26+
onSearchChange(value: string): void {
27+
// Ensure we reset to the first page when the search term changes
28+
this.page = 0;
29+
this.refresh();
30+
}
31+
2632
onPageChange(page: PageEvent): void {
2733
this.page = page.pageIndex;
2834
}
@@ -44,4 +50,11 @@ export class HomeComponent {
4450
}
4551
return filteredItems.slice(this.catalogueService.CONF.pageSize * this.page, this.catalogueService.CONF.pageSize * (this.page + 1));
4652
}
53+
54+
filteredLength(modules: Repository[]): number {
55+
if (!this.search) {
56+
return modules.length;
57+
}
58+
return modules.filter((item) => item.name.toLowerCase().indexOf(this.search.toLowerCase()) > -1).length;
59+
}
4760
}

0 commit comments

Comments
 (0)