简体   繁体   English

从 ng-bootstrap 表中删除行

[英]Delete row from ng-bootstrap table

I have successfully implemented ng-bootstrap table complete example.我已经成功实现了 ng-bootstrap 表完整示例。
Object deletion from DOM and database are working, but I can't find a way to delete row from view.从 DOM 和数据库中删除对象正在工作,但我找不到从视图中删除行的方法。 In order to see the changes page reload is required.为了查看更改页面,需要重新加载。 Please notice, that delete function is and should be triggered, from ng-bootstrap modal dialog confirm button.请注意,删除功能是并且应该从 ng-bootstrap 模态对话框确认按钮触发。

I can't call data$ from the for loop like in the bellow approach or similar ones, because todo (or what ever) is observable todos$ ,我不能像波纹管方法或类似方法那样从for循环中调用data$ ,因为todo (或任何东西)是可观察的todos$

<!-- html -->
<tr *ngFor="let todo of tableService.todos$ | async;">
// typescript
deleteRow(id){
  for(let i = 0; i < this.data.length; ++i){
    if (this.data[i].id === id) {
        this.data.splice(i,1);
    }
  }
}

Could someone point me to the right direction, please.请有人指出我正确的方向。

I have this chunk of code:我有这块代码:

deleteTodo(id: string) {
  this.todoService.deleteTodo(id)
    .subscribe(data => {
        console.log(data); // print message from server
    },
        error => console.log(error)
    );
  this.tableService.todoArray = this.tableService.todoArray.filter(elem => elem._id !== id);
  this.todoLength--;
  this.modalService.dismissAll();
  console.log('filtered array: ' + this.tableService.todoArray.length);
  console.log('id: ' + id);
}

This function deletes todo item from database and filter method deletes todo from an array.此函数从数据库中删除待办事项,过滤方法从数组中删除待办事项。 Please look at the screenshot bellow.请看下面的截图。

在此处输入图片说明

repo to my app source code:回购到我的应用程序源代码:
https://github.com/SrdjanMilic/NG-Bootstrap-Todo-list https://github.com/SrdjanMilic/NG-Bootstrap-Todo-list

Angular change detection doesn't work for splice because it does not change the reference to the array variable.角度变化检测不适用于splice因为它不会更改对数组变量的引用。 You need either to update the variable reference (example below) or trigger change detection manually .您需要更新变量引用(下面的示例)或手动触发更改检测

deleteRow(id) {
   this.data = this.data.filter(elem => elem.id !== id);
}

This is the code that works:这是有效的代码:

todo-list.component.ts

export class TodoListComponent implements OnInit {
  todos$: Observable<Todo[]>;
  total$: Observable<number>;

  @ViewChildren(NgbdSortableHeader) headers: QueryList<NgbdSortableHeader>;

  constructor(private todoService: TodoService, private router: Router, private modalService: NgbModal,
              public tableService: TableService, public updateTodoComponent: UpdateTodoComponent,
              public myBootstrapModalCoomponent: MyBootstrapModalComponent, private ref: ChangeDetectorRef) {
    this.todos$ = this.tableService.todos$;
    this.total$ = this.tableService.total$;
  }

...

deleteTodo(id: string) {
  this.todoService.deleteTodo(id)
    .subscribe(todo => {
      console.log(todo); // print message from server
    },
      error => console.log(error)
    );
  this.todos$.subscribe(todos => {
    for (let i = 0; i < todos.length; i++) {
      if (todos[i]._id === id) {
        todos.splice(i, 1);
      }
    }
  });
  this.tableService.todoArray.length--;
  this.modalService.dismissAll();
}

table.service.ts

...
private _todos$ = new BehaviorSubject<Todo[]>([]);

get todos$() {
  return this._todos$.asObservable();
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM