File
Index
Properties
|
|
|
Methods
|
|
|
Inputs
|
|
|
Accessors
|
|
|
|
dataSource
|
Type : MatTableDataSource<E>
|
|
|
|
idForSavingPagination
|
Type : string
|
|
|
Methods
|
onPaginateChange
|
onPaginateChange(event: PageEvent)
|
|
|
Parameters :
| Name |
Type |
Optional |
| event |
PageEvent
|
No
|
|
|
Readonly
LOCAL_STORAGE_KEY
|
Type : string
|
Default value : "PAGINATION-"
|
|
|
|
Readonly
pageSize
|
Type : unknown
|
Default value : signal(10)
|
|
|
|
Readonly
pageSizeOptions
|
Type : []
|
Default value : [10, 20, 50, 100]
|
|
|
|
paginator
|
Type : MatPaginator
|
|
|
Accessors
|
paginatorRef
|
setpaginatorRef(paginator: MatPaginator)
|
|
|
Parameters :
| Name |
Type |
Optional |
| paginator |
MatPaginator
|
No
|
|
import {
Component,
ViewChild,
ChangeDetectionStrategy,
effect,
input,
signal,
} from "@angular/core";
import {
MatPaginator,
MatPaginatorModule,
PageEvent,
} from "@angular/material/paginator";
import { MatTableDataSource } from "@angular/material/table";
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "app-list-paginator",
templateUrl: "./list-paginator.component.html",
styleUrls: ["./list-paginator.component.scss"],
imports: [MatPaginatorModule],
})
export class ListPaginatorComponent<E> {
readonly LOCAL_STORAGE_KEY = "PAGINATION-";
readonly pageSizeOptions = [10, 20, 50, 100];
dataSource = input<MatTableDataSource<E>>();
idForSavingPagination = input<string>();
private readonly paginatorReady = signal(false);
@ViewChild(MatPaginator, { static: true })
set paginatorRef(paginator: MatPaginator) {
this.paginator = paginator;
this.paginatorReady.set(!!paginator);
}
paginator: MatPaginator;
readonly pageSize = signal(10);
constructor() {
effect(() => {
if (this.idForSavingPagination() !== undefined) {
this.applyUserPaginationSettings();
}
});
effect(() => {
this.paginatorReady();
this.bindPaginator(this.dataSource());
});
}
onPaginateChange(event: PageEvent) {
this.pageSize.set(event.pageSize);
this.savePageSize(this.pageSize());
}
private applyUserPaginationSettings() {
const savedSize = this.getSavedPageSize();
this.pageSize.set(
savedSize && savedSize !== -1 ? savedSize : this.pageSize(),
);
}
private getSavedPageSize(): number {
return Number.parseInt(
localStorage.getItem(
this.LOCAL_STORAGE_KEY + this.idForSavingPagination(),
),
);
}
private savePageSize(size: number) {
localStorage.setItem(
this.LOCAL_STORAGE_KEY + this.idForSavingPagination(),
size?.toString(),
);
}
private bindPaginator(dataSource: MatTableDataSource<E> | undefined) {
if (!dataSource || !this.paginator) return;
dataSource.paginator = this.paginator;
}
}
<mat-paginator
(page)="onPaginateChange($event)"
[pageSize]="pageSize()"
[pageSizeOptions]="pageSizeOptions"
[showFirstLastButtons]="true"
></mat-paginator>
@use "variables/sizes";
/* The paginator is usually a little smaller than this */
$approx-width-paginator: 450px;
$slider-padding-all: 16px;
:host {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
background-color: white;
@media all and (max-width: $approx-width-paginator) {
flex-direction: column-reverse;
align-items: flex-start;
}
}
.slider {
padding-left: sizes.$regular;
font-size: small;
@media all and (max-width: $approx-width-paginator) {
padding-bottom: sizes.$regular;
}
}
Legend
Html element with directive