File

src/pagination/pagination-nav/pagination-nav.component.ts

Description

Use pagination when you have multiple pages of data to handle. Get started with importing the module:

Example :
import { PaginationModule } from 'carbon-components-angular';
Example :
<cds-pagination-nav [model]="model" (selectPage)="selectPage($event)"></cds-pagination-nav>

In your selectPage() method set the model.currentPage to selected page, after you load the page.

Example :
selectPage(page) {
    // ... your code to load the page goes here

    this.model.currentPage = page;

    // ... anything you want to do after page selection changes goes here
}

See demo

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
Accessors

Constructor

constructor(i18n: I18n, experimental: ExperimentalService)
Parameters :
Name Type Optional
i18n I18n No
experimental ExperimentalService No

Inputs

disabled
Type : boolean
Default value : false

Set to true to disable the backward/forward buttons.

model
Type : PaginationModel

PaginationNavModel with the information about pages you're controlling.

numOfItemsToShow
Type : number
Default value : 4

Number of items to show in pagination. Minimum is 4.

size
Type : "sm" | "md" | "lg"
Default value : "lg"

Sets the pagination nav size

translations
Type : PaginationNavTranslations

Expects an object that contains some or all of:

Example :
{
        "NEXT": "Next",
        "PREVIOUS": "Previous",
}

Outputs

selectPage
Type : EventEmitter

Emits the new page number.

You should tie into this and update model.currentPage once the fresh data is finally loaded.

HostBindings

class.cds--layout--size-lg
Type : boolean
class.cds--layout--size-md
Type : boolean
class.cds--layout--size-sm
Type : boolean

Methods

Private getCuts
getCuts(splitPoint: null)
Parameters :
Name Type Optional Default value
splitPoint null No null
Returns : { front: number; back: number; }
Public getPages
getPages()
Returns : any
handleOverflowSelection
handleOverflowSelection(page)
Parameters :
Name Optional
page No
Returns : void
Public jumpToNext
jumpToNext()
Returns : void
Public jumpToPrevious
jumpToPrevious()
Returns : void

Properties

nextItemText
Default value : this.i18n.getOverridable("PAGINATION.NEXT")
Static paginationCounter
Type : number
Default value : 0
previousItemText
Default value : this.i18n.getOverridable("PAGINATION.PREVIOUS")

Accessors

translations
settranslations(value: PaginationNavTranslations)

Expects an object that contains some or all of:

Example :
{
        "NEXT": "Next",
        "PREVIOUS": "Previous",
}
Parameters :
Name Type Optional
value PaginationNavTranslations No
Returns : void
smallLayoutSize
getsmallLayoutSize()
mediumLayoutSize
getmediumLayoutSize()
largeLayoutSize
getlargeLayoutSize()
totalNumbersArray
gettotalNumbersArray()
currentPage
getcurrentPage()
setcurrentPage(value)
Parameters :
Name Optional
value No
Returns : void
totalDataLength
gettotalDataLength()
startOffset
getstartOffset()
frontCuts
getfrontCuts()
backCuts
getbackCuts()
leftArrowDisabled
getleftArrowDisabled()
rightArrowDisabled
getrightArrowDisabled()
import { PaginationModel } from "../pagination-model.class";
import {
	Component,
	Input,
	Output,
	EventEmitter,
	HostBinding
} from "@angular/core";

import { I18n, Overridable } from "carbon-components-angular/i18n";
import { ExperimentalService } from "carbon-components-angular/experimental";
import { merge } from "carbon-components-angular/utils";
import { range } from "carbon-components-angular/common";

export interface PaginationNavTranslations {
	NEXT: string;
	PREVIOUS: string;
}

/**
 * Use pagination when you have multiple pages of data to handle. Get started with importing the module:
 *
 * ```typescript
 * import { PaginationModule } from 'carbon-components-angular';
 * ```
 *
 * ```html
 * <cds-pagination-nav [model]="model" (selectPage)="selectPage($event)"></cds-pagination-nav>
 * ```
 *
 * In your `selectPage()` method set the `model.currentPage` to selected page, _after_
 * you load the page.
 *
 * ```typescript
 * selectPage(page) {
 * 	// ... your code to load the page goes here
 *
 * 	this.model.currentPage = page;
 *
 * 	// ... anything you want to do after page selection changes goes here
 * }
 * ```
 *
 * [See demo](../../?path=/story/components-pagination-nav--basic)
 */
