Skip to content

Commit bd238c4

Browse files
committed
feat(pagination): possibility to use totalPages or totalItems
1 parent 3947513 commit bd238c4

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

libs/flowbite-angular/pagination/pagination.component.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,12 @@ export class PaginationComponent extends BaseComponent<PaginationClass> {
286286

287287
/**
288288
* Value of the total items
289-
*
290-
* @required
291289
*/
292-
public readonly totalItems = model.required<number>();
290+
public readonly totalItems = model<number>();
291+
/**
292+
* Value of the total pages
293+
*/
294+
public readonly totalPages = model<number>();
293295
/**
294296
* Value of the current page
295297
*
@@ -406,10 +408,26 @@ export class PaginationComponent extends BaseComponent<PaginationClass> {
406408
});
407409

408410
/**
409-
* Value of the maximum pages calculated from `totalItems`
411+
* Value of the maximum pages. If `totalPages` is given, it will be
412+
* equal to that; otherwise, it is calculated from `totalItems`.
410413
*/
411414
public readonly maxPages = computed(() => {
412-
return Math.max(Math.ceil(this.totalItems() / this.pageSize()), 1);
415+
if (this.totalPages() !== undefined) {
416+
/**
417+
* Note that if we return just `this.totalPages()`, the type of the computed
418+
* will be `Signal<number | undefined>`, even though there is a check.
419+
* So instead of that, we return `this.totalPages()!` to ensure
420+
* the type is always `Signal<number>`.
421+
*/
422+
return this.totalPages()!;
423+
}
424+
425+
/**
426+
* The same applies here, except there is no need to check for undefined,
427+
* because if `totalPages` is undefined, `totalItems` must have
428+
* a valid number value. We check it in the init function.
429+
*/
430+
return Math.max(Math.ceil(this.totalItems()! / this.pageSize()), 1);
413431
});
414432

415433
/**
@@ -455,6 +473,10 @@ export class PaginationComponent extends BaseComponent<PaginationClass> {
455473
}
456474

457475
public override init(): void {
476+
if (this.totalPages() === undefined && this.totalItems() === undefined) {
477+
throw new Error('Either `totalPages` or `totalItems` must have a value');
478+
}
479+
458480
this.iconRegistry.addRawSvgIconInNamepsace(
459481
'flowbite-angular',
460482
'chevron-left',

0 commit comments

Comments
 (0)