[英]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.