@Component({
	selector: "cds-pagination-nav, ibm-pagination-navm",
	template: `
	<div>
		<div class="cds--pagination-nav">
			<ul class="cds--pagination-nav__list">
				<li class="cds--pagination-nav__list-item">
					<cds-icon-button
						kind="ghost"
						[size]="size"
						(click)="jumpToPrevious()"
						[disabled]="leftArrowDisabled"
						[description]="previousItemText.subject | async">
						<svg
							cdsIcon="caret--left"
							size="16"
							class="cds--btn__icon">
						</svg>
					</cds-icon-button>
				</li>
				<cds-pagination-nav-item
					*ngIf="this.numOfItemsToShow >= 5 || (this.numOfItemsToShow <= 4 && currentPage <= 1)"
					page="1"
					(click)="currentPage = 1"
					[isActive]="currentPage == 1">
				</cds-pagination-nav-item>
				<cds-pagination-overflow
					*ngIf="frontCuts"
					[count]="frontCuts"
					[fromIndex]="startOffset"
					(change)="handleOverflowSelection($event)">
				</cds-pagination-overflow>
				<cds-pagination-nav-item
					*ngFor="let page of getPages();"
					[page]="page"
					(click)="currentPage = page"
					[isActive]="currentPage == page">
				</cds-pagination-nav-item>
				<cds-pagination-overflow
					*ngIf="backCuts"
					[count]="backCuts"
					[fromIndex]="totalNumbersArray.length - backCuts - 1"
					(change)="handleOverflowSelection($event)">
				</cds-pagination-overflow>
				<cds-pagination-nav-item
					*ngIf="totalDataLength > 1"
					[page]="totalNumbersArray.length"
					(click)="currentPage = totalNumbersArray.length"
					[isActive]="currentPage == totalNumbersArray.length">
				</cds-pagination-nav-item>
				<li class="cds--pagination-nav__list-item">
					<cds-icon-button
						kind="ghost"
						[size]="size"
						(click)="jumpToNext()"
						[disabled]="rightArrowDisabled"
						[description]="nextItemText.subject | async">
						<svg
							cdsIcon="caret--right"
							size="16"
							class="cds--btn__icon">
						</svg>
					</cds-icon-button>
				</li>
			</ul>
		</div>
	</div>
	`
})
export class PaginationNav {
	static paginationCounter = 0;
	/**
	 * `PaginationNavModel` with the information about pages you're controlling.
	 */
	@Input() model: PaginationModel;
	/**
	   * Set to `true` to disable the backward/forward buttons.
	 */
	@Input() disabled = false;
	/**
	 * Number of items to show in pagination. Minimum is 4.
	 */
	@Input() numOfItemsToShow = 4;

	/**
	 * Expects an object that contains some or all of:
	 * ```
	 * {
	 *		"NEXT": "Next",
	 *		"PREVIOUS": "Previous",
	 * }
	 * ```
	 */
	@Input()
	set translations(value: PaginationNavTranslations) {
		const valueWithDefaults = merge(this.i18n.getMultiple("PAGINATION"), value);
		this.nextItemText.override(valueWithDefaults.NEXT);
		this.previousItemText.override(valueWithDefaults.PREVIOUS);
	}

	/**
	 * Sets the pagination nav size
	 */
	@Input() size: "sm" | "md" | "lg" = "lg";

	// Size
	@HostBinding("class.cds--layout--size-sm") get smallLayoutSize() {
		return this.size === "sm";
	}
	@HostBinding("class.cds--layout--size-md") get mediumLayoutSize() {
		return this.size === "md";
	}
	@HostBinding("class.cds--layout--size-lg") get largeLayoutSize() {
		return this.size === "lg";
	}

	/**
	 * Emits the new page number.
	 *
	 * You should tie into this and update `model.currentPage` once the fresh
	 * data is finally loaded.
	 */
	@Output() selectPage = new EventEmitter<number>();

	get totalNumbersArray() {
		return range(this.totalDataLength + 1, 1);
	}

	get currentPage() {
		return this.model.currentPage;
	}

	set currentPage(value) {
		value = Number(value);
		// emits the value to allow the user to update current page
		// in the model once the page is loaded
		this.selectPage.emit(value);
	}

	get totalDataLength() {
		return this.model.totalDataLength;
	}

	get startOffset() {
		return this.numOfItemsToShow <= 4 && this.currentPage > 1 ? 0 : 1;
	}

	get frontCuts() {
		const cuts = this.getCuts();
		return cuts.front;
	}

	get backCuts() {
		const cuts = this.getCuts();
		return cuts.back;
	}

	get leftArrowDisabled() {
		return this.disabled || this.currentPage === 1;
	}

	get rightArrowDisabled() {
		return this.disabled || this.currentPage === this.totalDataLength;
	}

	nextItemText = this.i18n.getOverridable("PAGINATION.NEXT");
	previousItemText = this.i18n.getOverridable("PAGINATION.PREVIOUS");

	constructor(protected i18n: I18n, protected experimental: ExperimentalService) {
		PaginationNav.paginationCounter++;
	}

	handleOverflowSelection(page) {
		if (typeof page === "number") {
			this.currentPage = page;
		}
	}

	public jumpToNext() {
		this.currentPage = this.currentPage < this.totalDataLength ? this.currentPage + 1 : this.totalDataLength;
	}

	public jumpToPrevious() {
		this.currentPage = this.currentPage > 1 ? this.currentPage - 1 : 1;
	}

	public getPages() {
		if (this.totalDataLength <= 1) {
			return null;
		}
		const cuts = this.getCuts();
		return this.totalNumbersArray.slice(this.startOffset + cuts.front, (1 + cuts.back) * -1);
	}

	private getCuts(splitPoint = null) {
		const page = this.currentPage - 1;
		const totalItems = this.totalDataLength;
		const itemsThatFit = this.numOfItemsToShow;

		if (itemsThatFit >= totalItems) {
			return {
				front: 0,
				back: 0
			};
		}
		const split = splitPoint || Math.ceil(itemsThatFit / 2) - 1;
		let frontHidden = page + 1 - split;
		let backHidden = totalItems - page - (itemsThatFit - split) + 1;

		if (frontHidden <= 1) {
			backHidden -= frontHidden <= 0 ? Math.abs(frontHidden) + 1 : 0;
			frontHidden = 0;
		}
		if (backHidden <= 1) {
			frontHidden -= backHidden <= 0 ? Math.abs(backHidden) + 1 : 0;
			backHidden = 0;
		}
		return {
			front: frontHidden,
			back: backHidden
		};
	}
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""