Skip to content

Commit

Permalink
feat(paging): ability to jump to page n with page links (closes #496)…
Browse files Browse the repository at this point in the history
… (#544)

* feat: Paging: Jump to page n

Data table paging - Capability to jump to page n.  A new parameter 'pageLinkCount' on the Paging-Bar Component that is a number and will create the amount of page links that can be clicked on to jump to a certain page.

Closes #496

* Fixing cases where there are an even number of pageLinks or the pageLinkCount is 2 or less

* Change to have a guid on the paging bar to be able to handle multiple paginators on the same page

* Making inputs on unit tests dynamic. First testing initial value, then changing value, and then testing for the new value.

* Special case handling for when there are two pageLinks and going all the way to send and back

* handling case where pagelinksCount is above max possible pages based on per page setting

* handling reponsive pagelinks and updating total of datatable updates pagelinks
  • Loading branch information
jeremysmartt authored and emoralesb05 committed May 4, 2017
1 parent 5bd684f commit 459dcb3
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/app/components/components/paging/paging.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<p>Total: {{event?.total || '1345'}}</p>
</md-card-content>
<md-divider></md-divider>
<td-paging-bar #pagingBar pageSizeAllText="All" [pageSizeAll]="true" [pageSizes]="[50,100,200,500,1000,2000]"
<td-paging-bar #pagingBar pageSizeAllText="All" [pageSizeAll]="true" [pageSizes]="[50,100,200,500,1000,2000]" pageLinkCount="5"
[initialPage]="1" [firstLast]="firstLast" [pageSize]="100" [total]="1345" (change)="change($event)">
<span td-paging-bar-label hide-xs>Row per page:</span>
{{pagingBar.range}} <span hide-xs>of {{pagingBar.total}}</span>
Expand All @@ -27,7 +27,7 @@
<p>HTML:</p>
<td-highlight lang="html">
<![CDATA[
<td-paging-bar #pagingBar pageSizeAllText="All" [pageSizeAll]="true" [pageSizes]="[50,100,200,500,1000,2000]"
<td-paging-bar #pagingBar pageSizeAllText="All" [pageSizeAll]="true" [pageSizes]="[50,100,200,500,1000,2000]" pageLinkCount="5"
[initialPage]="1" [firstLast]="firstLast" [pageSize]="100" [total]="1345" (change)="change($event)">
<span td-paging-bar-label hide-xs>Row per page:</span>
{ {pagingBar.range} } <span hide-xs>of { {pagingBar.total} }</span>
Expand Down Expand Up @@ -101,7 +101,7 @@ <h3>Example:</h3>
<td-highlight lang="html">
<![CDATA[
<td-paging-bar #pagingBar pageSizeAllText="allText" [firstLast]="true|false" [pageSizeAll]="true|false" [pageSizes]="[100,200,500,1000,2000]"
[initialPage]="1" [pageSize]="100" [total]="1345" (change)="change($event)">
pageLinkCount="5" [initialPage]="1" [pageSize]="100" [total]="1345" (change)="change($event)">
<span td-paging-bar-label hide-xs>Row per page:</span>
{ {pagingBar.range} } <span hide-xs>of { {pagingBar.total} }</span>
</td-paging-bar>
Expand Down
4 changes: 4 additions & 0 deletions src/app/components/components/paging/paging.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class PagingDemoComponent {
description: `Selected page size for the pagination. Defaults to first element of the [pageSizes] array.`,
name: 'pageSize?',
type: 'number',
}, {
description: `Defines the number of PageLinks to display. PageLinks are used to jump to a specific page, default is 0.`,
name: 'pageLinkCount?',
type: 'number',
}, {
description: `ets starting page for the paging bar. Defaults to '1'`,
name: 'initialPage?',
Expand Down
7 changes: 5 additions & 2 deletions src/platform/core/paging/paging-bar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
<ng-content></ng-content>
</div>
<div class="td-paging-bar-navigation">
<button md-icon-button type="button" *ngIf="firstLast" [disabled]="isMinPage()" (click)="firstPage()">
<button [id]="'td-paging-bar-' + id + '-first-page'" md-icon-button type="button" *ngIf="firstLast" [disabled]="isMinPage()" (click)="firstPage()">
<md-icon>{{ isRTL ? 'skip_next' : 'skip_previous' }}</md-icon>
</button>
<button md-icon-button type="button" [disabled]="isMinPage()" (click)="prevPage()">
<md-icon>{{ isRTL ? 'navigate_next' : 'navigate_before' }}</md-icon>
</button>
<ng-template *ngIf="pageLinkCount > 0" let-link let-index="index" ngFor [ngForOf]="pageLinks">
<button [id]="'td-paging-bar-' + id + '-page-link-' + index" md-icon-button type="button" [color]="page === link ? 'accent' : ''" (click)="navigateToPage(link)">{{link}}</button>
</ng-template>
<button md-icon-button type="button" [disabled]="isMaxPage()" (click)="nextPage()">
<md-icon>{{ isRTL ? 'navigate_before' : 'navigate_next' }}</md-icon>
</button>
<button md-icon-button type="button" *ngIf="firstLast" [disabled]="isMaxPage()" (click)="lastPage()">
<button [id]="'td-paging-bar-' + id + '-last-page'" md-icon-button type="button" *ngIf="firstLast" [disabled]="isMaxPage()" (click)="lastPage()">
<md-icon>{{ isRTL ? 'skip_previous' : 'skip_next' }}</md-icon>
</button>
</div>
Expand Down
222 changes: 222 additions & 0 deletions src/platform/core/paging/paging-bar.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import {
TestBed,
inject,
async,
ComponentFixture,
} from '@angular/core/testing';
import 'hammerjs';
import { Component } from '@angular/core';
import { By } from '@angular/platform-browser';
import { TdPagingBarComponent } from './paging-bar.component';
import { CovalentPagingModule } from './paging.module';
import { NgModule, DebugElement } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

describe('Component: TdPagingBarComponent', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
BrowserAnimationsModule,
CovalentPagingModule,
],
declarations: [
TestpageSizeAllTextComponent,
TestInitialPageComponent,
TestPageSizesComponent,
TestFirstLastComponent,
TestPageLinkCountComponent,
],
});
TestBed.compileComponents();
}));

it('should create the component', (done: DoneFn) => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TestpageSizeAllTextComponent);
let component: TestpageSizeAllTextComponent = fixture.debugElement.componentInstance;

fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component).toBeTruthy();
done();
});
});

