Angular2とBootstrap4でページネーション(ページャー)

Angular2とBootstrap4でページネーション(ページャー)のサンプルを作成しました。
※Angular2のrc版、TypeScript、Bootstrap4のα版を使って確認したものです。

angular-uiチームによる下記ライブラリを使用しています。
Angular 2 powered Bootstrap
 

デモ


 
Angular : 2.0.0-rc.5
Bootstrap : 4.0.0-α.3
ng-bootstrap : 1.0.0-α.2

Loading…

<html>
  <head>
    <title>Angular UI Bootstrap</title>
    <script src="https://npmcdn.com/core-js/client/shim.min.js"></script>
    <script src="https://npmcdn.com/zone.js@0.6.12?main=browser"></script>
    <script src="https://npmcdn.com/reflect-metadata@0.1.3"></script>
    <script src="https://npmcdn.com/systemjs@0.19.27/dist/system.src.js"></script>
    <script src="./js/systemjs.config.js"></script>
    <script>
      System.import('./ts/app.ts').catch(function(err){ console.error(err);  });    </script>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/styles.css">
  </head>
  <body>
    <div class="container">
      <my-app>Loading...</my-app>
    </div>
  </body>
</html>

(systemjs.config.js)
(function(global) {

  var ngVer = '@2.0.0-rc.5';
  var routerVer = '@3.0.0-rc.1';
  var formsVer = '@0.3.0';
  var routerDeprecatedVer = '@2.0.0-rc.2';
  var map = {
    'app':                        'app',
    '@angular':                   'https://npmcdn.com/@angular',
    '@angular/router':            'https://npmcdn.com/@angular/router' + routerVer,
    '@angular/forms':             'https://npmcdn.com/@angular/forms' + formsVer,
    '@angular/router-deprecated': 'https://npmcdn.com/@angular/router-deprecated' + routerDeprecatedVer,
    'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api',
    'rxjs':                       'https://npmcdn.com/rxjs@5.0.0-beta.6',
    '@ng-bootstrap/ng-bootstrap': 'https://npmcdn.com/@ng-bootstrap/ng-bootstrap@1.0.0-alpha.2',
    'ts':                         'https://npmcdn.com/plugin-typescript@4.0.10/lib/plugin.js',
    'typescript':                 'https://npmcdn.com/typescript@1.9.0-dev.20160409/lib/typescript.js',
 };
  var packages = {
    'app':                        { main: 'main.ts',  defaultExtension: 'ts' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    '@ng-bootstrap/ng-bootstrap': { main: 'index.js', defaultExtension: 'js' }
  };

  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'upgrade',
  ];

  ngPackageNames.forEach(function(pkgName) {
    map['@angular/'+pkgName] = 'https://npmcdn.com/@angular/' + pkgName + ngVer;
  });
  ngPackageNames.concat(['forms', 'router', 'router-deprecated']).forEach(function(pkgName) {
    packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
  });

  var ngBootstrapPackageNames = [
    'accordion',
    'alert',
    'bundles',
    'buttons',
    'carousel',
    'collapse',
    'dropdown',
    'esm',
    'modal',
    'pagination',
    'popover',
    'progressbar',
    'rating',
    'tabset',
    'timepicker',
    'tooltip',
    'typeahead',
    'util'
  ];

  ngBootstrapPackageNames.forEach(function(pkgName) {
    packages['@ng-bootstrap/ng-bootstrap/' + pkgName] = {main: 'index.js', defaultExtension: 'js'};
  });

  var config = {
    transpiler: 'ts',
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },
    map: map,
    packages: packages
  };

  System.config(config);

})(this);

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { Component } from '@angular/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'my-app',
  template:`
<div>
  <ngb-pagination 
    [collectionSize]=30
    [pageSize]=2
    [maxSize]=5
    [boundaryLinks]=true
    [(page)]="page">
  </ngb-pagination>
</div>
<hr>
<ul>
  <li *ngFor="let item of range()">
    <p>{{item.id}}:{{item.content}}</p>
  </li>
</ul>
<hr>
<div>
  <ngb-pagination 
    [collectionSize]=30
    [pageSize]="itemsperpage"
    [maxSize]=5
    [rotate]=true
    [boundaryLinks]=true
    [(page)]="page">
  </ngb-pagination>
</div>
  `
})
export class AppComponent {
  public items = [
    {"id":1,"content": "記事1の中身です。"},
    {"id":2,"content": "記事2の中身です。"},
    :
    {"id":30,"content": "記事30の中身です。"}
  ];
  public page = 4;
  public itemsperpage = 2;
  range() {
    var start = (this.page - 1) * this.itemsperpage;
    var end = start + this.itemsperpage;
    return this.items.slice(start,end);
  };
}

@NgModule({
  imports: [NgbModule,BrowserModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

platformBrowserDynamic().bootstrapModule(AppModule);

関連記事の目次

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です