前提・実現したいこと
当方Angularの初心者です。
ボタンを押すことでウインドウ内の数値を増減させたいのですがうまくいかずに困っております。
また、子コンポーネントから親コンポーネントにデータを渡すときに
親コンポーネント内で設定されているmaxPage(今回は2とする)を超えない範囲で値を増減させたいです。今回の例でいうと
・フッター内の表示数値を[1,2]で変動させたい
・フッター内の表示数値が1のときに戻るボタンを押しても何も起こらないようにする
・フッター内の表示数値が2のときに進むボタンを押しても何も起こらないようにする
を条件に加えたいと考えています。
発生している問題・エラーメッセージ
子コンポーネント
child.component.ts
typescript
1import { 2 Component, 3 EventEmitter, 4 Input, 5 OnInit, 6 Output, 7 SimpleChanges, 8} from '@angular/core'; 9 10@Component({ 11 //省略 12}) 13 14export class ChildComponent implements OnInit { 15 /**1ページ目へ戻る */ 16 @Output() goFirst = new EventEmitter(); 17 18 /** 前ページへ戻る*/ 19 @Output() goPrevious = new EventEmitter(); 20 21 /** 次ページへ進む */ 22 @Output() goNext = new EventEmitter(); 23 24 /** 最終ページへ進む */ 25 @Output() goLast = new EventEmitter(); 26 27 /** 現在ページ */ 28 @Input() nowPage: number = 1; 29 @Output() nowPageChange = new EventEmitter<number>(); 30 31 inputPage: number = 1; 32 33 /** 最大ページ */ 34 @Input() maxPage: number = 100; 35 36 constructor() {} 37 38 ngOnInit(): void { 39 this.inputPage = this.currentPage; 40 } 41 42//ページが変更されるときに実行する 43pageNumberChange() { 44 if (入力された値が数字ではない) { 45 //省略 46 } else { 47 //メインの処理 48 this.nowPage = this.inputPage; 49 this.nowPageChange.emit(this.nowPage); 50 } 51 }
child.component.html
html
1<mat-card class="paging-area"> 2 <button mat-icon-button (click)="goFirst.emit()"> 3 <mat-icon>first_page</mat-icon> 4 </button> 5 <button mat-icon-button (click)="goPrevious.emit()"> 6 <mat-icon>navigate_before</mat-icon> 7 </button> 8 <input 9 type="text" 10 mat-input 11 style="" 12 [(ngModel)]="inputPage" 13 (change)="pageNumberChange()" 14 /> 15 <span>of</span> 16 <span>{{ maxPage }}</span> 17 <button mat-icon-button (click)="goNext.emit()"> 18 <mat-icon>navigate_next</mat-icon> 19 </button> 20 <button mat-icon-button (click)="goLast.emit()"> 21 <mat-icon>last_page</mat-icon> 22 </button> 23</mat-card>
このhtmlで表示されるものが以下になります(表の中身は例)
フッター部分の左右ボタンを押すことでウインドウ内の数値が変化するようにしているつもりなのですがうまくいっていない状況です(最右の 最終ページへ進むボタン や右から2番目の 1ページ先へ進む ボタンを押してもウインドウ内の数値が変更されない)
右部(左部)にあるボタンを押すことでウインドウ内の数値が下記画像のように増加(減少)するようにしたいです
該当のソースコード
親コンポーネント
parent.component.ts
typescript
1 /** 2 * 3 * @param value 入力されたページへ移動する 4 */ 5 changePage(value: number) { 6 this.currentPage = value; 7 this.offset = 30 * (this.currentPage - 1); 8 //Ag-gridの更新処理(省略) 9 this.chinldComponent.pageNumberChange(); 10 console.log(`move to ${value}`); 11 } 12 13 /** 14 * 検索結果の1ページ目へ移動する 15 */ 16 first() { 17 this.currentPage = 1; 18 //Ag-gridの更新処理(省略) 19 this.chinldPaginationComponent.pageNumberChange(); 20 console.log('first: OK.'); 21 } 22 23 /** 24 * 検索結果の表示を1ページ前へ戻る 25 */ 26 prev() { 27 this.currentPage -= 1; 28 //Ag-gridの更新処理(省略) 29 this.chinldPaginationComponent.pageNumberChange(); 30 console.log('prev: OK.'); 31 } else console.log('prev: NG. currentPage = ' + this.currentPage); 32 } 33 34 /** 35 * 検索結果の表示を1ページ次へ進む 36 */ 37 next() { 38 this.currentPage += 1; 39 //Ag-gridの更新処理(省略) 40 this.chinldPaginationComponent.pageNumberChange(); 41 } else console.log('next: NG. currentPage = ' + this.currentPage); 42 } 43 44 /** 45 * 検索結果の表示を最終ページまで移動する 46 * this.maxPage: 今回は値「2」とする 47 */ 48 last() { 49 if (this.currentPage != this.maxPage) { 50 this.currentPage = this.maxPage; 51 //Ag-gridの更新処理(省略) 52 this.chinldPaginationComponent.pageNumberChange(); 53 console.log('last: OK.'); 54 } else 55 console.log( 56 'last: NG. currentPage = ' + 57 this.currentPage + 58 ' maxPage = ' + 59 this.maxPage 60 ); 61 }
parent.component.html
html
1<ag-grid-angular 2 #sampleGrid 3 [columnDefs]="columnDefs" 4 class="ag-theme-alpine" 5 style="width: 100%; height: calc(100% - 70px - 50px)" 6 [rowData]="rowData" 7 [defaultColDef]="defaultColDef" 8></ag-grid-angular> 9 10<grid-pagination 11 [currentPage]="currentPage" 12 [maxPage]="maxPage" 13 (toFirst)="first()" 14 (toPrevious)="prev()" 15 (toNext)="next()" 16 (toLast)="last()" 17 (currentPageChange)="changePage($event)" 18> 19</grid-pagination>
試したこと
一通りAngularの日本語ドキュメンテーション(https://angular.jp/guide/component-overview)やブログ記事(https://www.yoheim.net/blog.php?q=20180311)を確認したのですがやりたいことが書いてある記事を見つけられませんでした
補足情報(FW/ツールのバージョンなど)
Package Version
@angular-devkit/architect 0.1202.4
@angular-devkit/build-angular 12.2.4
@angular-devkit/core 12.2.4
@angular-devkit/schematics 12.2.4
@schematics/angular 12.2.4
rxjs 7.3.0
typescript 4.2.4
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。