Usages Custom Pagination

Custom Pagination

Below is an example of custom pagination component. To connect your custom pagination component to controller, call PanemuTableController.initPaginationComponent().

Page 1 of 27 ( 266 rows )
Go to page
Id
Name
Email
Gender
Country
Amount
Enrolled
Last Login
Verified
1 Abagail Kingscote Female Philippines 9,339.72 Wed, 26 Apr 2023 Yes
2 Nicolina Coit Male Kazakhstan 3,744.95 Wed, 1 Nov 2023 No
3 Sarene Greim Male United States 4,397.68 Tue, 3 Jan 2023 Yes
4 Blair Millbank Female Philippines 3,334.58 Sat, 1 Jul 2023 No
5 Kliment Sprowle Male Indonesia 6,459.93 Mon, 25 Dec 2023 Yes
6 Winifred Dikle Female Kazakhstan 9,833.93 Sun, 6 Aug 2023 No
7 Chadd Nacci Male China 4,383.54 Tue, 3 Jan 2023 Yes
8 Matthiew Morland Female Greece 9,727.90 Sun, 22 Oct 2023 No
9 Perceval Glasheen Male Netherlands 7,201.29 Sun, 23 Jul 2023 Yes
10 Fenelia Oblein Male Sweden 3,654.03 Fri, 13 Oct 2023 No

Take a look at Global Search to see how this custom pagination plays with searching.

The code of the custom component is below.

custom-pagination.component.ts
custom-pagination.component.html
custom-pagination.component.scss
import { Component, input, OnInit, signal } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { PanemuTableController } from 'ngx-panemu-table';

const DEFAULT_MAX_ROWS = 100;

@Component({
  selector: 'custom-pagination',
  imports: [ReactiveFormsModule],
  standalone: true,
  templateUrl: 'custom-pagination.component.html',
  styleUrl: 'custom-pagination.component.scss'
})

export class CustomPaginationComponent implements OnInit {
  maxRowOptions = [10, 20, 50, 100];
  cmbMaxRow = new FormControl(DEFAULT_MAX_ROWS);
  txtGoto = new FormControl(1);
  isFirstPage = signal(true);
  isLastPage = signal(false);
  pageIndex = signal(0);
  totalPages = signal(0);
  controller = input<PanemuTableController<any>>();
  totalRows = signal(0);
  onFocusGotoPage = 0;
  
  constructor() {

    this.cmbMaxRow.valueChanges.subscribe((val) => {
      this.applyChange();
    });

  }

  ngOnInit() {
    this.controller()?.initPaginationComponent(this.refresh.bind(this));
  }

  private refresh(start: number, maxRows: number, totalRows: number) {
    /**
     * This method is called after table reloads data. It ensure that this component
     * display correct data although the pagination information (start & maxRows) is
     * set programmatically.
     */
    this.isFirstPage.set(start == 0);
    this.isLastPage.set(start + maxRows >= totalRows);
    this.pageIndex.set(Math.floor(start / maxRows));
    this.totalPages.set(Math.ceil(totalRows / maxRows));
    this.cmbMaxRow.setValue(maxRows, { emitEvent: false });
    this.totalRows.set(totalRows);
  }

  first() {
    this.pageIndex.set(0);
    this.applyChange();
  }

  previous() {
    if (this.pageIndex() > 0) {
      this.pageIndex.set(this.pageIndex() - 1);
      this.applyChange();
    }
  }

  last() {
    this.pageIndex.set(this.totalPages() - 1);
    this.applyChange();
  }

  next() {
    if (this.pageIndex() + 1 < this.totalPages()) {
      this.pageIndex.set(this.pageIndex() + 1);
      this.applyChange();
    }
  }

  private applyChange() {
    let maxRows = (this.cmbMaxRow.value ?? DEFAULT_MAX_ROWS);
    let start = this.pageIndex() * maxRows;
    this.controller()!.startIndex = start;
    this.controller()!.maxRows = maxRows;
    this.controller()?.reloadCurrentPage();
  }

  goToPage() {
    let gotoPage = Number(this.txtGoto.value);

    this.pageIndex.set(gotoPage - 1);
    this.applyChange();

  }

  onGotoBlur() {
    let gotoPage = Number(this.txtGoto.value);
    
    if (gotoPage == this.onFocusGotoPage) {
      return;
    }
    this.goToPage();
  }

  onGotoFocus() {
    this.onFocusGotoPage = this.txtGoto.value || 1;
  }
}