<!DOCTYPE html>
<html>

  <head>
    <base href="." />
    <script type="text/javascript" charset="utf-8">
      window.AngularVersionForThisPlunker = 'latest'
    </script>
    <title>Angularページングサンプル</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" />
		<style>html{font-size:14px;}</style>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="style.css" />
    <script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
    <script src="https://unpkg.com/zone.js/dist/zone.js"></script>
    <script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
    <script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
    <script src="config.js"></script>
    <script>
    System.import('app')
      .catch(console.error.bind(console));
  </script>
  </head>

  <body>
    <my-app>
    loading...
  </my-app>
  </body>

</html>
/* Styles go here */

var angularVersion;
if(window.AngularVersionForThisPlunker === 'latest'){
  angularVersion = ''; //picks up latest
}
else {
  angularVersion = '@' + window.AngularVersionForThisPlunker;
}

System.config({
  //use typescript for compilation
  transpiler: 'typescript',
  //typescript compiler options
  typescriptOptions: {
    emitDecoratorMetadata: true
  },
  paths: {
    'npm:': 'https://unpkg.com/'
  },
  //map tells the System loader where to look for things
  map: {
    
    'app': './src',
    '@angular/core': 'npm:@angular/core'+ angularVersion + '/bundles/core.umd.js',
    '@angular/common': 'npm:@angular/common' + angularVersion + '/bundles/common.umd.js',
    '@angular/common/http': 'npm:@angular/common' + angularVersion + '/bundles/common-http.umd.js',
    '@angular/compiler': 'npm:@angular/compiler' + angularVersion  + '/bundles/compiler.umd.js',
    '@angular/platform-browser': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser.umd.js',
    '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic.umd.js',
    '@angular/http': 'npm:@angular/http' + angularVersion + '/bundles/http.umd.js',
    '@angular/router': 'npm:@angular/router' + angularVersion +'/bundles/router.umd.js',
    '@angular/forms': 'npm:@angular/forms' + angularVersion + '/bundles/forms.umd.js',
    '@angular/animations': 'npm:@angular/animations' + angularVersion + '/bundles/animations.umd.js',
    '@angular/platform-browser/animations': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-animations.umd.js',
    '@angular/animations/browser': 'npm:@angular/animations' + angularVersion + '/bundles/animations-browser.umd.js',
    
    '@angular/core/testing': 'npm:@angular/core' + angularVersion + '/bundles/core-testing.umd.js',
    '@angular/common/testing': 'npm:@angular/common' + angularVersion + '/bundles/common-testing.umd.js',
    '@angular/common/http/testing': 'npm:@angular/common' + angularVersion + '/bundles/common-http-testing.umd.js',
    '@angular/compiler/testing': 'npm:@angular/compiler' + angularVersion + '/bundles/compiler-testing.umd.js',
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-testing.umd.js',
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic-testing.umd.js',
    '@angular/http/testing': 'npm:@angular/http' + angularVersion + '/bundles/http-testing.umd.js',
    '@angular/router/testing': 'npm:@angular/router' + angularVersion + '/bundles/router-testing.umd.js',
    'tslib': 'npm:tslib@1.6.1',
    'rxjs': 'npm:rxjs',
    'typescript': 'npm:typescript@2.2.1/lib/typescript.js'
  },
  //packages defines our app package
  packages: {
    app: {
      main: './main.ts',
      defaultExtension: 'ts'
    },
    rxjs: {
      defaultExtension: 'js'
    }
  }
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { PagerComponent } from './pager.component';
import { AppService } from './app.service';

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ AppComponent, PagerComponent ],
  providers: [ AppService ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}
import { Component } from '@angular/core';
import { AppService } from './app.service';
import { PagerComponent } from './pager.component';

@Component({
  selector: 'my-app',
  template: `
<div class="container">
  <pager [itemCount]="100" [(pageIndex)]="pageIndex" [pageSize]="pageSize"></pager>
  <table class="table table-sm">
    <thead>
      <tr>
        <th *ngFor="let fieldName of fieldNames">{{fieldName}}</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let item of items | slice:pageIndex*pageSize:(pageIndex+1)*pageSize">
        <td *ngFor="let name of fieldNames">{{item[name]}}</td>
      </tr>
    </tbody>
  </table>
</div>
  `,
})
export class AppComponent {
  items: object[];
  fieldNames: string[];
  pageIndex: number;
  pageSize: number;

  constructor(appService: AppService) {
    this.items = appService.getOrders(100);
    this.fieldNames = [ 'No', '商品名', '受注日', '金額' ];
    this.pageIndex = 0;
    this.pageSize = 10;
  }
}
import { Injectable } from '@angular/core';

const _years: number = 5;
const _productNames: string[] = '果汁100%オレンジ,果汁100%グレープ,果汁100%レモン,果汁100%ピーチ,コーヒーマイルド,コーヒービター,コーヒーミルク,ピリピリビール,オタル白ラベル,バードワイン,ホワイトソルト,ブラックペッパー,ピュアシュガー,うまい素,ピュアデミグラスソース,だしかつお,だしこんぶ,ピリカラタバスコ,のり山椒,特製和風醤油,バニラクリームアイス,チョコクリームアイス,紅茶バー,じゃがチップス,アメリカンクラッカー,バナナキャンディー,メロンミルクキャンディー,小倉あんぱん,インドカレーパン,チーズあんぱん,ロッキーチーズ,パルメザンチーズ,フレッシュバター,ライフマーガリン,ローカロリー牛乳,牛乳マイルド,ストロベリーヨーグルト,ブルーベリーヨーグルト,ラズベリーヨーグルト,ココナッツミルク,モーニングブレッド,バタートースト,バケットフランス,極上パスタ,極上マカロニ,伝統スパゲッティ,ヘルシークラッカー,コーンフレークプレーン,コーンフレークチョコ,コーンフレークシュガー,アメリカンポーク,うす味ウインナー,ベターローストハム,ベタープレスハム,ベター生ハム,特製サラミ,和風ハンバーグレトルト,照焼きミートボール,ミックスハム,ミートバー,もめんどうふ特上,きぬごしどうふ特上,冷凍ミックスベジタブル,冷凍クリームコロッケ,冷凍コーンクリームコロッケ,冷凍ポテトコロッケ,冷凍枝豆,冷凍やきおにぎり,乾燥バナナ,乾燥アップル,特選味のり,北海道昆布,やきいかするめくん,食卓わかめ,ふりかけかつお風味,ふりかけ鮭風味,大陸サーモン,特選にぼし,本がつお特上,ころもはんぺん'.split(',');

@Injectable()
export class AppService {
  getOrders(length = 5): object[] {
    let orders: object[] = [];
    for (let i = 0; i < length; i++) {
      let date = new Date();
      orders.push({
        No: i + 1,
        商品名: this.randomArray(_productNames),
        受注日: this.addDays(date, -this.random(30)).toLocaleDateString(),
        金額: this.random(500, 10) * 100
      });
    }
    return orders;
  }

  random(max: number, min = 0): number {
    return Math.floor(Math.random() * (max - min)) + min;
  }

  randomArray(items: any[]): any {
    return items[this.random(items.length)];
  }

  addDays(value: Date, days: number): Date {
    return new Date(value.getFullYear(), value.getMonth(), value.getDate() + days);
  }
}
import { Component, EventEmitter } from '@angular/core';

@Component({
  selector: 'pager',
  inputs: [ 'itemCount', 'pageSize', 'pageIndex' ],
  outputs: [ 'pageIndexChange' ],
  template: `
<div class="btn-group" role="group">
  <button class="btn btn-secondary" [disabled]="!canMoveToPreviousPage" (click)="moveToFirstPage()"><i class="fa fa-angle-double-left"></i></button>
  <button class="btn btn-secondary" [disabled]="!canMoveToPreviousPage" (click)="moveToPreviousPage()"><i class="fa fa-angle-left"></i></button>
  <button class="btn btn-secondary" disabled style="width:100px">{{pageIndex + 1}} / {{_pageCount}}</button>
  <button class="btn btn-secondary" [disabled]="!canMoveToNextPage" (click)="moveToNextPage()"><i class="fa fa-angle-right"></i></button>
  <button class="btn btn-secondary" [disabled]="!canMoveToNextPage" (click)="moveToLastPage()"><i class="fa fa-angle-double-right"></i></button>
</div>
  `,
})
export class PagerComponent {
  constructor() {
    this.pageSize = 1;
  }

  _itemCount: number;
  get itemCount() {
    return this._itemCount;
  }
  set itemCount(value) {
    this._itemCount = value;
    this.updatePageCount();
  }

  _pageSize: number;
  get pageSize() {
    return this._pageSize;
  }
  set pageSize(value) {
    this._pageSize = value;
    this.updatePageCount();
  }

  _pageCount: number;
  updatePageCount() {
    this._pageCount = Math.ceil(this.itemCount / this.pageSize);
  }

  _pageIndex: number;
  pageIndexChange = new EventEmitter();
  get pageIndex() {
    return this._pageIndex;
  }
  set pageIndex(value) {
    this._pageIndex = value;
    this.pageIndexChange.emit(this.pageIndex);
  }

  get canMoveToNextPage() : boolean {
    return this.pageIndex < this._pageCount - 1 ? true : false;
  }

  get canMoveToPreviousPage() : boolean {
    return this.pageIndex >= 1 ? true : false;
  }

  moveToNextPage() {
    if (this.canMoveToNextPage) {
      this.pageIndex++;
    }
  }

  moveToPreviousPage() {
    if (this.canMoveToPreviousPage) {
      this.pageIndex--;
    }
  }

  moveToLastPage() {
    this.pageIndex = this._pageCount - 1;
  }

  moveToFirstPage() {
    this.pageIndex = 0;
  }
}