Skip to content

Commit

Permalink
Add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Popovkov57 committed Sep 27, 2022
1 parent ab9a0c8 commit a364c4f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/app/store/store.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ <h4>
</h4>
<div class="card-text bg-white p-1">{{product.description}}</div>
</div>
<div class="form-inline float-start mr-1">
<select class="form-control" [value]="productsPerPage" (change)="changePageSize($event.target.value)">
<option value="3">3 per Page</option>
<option value="4">4 per Page</option>
<option value="6">6 per Page</option>
<option value="8">8 per Page</option>
</select>
</div>
<div class="btn-group float-end">
<button *ngFor="let page of pageNumbers"
(click)="changePage(page)"
class="btn btn-outline-primary"
[class.active]="page == selectedPage">
{{page}}
</button>
</div>
</div>
</div>
</div>
18 changes: 17 additions & 1 deletion src/app/store/store.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { ProductRepository } from "../model/product.repository";
export class StoreComponent {

public selectedCategory: string = null;
public productsPerPage = 4;
public selectedPage = 1;

constructor(private repository: ProductRepository) {}

get products(): Product[] {
return this.repository.getProducts(this.selectedCategory);
let pageIndex = (this.selectedPage - 1) * this.productsPerPage;
return this.repository.getProducts(this.selectedCategory).slice(pageIndex, pageIndex + this.productsPerPage);
}

get categories(): string[] {
Expand All @@ -25,4 +28,17 @@ export class StoreComponent {
this.selectedCategory = newCategory;
}

changePage(newPage: number) {
this.selectedPage = newPage;
}

changePageSize(newSize: number) {
this.productsPerPage = newSize;
this.changePage(1);
}

get pageNumbers(): number[] {
return Array(Math.ceil(this.repository.getProducts(this.selectedCategory).length / this.productsPerPage)).fill(0).map((x,i) => i+1);
}

}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
"strictTemplates": true,
"strictDomEventTypes": false
}
}

0 comments on commit a364c4f

Please sign in to comment.