删除选定的行索引

时间:2018-05-24 03:48:03

标签: angular

My Forms

大家好,我在删除问题表中的行表时遇到了一些麻烦。

这是我在component.ts中删除的函数

  deleteQuestions(questionsId: number) {

    this.questionerDetails.removeAt(1);

  }

这是我的组件html

      <!--qeustion input-->
      <div formArrayName="questionerDetails" class="rap">
        <table id="myTable" align="center" class="table table-bordered">
          <tr><th>Questions</th><th>Actions</th></tr>
          <tr *ngFor="let questions of questionerDetails.controls; let i=index" [formGroupName]="i">
            <td><input style="width:100%;" formControlName="questionDetail" class="form-control" placeholder="type your questions here" /></td>
            <td align="center">

              <button (click)="deleteQuestions()" type="button" class="btn btn-success bc"><i class="fa fa-trash"></i></button>

            </td>

          </tr>
        </table>
      </div>

我的麻烦是我只能删除行表的最后一个索引或第一个索引。
示例:我有5个行表[0],1,[2],[3],[4],我想删除行号1该行被删除,之后我删除号码[2]。所以行表有[0],[4] 我选择行时如何删除行? 感谢。

2 个答案:

答案 0 :(得分:0)

您应该通过删除方法传递所选索引

(click)="deleteQuestions(i)"

并且该行给出了所选索引let i=index

您的方法应该使用splice()而不是removeAt()

deleteQuestions(index: number) {
    this.questionerDetails.splice(index, 1);

  }

答案 1 :(得分:0)

首先。
您错过了deleteQuestion()中的索引。这就是为什么你的ts无法找到你想要删除的元素的索引的原因。您需要将其更改为deleteQuestion(i),我是表中元素的索引
二。
您应该使用的方法是Slice(),而不是removeAt()。 这是关于它的文档。玩得开心 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

相关问题