<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>ng-bootstrap demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
<script src="https://unpkg.com/zone.js@0.8.7/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js@0.8.7/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@^0.1.8/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@^0.19.40/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app').catch(console.error.bind(console));
</script>
</head>
<body>
<my-grid>loading...</my-grid>
</body>
</html>
var ver = {
ng: '4.0.3'
};
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
map: {
'app': './src',
'@angular/core': 'npm:@angular/core@' + ver.ng + '/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common@' + ver.ng + '/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler@' + ver.ng + '/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser@' + ver.ng + '/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@' + ver.ng + '/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http@' + ver.ng + '/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router@' + ver.ng + '/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms@' + ver.ng + '/bundles/forms.umd.js',
'rxjs': 'npm:rxjs@^5.0.1',
'typescript': 'npm:typescript@2.1.5/lib/typescript.js',
'@ng-bootstrap/ng-bootstrap': 'npm:@ng-bootstrap/ng-bootstrap@1.0.0-alpha.25/bundles/ng-bootstrap.js',
'@swimlane/ngx-datatable': 'npm:@swimlane/ngx-datatable@9.1.0/release/index.js',
},
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule);
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { NgxDatatableModule } from '@swimlane/ngx-datatable';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MyGridComponent } from './mygrid.component';
@NgModule({
declarations: [
MyGridComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
NgxDatatableModule,
NgbModule.forRoot(),
],
bootstrap: [MyGridComponent]
})
export class AppModule {
}
import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { DatatableComponent } from '@swimlane/ngx-datatable';
@Component({
selector: 'my-grid',
templateUrl: './src/mygrid.component.html'
})
export class MyGridComponent implements OnInit {
@ViewChild(DatatableComponent) table:DatatableComponent;
data = [];
cols = [
{prop: 'name'},
{prop: 'age'}
];
totalSize:number;
pageSize:number;
currentPage:number; // 1 based paging for ng-bootstrap
limitOptions = [
{
key: '5',
value: 5
},
{
key: '10',
value: 10
},
{
key: '20',
value: 20
}
];
constructor() {
}
ngOnInit() {
this.currentPage = 1;
this.pageSize = 10;
this.table.limit = 10;
this.updateData();
}
onPageChanged(pageNum) {
this.currentPage = pageNum;
this.table.offset = pageNum - 1;
}
onSort(event) {
this.onPageChanged(1);
}
updateData() {
let dataLength = 50 + Math.floor(Math.random() * 100);
this.data = [];
for (var i = 0; i < dataLength; i++) {
this.data.push({
name: 'name ' + (i + 1),
age: Math.floor(Math.random() * 50)
});
}
this.totalSize = this.data.length;
// clear sorting on new data
this.table.sorts = [];
// reset current pagination to first page after data refresh
this.onPageChanged(1);
}
onPageSizeChanged(event) {
this.table.limit = event;
this.onPageChanged(1);
this.data = this.data.slice();
console.log(event);
}
}
<select [(ngModel)]="pageSize" (ngModelChange)="onPageSizeChanged($event)">
<option *ngFor="let opt of limitOptions" [ngValue]="opt.value">{{opt.key}}</option>
</select> Rows per page
<br/>
<ngx-datatable
#table
class="material striped"
[rows]="data"
[columns]="cols"
[columnMode]="'force'"
[footerHeight]="50"
[rowHeight]="'auto'"
(sort)="onSort($event)">
<ngx-datatable-footer>
<ng-template ngx-datatable-footer-template>
<ngb-pagination
#ngbPage
[boundaryLinks]="true"
[collectionSize]="totalSize"
[pageSize]="pageSize"
[page]="currentPage"
[maxSize]="5"
(pageChange)="onPageChanged($event)">
</ngb-pagination>
</ng-template>
</ngx-datatable-footer>
</ngx-datatable>