Angular 9.0 - FormGroup 和 FormArray - 从表中删除行

标签 angular

我曾尝试使用单击按钮来删除行,但它不起作用,而且我不太确定我能做些什么来解决这个问题。

显示的初始 json 是:

{ "list": [ { "description": null, "status": false } ] }

当我点击移除按钮时,显示的json会变成:

{ "list": [] }

并且所选行根本没有被删除。

下面是代码。

todo-list.component.ts

todoListForm: FormGroup;

  constructor(private toDoService: TodoService, private formBuilder: FormBuilder) {
    this.createForm();
  }

  tableData: TableData[] = [
    { id: 'aaaaa', description: 'Go to market', status: true },
    { id: 'bbbbb', description: 'Buy groceries', status: true },
    { id: 'ccccc', description: 'Order pizza delivery', status: false },
  ];

  ngOnInit(): void { }

  createForm() {
    this.todoListForm = this.formBuilder.group({
      list: this.formBuilder.array([])
    });

    this.addList();
  }

  get list(): FormArray {
    return this.todoListForm.get('list') as FormArray;
  }

    addList(): void {
      const addTodoListForm = this.formBuilder.group({
        description: [null, Validators.required],
        status: [false]
      });
  
      this.list.push(addTodoListForm);
  
      console.log('addTodoListForm', addTodoListForm);
      console.log('list', this.list);
    }

  removeRow(i: number): void {
    console.log(this.list);
    this.list.removeAt(i);
  }
}

export interface TableData {
  id: string,
  description: string,
  status: boolean,
}

todo-list-component.html

<h1> To-do List </h1>
{{ todoListForm.value | json}}
<form [formGroup]="todoListForm">
    <div formArrayName="list">
        <div class="tableFlex">
            <table class="table table-striped" table-layout="fixed">
                <tr>
                    <th> #ID </th>
                    <th> Description </th>
                    <th> Action </th>
                </tr>
                <tr *ngFor="let data of tableData, let i = index">
                    <td> {{ i + 1 }} </td>
                    <td> <del *ngIf="data.status === true">{{data.description}}</del>
                        <div *ngIf="data.status === false">{{data.description}}</div>
                    </td>
                    <td>
                        <div class="btn-toolbar pull right">
                            <div class="btn-group mr-2" role="group" aria-label="Mark-as">
                                <button type="button" class="btn btn-secondary" (click)="updateStatus(i, data.status)">
                                    {{ data.status === true ? 'Mark as incomplete' : 'Mark as completed' }} </button>
                            </div>

                            <div class="btn-group mr-2" role="group" aria-label="Remove">
                                <button type="submit" class="btn btn-danger" (click)="removeRow(i)">
                                    Remove
                                </button>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>

最佳答案

通过添加变化检测器引用来尝试这一点

constructor(private readonly changeDetectorRef: ChangeDetectorRef){

}

  removeRow(i: number): void {
    this.list.removeAt(i);
    this.changeDetectorRef.markForCheck();
  }

如果这不起作用,也请尝试 this.detectChanges()。如果这也不起作用,您的更改将超出 ng 区域,那么您可以尝试以下操作

  removeRow(i: number): void {
       this.zone.run(() => this.list.removeAt(i););
      }

也将 Zone 注入(inject)到这个文件中

关于Angular 9.0 - FormGroup 和 FormArray - 从表中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64003106/

相关文章:

angular - 删除空格后,带管道的输入框不会更新值

angular - 如何(模糊) Angular 元素,除非在子元素中单击它

javascript - Angular 2跨应用程序(根组件)通信

angular - 在 Angular 6 中写入文件

css - ng-select 使用图像 url 更改下拉箭头

angular - 这是ionic3的页面跳转

angular - Angular2- Material 2 : Want to setup a fixed sidenav below toolbar

Angular 2 : ngFor index only increment if case met

javascript - Angular 7/1.x 混合应用程序可以支持 HMR 吗?

javascript - ngrx 使用调度时从列表中删除项目