移动表格 Up/Down Angular 2

Moving Forms Up/Down Angular 2

我在 Angular 2 中创建了一个动态表单,它在单击按钮时添加了附加项。我现在想实现一个表单 up/down 功能,基本上用 'Move' 按钮替换项目。 以下是我的代码:

component.ts

    public pipeLineForm: FormGroup;
    pipelineConfigs: PipelineConfigs[];

constructor(
    private http: Http,
    private _fb: FormBuilder)

    ngOnInit() {
    // initialize form here
    this.pipeLineForm = this._fb.group({
        pipelineConfigs: this.initPipeLineArray()
    });
}

initPipeLineArray() {
    let pipeArray = this._fb.array([]);
    pipeArray.push(this.initPipeline());
    pipeArray.push(this.initPipeline());
    return pipeArray;
}

initPipeline() {
    return this._fb.group({
       sourceType: ['', Validators.required],
       sourceName: ['', Validators.required],
       sourcePath: ['', Validators.required],
       query: ['', Validators.required]
    });
}


removePipeline(i: number) {
    if (i > 1) {
    const control = <FormArray>this.pipeLineForm.controls['pipelineConfigs'];
    control.removeAt(i);
    }
}

addNewPipelineConfigs() {
    const control = <FormArray>this.pipeLineForm.controls['pipelineConfigs'];
    control.push(this.initPipeline());
}

pipelineconfigs.ts

    export class PipelineConfigs {
    searchField: string;
    sourceType: string;
    sourceName: string;
    sourcePath: string;
    query: string;
}

html

  <div class="panel panel-default clearfix" [formGroup] = "pipeLineForm">
            <div class="row"  >
                <div class="col-xs-12">
                    <p class="margin-b20px">This is the base line item and you can change it using up &amp; down arrows.
                    </p>
                </div>
            </div>
            <div formArrayName="pipelineConfigs">
            <div class="row" *ngFor="let pipes of pipeLineForm.controls.pipelineConfigs.controls; let i=index;" >
                <div [formGroupName]="i">
                    <div class="col-xs-10 additional-details-fields">
                    <div class="row">
                        <div class="col-xs-4">
                            <label class="float-label" [class.empty]="sourceType1 ==''">
                                <span class="placeholder">Select Source Type
                                    <span class="hide-placeholder">&nbsp;</span>
                                </span>
                                <select formControlName="sourceType">
                                    <option disabled>Select Source Type</option>
                                    <option value="Stream">Stream</option>
                                    <option value="Stream 1">Stream 1</option>
                                    <option value="Stream 2">Stream 2</option>
                                </select>
                            </label>
                        </div>
                        <div class="col-xs-4 form-group">
                            <label class="float-label" [class.empty]="sourceName1.length==0">
                                <span class="placeholder">Source Name
                                    <span class="hide-placeholder">&nbsp;</span>
                                </span>
                                <input formControlName="sourceName" type="text">
                            </label>
                        </div>
                        <div class="col-xs-4 form-group">
                            <label class="float-label" [class.empty]="sourcePath1.length==0">
                                <span class="placeholder">Source Path
                                    <span class="hide-placeholder">&nbsp;</span>
                                </span>
                                <input formControlName="sourcePath" type="text">
                            </label>
                        </div>
                        <div class="col-xs-12 form-group">
                            <label class="float-label" [class.empty]="query1.length==0">
                                <span class="placeholder">Query
                                    <span class="hide-placeholder">&nbsp;</span>
                                </span>
                                <textarea formControlName="query">{{query}}</textarea>
                            </label>
                        </div>
                    </div>
                    </div>

                <div class="col-xs-2 additional-details-actions">
                    <div class="col-xs-4">
                        <label class="label-text block">Order</label>
                        <label class="label-value block">{{i}}
                        </label>
                    </div>
                    <div class="col-xs-4">
                        <div class="row">
                            <label class="label-text block">Move</label>
                            <div>
                                <a class="clickable actionbtn order-up">Order Up</a>
                                <a class="clickable actionbtn order-down">Order Down</a>
                            </div>
                        </div>
                    </div>

我基本上已经确定 pipelineConfigs 是一个对象数组,我需要以某种方式更改项目的索引但是因为我是 angular 的新手打字稿,我无法弄清楚如何将其放入代码中。 如果有人能提供帮助,我会很高兴。

谢谢

你必须

  1. 定义 2 种方法:moveUpmoveDown
  2. 定义一个 swap 函数来逐个交换数组中的项目

这是一个简单的例子,demo

component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  array = ["One", "Two", "Three", "Four", "Five"]

  moveUp(index: number) {
    console.log("up", this.array[index]);
    if (index >= 1)
      this.swap(index, index - 1)
  }

  moveDown(index: number) {
    console.log("down", this.array[index])
    if(index < this.array.length-1)
    this.swap(index, index + 1)
  }

  private swap(x: any, y: any) {
    var b = this.array[x];
    this.array[x] = this.array[y];
    this.array[y] = b;
  }
}

component.html

<div *ngFor="let item of array; let i=index">
  <span>{{item}}</span>
  <button (click)="moveUp(i)">Move up</button>
  <button (click)="moveDown(i)">Move down</button>
</div>