使用formArray进行角度拖放

时间:2019-02-15 15:20:17

标签: angular angular-material angular-cdk angular-dragdrop angulardraganddroplists

我有2个数组。我正在实现一个Angular拖放操作,并且我想使用FormArray保存将元素放入其中的数组。

问题是我无法将formcontrol应用于div,因为它会给出错误

  

错误:名称为'language'的表单控件没有值访问器

这是html         

      <div>

        <div class="example-container">
          <h2>Selected Languages</h2>
          <div
          cdkDropList
          #todoList="cdkDropList"
          [cdkDropListData]="anotherarray"
          [cdkDropListConnectedTo]="[doneList]"
          class="example-list"
          (cdkDropListDropped)="drop($event)"
           formControlName="language">
          <div class="list-group-item list-group-item-action " *ngFor="let item of anotherarray" cdkDrag>
            {{item}}
          </div>
        </div>
      </div>

      <div class="example-container">
        <h2>Available Languages</h2>

        <div
        cdkDropList
        #doneList="cdkDropList"
        [cdkDropListData]="testingarray"
        [cdkDropListConnectedTo]="[todoList]"
        class="example-list"
        (cdkDropListDropped)="drop($event)">
        <div class="list-group-item list-group-item-action " *ngFor="let item of testingarray" cdkDrag>{{item}}</div>
      </div>
      </div>

      </div>

      <button type="submit" class="btn btn-primary my-2" translate>saveButtonLabel
        <fa-icon *ngIf="saveIcon" [icon]="saveIcon" [spin]="saveIcon.iconName === 'spinner'"></fa-icon>
      </button>
    </form>

2 个答案:

答案 0 :(得分:2)

Angular Material拖放API具有moveItemInArray函数,如here

这仅支持常规数组,但根据pierNik的回答here on StackOverflow,您可以使用以下函数复制FormArray的功能:

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 item = formArray.at(fromIndex);
  for (let i = fromIndex; i * dir < toIndex * dir; i = i + dir) {
    const current = formArray.at(i + dir);
    formArray.setControl(i, current);
  }
  formArray.setControl(toIndex, item);
}

然后在您的调用代码中,传递formArray代替普通数组。

get formControls(): FormArray {
    return this.form.get('arrayName') as FormArray;
}

constructor() {
    this.form = this.formBuilder.group({
      arrayName: this.formBuilder.array([]),
    });
}

drop(event: CdkDragDrop<string[]>) {
    moveItemInFormArray(
      this.formControls,
      event.previousIndex,
      event.currentIndex
    );
  }

请参阅工作中的Stackblitz here

答案 1 :(得分:0)

当前,我们无法将formControl与Angular Drag and Drop一起使用,因为它可与div一起使用,并且无法向其中添加formContol。因此,我们需要使用事件cdkDropListDropped每次删除项目时手动更新我们的模型。