删除绑定数组中的一行后刷新表?

时间:2019-04-29 08:52:45

标签: html angular typescript

我正在尝试在我的网站上创建用于用户管理的表。该表工作正常,现在我正尝试在每行上添加按钮,以便删除行。

但是,当我连续单击一个按钮时,没有任何反应。使用调试器,我可以看到绑定到表的数组的行已被删除,但表始终显示该行。

HTML:

<tr *ngFor="let user of users |  filter : usersProperties : searchString; let id = index">
          <th class='UId'>{{user.uId}}</th>
          <th class='Username' contenteditable="true" (keyup)="changeValue(id, 'name', $event)" (blur)="updateList(id, 'name', $event)">{{user.userName}}</th>
          <th class='Prenom' contenteditable="true" (keyup)="changeValue(id, 'name', $event)" (blur)="updateList(id, 'name', $event)">{{user.uFirstName}}</th>
          <th class='Nom' contenteditable="true" (keyup)="changeValue(id, 'name', $event)" (blur)="updateList(id, 'name', $event)">{{user.uLastName}}</th>
          <th class='Admin' contenteditable="true" (keyup)="changeValue(id, 'name', $event)" (blur)="updateList(id, 'name', $event)">{{user.uIsAdmin}}</th>
          <th class='suppr'>
            <span class="table-remove">    
              <!-- Her it's the butto for delete the row -->
              <button type="button" class='btn btn-outline-danger my-2 my-sm-0 shadow-sm' (click)="remove(id)">Remove</button>
            </span>
          </th>
        </tr>

TypeScript:

remove(id: number) {
    this.deleteddrow = []
    if(this.users[id] != null){
      this.deleteddrow.push(this.users[id]);
      this.users.splice(id, 1);
    }
  }

(我将删除的行保留在数组中以更新数据库)

编辑: 用户对象使用模型:

export class UserModel{
    uId : Number
    userName: string
    uFirstName: string
    uLastName: string
    uPassword: string
    uIsAdmin: boolean
}

1 个答案:

答案 0 :(得分:3)

您需要使用pipe transform()进行触发。尝试交换以下内容,重新分配应调用它。

this.users.splice(id, 1);

用于以下内容。您可能不应该将索引作为id传递,但是下面的内容应该有所帮助。

this.users = this.users.filter(data => data !== this.users[id]);

与执行此操作相同,但如上所述应触发transform()

let x = [1, 2, 3];

x = x.filter(data => data !== x[1]);
console.log(x);