it('should set pageSizeAllText, pageSizeAll and see it in markup', (done: DoneFn) => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TestpageSizeAllTextComponent);
let component: TestpageSizeAllTextComponent = fixture.debugElement.componentInstance;

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
let pageSizeAllText: string = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.pageSizeAllText;
expect(pageSizeAllText).toBe('SomeOtherText');

component.pageSizeAllText = 'aDifferentText';
fixture.detectChanges();
fixture.whenStable().then(() => {
pageSizeAllText = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.pageSizeAllText;
expect(pageSizeAllText).toBe('aDifferentText');

let pageSizeAll: boolean = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.pageSizeAll;
expect(pageSizeAll).toBe(true);

component.pageSizeAll = false;
fixture.detectChanges();
fixture.whenStable().then(() => {
pageSizeAll = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.pageSizeAll;
expect(pageSizeAll).toBe(false);
done();
});
});
});
});
});

it('should set pageSizes and then component instantiate with that pageSize', (done: DoneFn) => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TestPageSizesComponent);
let component: TestPageSizesComponent = fixture.debugElement.componentInstance;

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
let pageSize: number = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.pageSize;
expect(pageSize).toBe(37);

component.pageSizes = [55, 77];
fixture.detectChanges();
fixture.whenStable().then(() => {
pageSize = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.pageSize;
expect(pageSize).toBe(55);
done();
});
});
});
});

it('should set intialPage and then component instantiate with that page', (done: DoneFn) => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TestInitialPageComponent);
let component: TestInitialPageComponent = fixture.debugElement.componentInstance;

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
let page: number = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.page;
expect(page).toBe(3);
done();
});
});
});

it('should set firstLast and then see buttons in markup', (done: DoneFn) => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TestFirstLastComponent);
let component: TestFirstLastComponent = fixture.debugElement.componentInstance;

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
let id: string = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.id;
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-first-page'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-last-page'))).toBeTruthy();

component.firstLast = false;
fixture.detectChanges();
fixture.whenStable().then(() => {
id = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.id;
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-first-page'))).toBeFalsy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-last-page'))).toBeFalsy();
done();
});
});
});
});

it('should set pageLinkCount and then see buttons in markup', (done: DoneFn) => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TestPageLinkCountComponent);
let component: TestPageLinkCountComponent = fixture.debugElement.componentInstance;

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
let id: string = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.id;
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-0'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-1'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-2'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-3'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-4'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-5'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-6'))).toBeTruthy();

component.pageLinkCount = 4;
component.pageSize = 50;
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
id = fixture.debugElement.query(By.directive(TdPagingBarComponent)).componentInstance.id;
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-0'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-1'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-2'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-3'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-4'))).toBeFalsy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-5'))).toBeFalsy();
expect(fixture.debugElement.query(By.css('#td-paging-bar-' + id + '-page-link-6'))).toBeFalsy();
done();
});
});
});
});
});
});

@Component({
template: `
<td-paging-bar [pageSizeAllText]="pageSizeAllText" [pageSizeAll]="pageSizeAll"
[pageSizes]="[50,100,200,500,1000,2000]" [total]="9215"></td-paging-bar>`,
})
class TestpageSizeAllTextComponent {
pageSizeAllText: string = 'SomeOtherText';
pageSizeAll: boolean = true;
}

@Component({
template: `
<td-paging-bar pageSizeAllText="SomeOtherText" [pageSizeAll]="true"
[pageSizes]="pageSizes" [total]="9215"></td-paging-bar>`,
})
class TestPageSizesComponent {
pageSizes: number[] = [37, 48];
}

@Component({
template: `
<td-paging-bar pageSizeAllText="SomeOtherText" [pageSizeAll]="true"
[initialPage]="initialPage" [pageSizes]="[50,100,200,500,1000,2000]" [total]="9215"></td-paging-bar>`,
})
class TestInitialPageComponent {
initialPage: number = 3;
}

@Component({
template: `
<td-paging-bar pageSizeAllText="All" [pageSizeAll]="true" [pageSizes]="[50,100,200,500,1000,2000]"
[initialPage]="1" [firstLast]="firstLast" [pageSize]="100" [total]="9333"></td-paging-bar>`,
})
class TestFirstLastComponent {
firstLast: boolean = true;
}

@Component({
template: `
<td-paging-bar pageSizeAllText="All" [pageSizeAll]="true" [pageSizes]="[50,100,200,500,1000,2000]" [pageLinkCount]="pageLinkCount"
[initialPage]="1" [firstLast]="true" [pageSize]="pageSize" [total]="1345"></td-paging-bar>`,
})
class TestPageLinkCountComponent {
pageLinkCount: number = 7;
pageSize: number = 100;
}
Loading

0 comments on commit 459dcb3

Please sign in to comment.