简体   繁体   English

回到步进角料的第一步

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

first of all hello to the whole community, I am beginner with angular, this is my first message.首先向整个社区问好,我是 angular 的初学者,这是我的第一条消息。

I encounter the following problem :我遇到以下问题:

I have a stepper with two fields.我有一个带有两个字段的步进器。 Whole stepper整体步进

When I validate the second field, I would like to go back to step1 with the focus enabled like this:当我验证第二个字段时,我想回到 step1 并启用焦点,如下所示:

back stepper 1 with focus带焦点的后退步进器 1

here is my code :这是我的代码:

typsescript :打字稿:

    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 : 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>

Thank you谢谢

Sorry, I found my answer I added抱歉,我找到了我添加的答案

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

it solve the problem它解决了问题

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

相关问题 Angular Material(7.3.7)步进器,不扩展第一步 - Angular Material(7.3.7) stepper without expanding the first step 角形材料 2 步进器 - 下一步 - Angular material 2 stepper - next step 为什么 Angular Material mat-horizontal-stepper 和 mat-stepper,在接下来的步骤中,首先(单击)不起作用? - Why Angular Material mat-horizontal-stepper and mat-stepper, on following step, first (click) does not work? 如何在 Angular Material Stepper 中禁用一个步骤? - How to disable a step in Angular Material Stepper? 每个步骤的角度材料步进组件 - Angular Material Stepper Component For Each Step 如果在上一步中满足条件,如何跳过一个步骤 - Stepper | 角材料 - How to skip a step if a condition is met in a previous step - Stepper | Angular Material 首次加载时未定义角材料步进器 - Angular Material Stepper is undefined on first load Angular - 如何验证 Angular Material stepper 中的每一步 - Angular - How to Validate each step in Angular Material stepper 每个步骤都有单独组件的角度材质步进器 - ExpressionChangedAfterItHasBeenCheckedError - Angular Material Stepper with Separate Component for each step - ExpressionChangedAfterItHasBeenCheckedError 角度材料步进器在每次点击时动态添加新的步骤项目 - angular material stepper add new step items dynamically on every click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM