angular - formArray 中的可拖动 formGroups(反应式表单)
问题描述
在角度拖放模块中,他们提供了 moveItemInArray() 函数的文档,通过使用它我们只能拖动数组中的内容。但是,我们如何在 formArray 中洗牌(formGroups/formControls)?
即使我尝试了这个 moveItemInFormArray() 函数,如此处所述https://github.com/angular/angular/issues/27171。但我不能让它工作。
groupDrag.component.html
<form [formGroup]="exampleForm">
<div formArrayName="formUnits" cdkDropList (cdkDropListDropped)="drop($event)" *ngFor="let unit of exampleForm.controls.formUnits.controls; let i=index" class="rowGroup">
<div [formGroupName]="i" class="basic-container" cdkDrag>
<div class="row row-container" >
<button type="button" class="drag-handle" mat-icon-button cdkDragHandle>
<mat-icon>unfold_more</mat-icon>
</button>
<!-- label input field -->
<mat-form-field class="col-lg-4">
<input matInput placeholder="Please enter label without spaces" formControlName="label" required>
</mat-form-field>
<!-- options input field -->
<mat-form-field class="col-lg-3">
<input matInput placeholder="Enter Placeholdertext" formControlName="placeholder">
</mat-form-field>
</div>
</div>
</div>
</form>
groupDrag.component.ts
drop(event: CdkDragDrop<FormGroup[]>) {
console.log('drop event triggers')
this.formArray = this.exampleForm.get('formUnits') as FormArray;
const from = event.previousIndex;
const to = event.currentIndex;
this.moveItemInFormArray(this.formArray, from, to)
}
/**
* Moves an item in a FormArray to another position.
* @param formArray FormArray instance in which to move the item.
* @param fromIndex Starting index of the item.
* @param toIndex Index to which he item should be moved.
*/
moveItemInFormArray(formArray: FormArray, fromIndex: number, toIndex: number): void {
const from = this.clamp(fromIndex, formArray.length - 1);
const to = this.clamp(toIndex, formArray.length - 1);
if (from === to) {
return;
}
const delta = from > to ? 1 : -1;
for (let i = from; i * delta < to * delta; i += delta) {
const previous = formArray.at(i);
const current = formArray.at(i + delta);
formArray.setControl(i, current);
formArray.setControl(i + delta, previous);
}
}
/** Clamps a number between zero and a maximum. */
clamp(value: number, max: number): number {
return Math.max(0, Math.min(max, value));
}
解决方案
这是工作示例:
groupDrag.component.ts
import {moveItemInFormArray} from "./move-item-in-form-array";
drop(event: CdkDragDrop<string[]>) {
moveItemInFormArray(this.arrayControls, event.previousIndex, event.currentIndex);
}
移动项目形式-array.ts
import {FormArray} from '@angular/forms';
/**
* Moves an item in a FormArray to another position.
* @param formArray FormArray instance in which to move the item.
* @param fromIndex Starting index of the item.
* @param toIndex Index to which he item should be moved.
*/
export function moveItemInFormArray(formArray: FormArray, fromIndex: number, toIndex: number): void {
const dir = toIndex > fromIndex ? 1 : -1;
const from = fromIndex;
const to = toIndex;
const temp = formArray.at(from);
for (let i = from; i * dir < to * dir; i = i + dir) {
const current = formArray.at(i + dir);
formArray.setControl(i, current);
}
formArray.setControl(to, temp);
}
推荐阅读
- javascript - 如何找到标签关闭并清除本地存储
- tycho - 将参数“--add-modules”添加到 Tycho 编译器
- java - Intellij Idea 提示“Unknown type Long” Grapqhl 的配置
- mysql - Mysql FIFO消耗
- msbuild - 错误 NU1101:找不到包 ProjectABC.Core.Services。源中不存在具有此 ID 的包
- python - 将 Pandas 数据框中的 JSON 数据转换为数据框本身的最佳方法
- python - 如何在 Python 3 中将列表变成循环列表?
- mysql - 根据 id 列将 MySQL 表从垂直转换为水平
- css - Less - 如何在循环中生成值
- python - gensim doc2vec 模型不学习一些单词