繁体   English   中英

回到步进角料的第一步

[英]Return to the first step of a stepper angular material

首先向整个社区问好,我是 angular 的初学者,这是我的第一条消息。

我遇到以下问题:

我有一个带有两个字段的步进器。 整体步进

当我验证第二个字段时,我想回到 step1 并启用焦点,如下所示:

带焦点的后退步进器 1

这是我的代码:

打字稿:

    export class ZoneUtilityComponent implements OnInit, AfterViewInit {

  isLinear = true;
  refFormGroup: FormGroup;
  secondFormGroup: FormGroup;
  targetInput='input0';
  displayErrorSnackbar: number = 2;
  refBarCodePattern = "[0-9]{6}";
  stepperOrientation: Observable<StepperOrientation>;

  constructor(
    private formBuilder: FormBuilder,
    breakpointObserver: BreakpointObserver,
    private snackBar: MatSnackBar,
    private rayonService: SharedRayonService
  ) {
    this.stepperOrientation = breakpointObserver.observe('(min-width: 800px)')
      .pipe(map(({matches}) => matches ? 'horizontal' : 'vertical'));
  }

  ngOnInit() {
    this.initRefForm();
  }

  onChange(event: any) {
    console.log("changement activé")
    let index = String(event.selectedIndex);
    this.targetInput = 'input' + index;
    this.setFocus();
  }

  ngAfterViewInit() {
    this.setFocus();
  }

  setFocus() {
    console.log("setfocus")
    let targetElem = document.getElementById(this.targetInput);
    setTimeout(function waitTargetElem() {
      if (document.body.contains(targetElem)) {
        targetElem.focus();
      } else {
        setTimeout(waitTargetElem, 100);
      }
    }, 100);
  }

  initRefForm() {
    this.refFormGroup = this.formBuilder.group({
      reference: ['', [Validators.required, Validators.pattern(this.refBarCodePattern)]]
    });
    this.secondFormGroup = this.formBuilder.group({
      secondCtrl: ['', Validators.required]
    });
    this.onMenu();
  }

  onCheckRef() {
    if (this.refFormGroup.get('reference').invalid) {
      this.openSnackBar();
    }
    this.onMenu();
  }

  openSnackBar() {
    this.snackBar.openFromComponent(ErrorRefComponent, {duration: this.displayErrorSnackbar * 1000});
  }

  onMenu() {
    this.rayonService.obsRef.next(this.refFormGroup.get('reference').value);
 }

}

html :

<app-header-zone></app-header-zone>
<div>
  <mat-stepper class="example-stepper" [linear]="isLinear" #stepper (selectionChange)="onChange($event)" [orientation]="(stepperOrientation | async)!">
  <mat-step [stepControl]="refFormGroup" label="Scannez une référence">
    <form [formGroup]="refFormGroup">
      <div class="flexInput">
        <mat-form-field>
          <!-- <mat-label>Scannez une référence</mat-label> -->
          <input matInput id="input0" formControlName="reference" placeholder="EAN13" required>
        </mat-form-field>
        <div>
          <button mat-button (click)="onCheckRef()" matStepperNext>Suivant</button>
        </div>
      </div>
    </form>
  </mat-step>

  <mat-step [stepControl]="secondFormGroup" label="Scannez une zone">
    <form [formGroup]="secondFormGroup">
      <div class="flexInput">
        <mat-form-field>
          <mat-label>Scannez une zone</mat-label>
          <input matInput id="input0" formControlName="secondCtrl" placeholder="Zonage" required>
        </mat-form-field>
        <div>
          <button mat-button (click)="initRefForm()" matStepperNext>Valider</button>
        </div>
      </div>
    </form>
  </mat-step>
</mat-stepper>
</div>

谢谢

抱歉,我找到了我添加的答案

<button mat-button (click)="stepper.reset()" matStepperNext>Valider</button>
        </div>

它解决了问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM