Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pagination API #1352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, Input, Output, EventEmitter, OnChanges, SimpleChanges } from
import { Subscription } from 'rxjs';

import { DataSource } from '../../lib/data-source/data-source';
import {ChangePageResult} from "./types";

@Component({
selector: 'ng2-smart-table-pager',
Expand Down Expand Up @@ -39,7 +40,7 @@ import { DataSource } from '../../lib/data-source/data-source';
<span class="sr-only">Next</span>
</a>
</li>

<li class="ng2-smart-page-item page-item"
[ngClass]="{disabled: getPage() == getLast()}">
<a class="ng2-smart-page-link page-link" href="#"
Expand All @@ -50,7 +51,7 @@ import { DataSource } from '../../lib/data-source/data-source';
</li>
</ul>
</nav>

<nav *ngIf="perPageSelect && perPageSelect.length > 0" class="ng2-smart-pagination-per-page">
<label for="per-page">
Per Page:
Expand All @@ -66,13 +67,13 @@ export class PagerComponent implements OnChanges {
@Input() source: DataSource;
@Input() perPageSelect: any[] = [];

@Output() changePage = new EventEmitter<any>();
@Output() changePage = new EventEmitter<ChangePageResult>();

currentPerPage: any;

protected pages: Array<any>;
protected page: number;
protected count: number = 0;
protected count = 0;
protected perPage: number;

protected dataChangedSub: Subscription;
Expand Down
1 change: 1 addition & 0 deletions projects/ng2-smart-table/src/lib/components/pager/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ChangePageResult = {page: number};
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
<ng2-smart-table-pager *ngIf="isPagerDisplay"
[source]="source"
[perPageSelect]="perPageSelect"
(changePage)="changePage($event)">
(changePage)="onPageChange($event)">
</ng2-smart-table-pager>
19 changes: 11 additions & 8 deletions projects/ng2-smart-table/src/lib/ng2-smart-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DataSource } from './lib/data-source/data-source';
import { Row } from './lib/data-set/row';
import { deepExtend, getPageForRowIndex } from './lib/helpers';
import { LocalDataSource } from './lib/data-source/local/local.data-source';
import {ChangePageResult} from './components/pager/types';

@Component({
selector: 'ng2-smart-table',
Expand All @@ -16,7 +17,7 @@ import { LocalDataSource } from './lib/data-source/local/local.data-source';
export class Ng2SmartTableComponent implements OnChanges, OnDestroy {

@Input() source: any;
@Input() settings: Object = {};
@Input() settings: object = {};

@Output() rowSelect = new EventEmitter<any>();
@Output() rowDeselect = new EventEmitter<any>();
Expand All @@ -28,6 +29,7 @@ export class Ng2SmartTableComponent implements OnChanges, OnDestroy {
@Output() deleteConfirm = new EventEmitter<any>();
@Output() editConfirm = new EventEmitter<any>();
@Output() createConfirm = new EventEmitter<any>();
@Output() changePage = new EventEmitter<ChangePageResult>();
@Output() rowHover: EventEmitter<any> = new EventEmitter<any>();

tableClass: string;
Expand All @@ -36,10 +38,10 @@ export class Ng2SmartTableComponent implements OnChanges, OnDestroy {
isHideHeader: boolean;
isHideSubHeader: boolean;
isPagerDisplay: boolean;
rowClassFunction: Function;
rowClassFunction: (name: string, defaultValue?: any) => string;

grid: Grid;
defaultSettings: Object = {
defaultSettings: object = {
mode: 'inline', // inline|external|click-to-edit
selectMode: 'single', // single|multi
/**
Expand Down Expand Up @@ -94,18 +96,18 @@ export class Ng2SmartTableComponent implements OnChanges, OnDestroy {
rowClassFunction: () => '',
};

isAllSelected: boolean = false;
isAllSelected = false;

private onSelectRowSubscription: Subscription;
private onDeselectRowSubscription: Subscription;
private destroyed$: Subject<void> = new Subject<void>();

ngOnChanges(changes: { [propertyName: string]: SimpleChange }) {
if (this.grid) {
if (changes['settings']) {
if (changes.settings) {
this.grid.setSettings(this.prepareSettings());
}
if (changes['source']) {
if (changes.source) {
this.source = this.prepareSource();
this.grid.setSource(this.source);
}
Expand Down Expand Up @@ -226,11 +228,12 @@ export class Ng2SmartTableComponent implements OnChanges, OnDestroy {
return new LocalDataSource();
}

prepareSettings(): Object {
prepareSettings(): object {
return deepExtend({}, this.defaultSettings, this.settings);
}

changePage($event: any) {
onPageChange(changePageResult: ChangePageResult) {
this.changePage.emit(changePageResult);
this.resetAllSelector();
}

Expand Down