add pagination + pageSize filter
This commit is contained in:
1 parent
9512e5bd28
commit
26594a10d3
2 files changed
+96
-17
No files matched your search
@@ -10,17 +10,20 @@ import { LibraryService } from '../../../core/services/library.service';
|
||||
export class LibraryComponent {
|
||||
entryType: number = 0;
|
||||
|
||||
allEntries: Entry[] = [];
|
||||
|
||||
entries: Entry[] = [];
|
||||
entriesToShow: Entry[] = [];
|
||||
|
||||
entriesCount: number = 0;
|
||||
maxPage: number = 10;
|
||||
currentPage: number = 1;
|
||||
pageSize: number = 20;
|
||||
pageSize: number = 10;
|
||||
|
||||
constructor(private libraryService: LibraryService) {}
|
||||
|
||||
ngOnInit() {
|
||||
console.log('initing');
|
||||
this.entriesCount = this.entries.length;
|
||||
this.maxPage = this.entriesCount / this.pageSize;
|
||||
this.pageSize = this.getDefaultPageSize();
|
||||
this.fetchEntries();
|
||||
}
|
||||
|
||||
@@ -28,7 +31,7 @@ export class LibraryComponent {
|
||||
fetchEntries() {
|
||||
this.libraryService.getAllEntries().subscribe({
|
||||
next: (entries) => {
|
||||
this.allEntries = entries;
|
||||
this.entries = entries;
|
||||
this.filterEntries();
|
||||
},
|
||||
error: (error) => {
|
||||
@@ -37,17 +40,19 @@ export class LibraryComponent {
|
||||
});
|
||||
}
|
||||
|
||||
filterEntries() {
|
||||
async filterEntries() {
|
||||
var temp = [] as Entry[];
|
||||
|
||||
// ? type filter
|
||||
if (this.entryType != 0) {
|
||||
this.allEntries.forEach((entry) => {
|
||||
if (Boolean(this.entryType - 1) == entry.is_tvseries) temp.push(entry);
|
||||
await this.entries.forEach((entry) => {
|
||||
if (this.entryType == 1 && !entry.is_tvseries) temp.push(entry);
|
||||
else if (this.entryType == 2 && entry.is_tvseries) temp.push(entry);
|
||||
});
|
||||
} else {
|
||||
temp = this.allEntries;
|
||||
temp = this.entries;
|
||||
}
|
||||
console.log(temp);
|
||||
|
||||
// ? title search
|
||||
// let len = temp.length;
|
||||
@@ -61,20 +66,44 @@ export class LibraryComponent {
|
||||
const startIndex = (this.currentPage - 1) * this.pageSize;
|
||||
const endIndex = startIndex + this.pageSize;
|
||||
|
||||
this.entries = temp.slice(startIndex, endIndex);
|
||||
this.entriesCount = this.entriesToShow.length;
|
||||
this.maxPage = this.entriesCount / this.pageSize;
|
||||
this.entriesToShow = [];
|
||||
this.entriesToShow = temp.slice(startIndex, endIndex);
|
||||
}
|
||||
|
||||
// ? pagination
|
||||
goToPage(page: number) {
|
||||
this.currentPage = page;
|
||||
this.fetchEntries();
|
||||
this.filterEntries();
|
||||
}
|
||||
|
||||
get pageNumbers(): number[] {
|
||||
this.currentPage = this.currentPage <= 0 ? 1 : this.currentPage; // page <= 0 fix
|
||||
let startPage = this.currentPage <= 3 ? 1 : this.currentPage - 2;
|
||||
let endPage = startPage + 4;
|
||||
this.entriesCount = this.entries.length;
|
||||
this.maxPage = this.entriesCount / this.pageSize;
|
||||
|
||||
// Ensure currentPage is within the bounds.
|
||||
this.currentPage = Math.max(1, Math.min(this.currentPage, this.maxPage));
|
||||
|
||||
let pagesToShow = 5; // Total pages to show at any time
|
||||
let startPage: number, endPage: number;
|
||||
|
||||
if (this.maxPage <= pagesToShow) {
|
||||
// Case when the total number of pages is less or equal to pagesToShow.
|
||||
startPage = 1;
|
||||
endPage = this.maxPage;
|
||||
} else {
|
||||
// Calculate the start page.
|
||||
startPage = Math.max(this.currentPage - Math.floor(pagesToShow / 2), 1);
|
||||
endPage = startPage + pagesToShow - 1;
|
||||
|
||||
if (endPage >= this.maxPage) {
|
||||
endPage = this.maxPage;
|
||||
startPage = endPage - pagesToShow + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the page numbers array.
|
||||
return Array.from(
|
||||
{ length: endPage - startPage + 1 },
|
||||
(_, i) => startPage + i
|
||||
@@ -84,10 +113,16 @@ export class LibraryComponent {
|
||||
// ? setter
|
||||
setEntryType(entryType: number) {
|
||||
this.entryType = entryType;
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
}
|
||||
|
||||
setPageSize(pageSize: number) {
|
||||
this.pageSize = pageSize;
|
||||
setPageSize(event: any) {
|
||||
this.pageSize = event.target.value;
|
||||
this.filterEntries()
|
||||
}
|
||||
|
||||
getDefaultPageSize() {
|
||||
return 20;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user