web-dev-qa-db-ja.com

Angular / Angular Materialのmat-h​​orizo​​ntal-stepperのステップをプログラムで移動できますか

Angularマテリアル(Angular 4+)に関する質問があります。コンポーネントテンプレートに<mat-horizontal-stepper>コンポーネントを追加し、各ステップ<mat-step>にコンポーネントをナビゲートするステッパーボタンがあるとしましょう。そのようです...

<mat-horizontal-stepper>
  <mat-step>
    Step 1
    <button mat-button matStepperPrevious type="button">Back</button>
    <button mat-button matStepperNext type="button">Next</button>
  </mat-step>
  <mat-step>
    Step 2
    <button mat-button matStepperPrevious type="button">Back</button>
    <button mat-button matStepperNext type="button">Next</button>
  </mat-step>
  <mat-step>
    Step 3
    <button mat-button matStepperPrevious type="button">Back</button>
    <button mat-button matStepperNext type="button">Next</button>
  </mat-step>
</mat-horizontal-stepper>

今、私は各ステップからボタンを削除し、<mat-horizontal-stepper>の静的な位置または<mat-horizontal-stepper>の外側にボタンを配置することが可能であり、コードを使用して前後にナビゲートできるかどうか疑問に思っていますコンポーネントのTypeScriptファイル。アイデアを与えるために、私は私のHTMLがこのようなものになりたいです

<mat-horizontal-stepper>
    <mat-step>
        Step 1
    </mat-step>
    <mat-step>
        Step 2
    </mat-step>
    <mat-step>
        Step 3
    </mat-step>
    <!-- one option -->
    <div>
       <button mat-button matStepperPrevious type="button">Back</button>
       <button mat-button matStepperNext type="button">Next</button>
    </div>
</mat-horizontal-stepper>

<!-- second option -->
<div>
   <button (click)="goBack()" type="button">Back</button>
   <button (click)="goForward()" type="button">Next</button>
</div>
42
Mike Sav

はい。 selectedIndexMatStepperプロパティを使用すると、特定のステッパーにジャンプできます。また、MatStepperはパブリックメソッドnext()およびprevious()を公開します。それらを使用して前後に移動できます。

テンプレート内:

ステッパーにIDを追加します(例: #stepper。次に、goBack()およびgoForward()メソッドで、ステッパーIDを渡します。

<mat-horizontal-stepper #stepper>
    <!-- Steps -->
</mat-horizontal-stepper>    
<!-- second option -->
<div>
   <button (click)="goBack(stepper)" type="button">Back</button>
   <button (click)="goForward(stepper)" type="button">Next</button>
</div>

..およびTypeScriptで:

import { MatStepper } from '@angular/material/stepper';

goBack(stepper: MatStepper){
    stepper.previous();
}

goForward(stepper: MatStepper){
    stepper.next();
}

stackblitz demo へのリンク。

次を使用して、BackおよびNextボタンを有効/無効にできます。

<button (click)="goBack(stepper)" type="button" 
        [disabled]="stepper.selectedIndex === 0">Back</button>
<button (click)="goForward(stepper)" type="button" 
        [disabled]="stepper.selectedIndex === stepper._steps.length-1">Next</button>
94
Faisal

@ Faisal の答えに加えて、これはステッパーを引数に渡す必要なしにMatStepperをジャンプさせるための私の考えです。

これは、ステッパーをより柔軟に操作する必要がある場合に役立ちます。 Serviceまたは他の何かから。

HTML:

<div fxLayout="row" fxLayoutAlign="center center" fxLayoutGap="6px">
  <button (click)="move(0)">1st</button>
  <button (click)="move(1)">2nd</button>
  <button (click)="move(2)">3rd</button>
  <button (click)="move(3)">4th</button>
</div>

TSファイル:

move(index: number) {
    this.stepper.selectedIndex = index;
}

ここにstackblitz demo

18
Altus

プログラムでナビゲートしたい場合次のステップに進み、リニアステッパーを使用の場合、以下の手順に従います。

  • 次のようなstepperを作成します:<mat-horizontal-stepper linear #matHorizontalStepper>
  • mat-stepを次のように定義します:<mat-step [completed]="isThisStepDone">
  • mat-stepの内部から、次のような次のステップに進むボタンを作成します:<button (click)="next(matHorizontalStepper)">NEXT STEP</button>
  • .tsファイルで、stepperという名前のMatStepper参照を宣言します。
    @ViewChild('matHorizontalStepper') stepper: MatStepper;
  • また、.tsファイル内でisThisStepDonefalseとして初期化します:isThisStepDone: boolean = false;
  • 次に、next()という名前のNEXT STEPボタンのメソッドを記述します。

    submit(stepper: MatStepper) {
     this.isThisStepDone = true;
     setTimeout(() => {           // or do some API calls/ Async events
      stepper.next();
     }, 1);
    }
    
15
BlackBeard

SelectedIndexを使用して、ステッパーの実際のインデックスを指定することでも可能です。

stackblitz: https://stackblitz.com/edit/angular-4rvy2s?file=app%2Fstepper-overview-example.ts

HTML:

<div class="fab-nav-container">
   <mat-vertical-stepper linear="false" #stepper>
       <mat-step *ngFor="let step of stepNodes; let i = index">
           <ng-template matStepLabel>
               <p> {{step.title}} </p>
           </ng-template>
       </mat-step>
   </mat-vertical-stepper>
</div>

<div class="button-container">
   <div class="button-grp">
      <button mat-stroked-button (click)="clickButton(1, stepper)">1</button>
      <button mat-stroked-button (click)="clickButton(2, stepper)">2</button>
      <button mat-stroked-button (click)="clickButton(3, stepper)">3</button>
   </div>
</div>

TS:

import {Component, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import { MatVerticalStepper } from '@angular/material';
import { MatStepper } from '@angular/material';
export interface INodes {
    title: string;
    seq: number;
    flowId: string;
}
/**
 * @title Stepper overview
 */
@Component({
  selector: 'stepper-overview-example',
  templateUrl: 'stepper-overview-example.html',
  styleUrls: ['stepper-overview-example.scss'],
})
export class StepperOverviewExample implements OnInit {
  @ViewChild(MatVerticalStepper) vert_stepper: MatVerticalStepper;
  @ViewChild('stepper') private myStepper: MatStepper;

  stepNodes: INodes[] = [
    { title: 'Request Submission', seq: 1, flowId: 'xasd12'}, 
    { title: 'Department Approval', seq: 2, flowId: 'erda23'}, 
    { title: 'Requestor Confirmation', seq: 3, flowId: 'fsyq51'}, 
  ];

  ngOnInit() {
  }
  ngAfterViewInit() {
    this.vert_stepper._getIndicatorType = () => 'number';
  }
  clickButton(index: number, stepper: MatStepper) {
      stepper.selectedIndex = index - 1;
  }
}
2
M.Laida