javascript - Angular,如何通过单击按钮隐藏表格行

标签 javascript angular

我有一个表,其中每一行都有一个删除按钮。单击按钮后,数据将被删除。但是,要验证记录是否已删除,我必须刷新页面。我想在单击删除按钮时隐藏当前行。这是我的代码。

<table>
    <tr>
        <th>Delete</th>
        <th>Id</th>
        <th>Name</th>
    </tr>
    <tr *ngFor="let person of people" *ngIf="!hideRow">
        <td><button (click)="delete(person.id)" title="Delete">Delete</button></td>
        <td>person.Id</td>
        <td>person.Name</td>
    </tr>
</table>

在我的 component.ts 中,删除时我更改 hideRow 的值

delete(id) {  
  this.hideTr = true;
  this.personService.delete(id).subscribe(p=> console.log(p));
}

hideRow 是一个 bool 变量,默认值为 false。问题是,当我单击删除时,所有行都会被隐藏(当然)。我怎样才能只引用当前行?

最佳答案

当你想删除该行时,你应该实际删除它,而不是隐藏行。不需要 *ngIf="!hideRow"。你不需要刷新页面,这就是 AngularJS 的美妙之处。下面是删除特定行的代码。传递行的 $index:

HTML 代码:

<table>
    <tr>
        <th>Delete</th>
        <th>Id</th>
        <th>Name</th>
    </tr>
    <tr *ngFor="let person of people">
        <td><button (click)="delete($index)" title="Delete">Delete</button></td>
        <td>person.Id</td>
        <td>person.Name</td>
    </tr>
</table>

JavaScript 代码:

// delete row
$scope.delete = function(index) {
    $scope.people.splice(index, 1);
};

关于javascript - Angular,如何通过单击按钮隐藏表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901964/

相关文章:

javascript - jQuery 缓存选择器无法正常工作

angular - 在 Angular 2+ 中绑定(bind) translateX() 变换

javascript - DIV 使用事件处理程序更改颜色 "click"

javascript - 如何在 jQuery 中创建显示/隐藏循环?

html - 如何定位特定的 html 元素以在 CSS 中进行编辑

python - Django + Angular2 : How to fetch data from database?

angular - FormGroup 和 FormArray 有什么区别?什么时候用什么?

javascript - Angular 延迟加载模块错误 - 'RouterModule.forRoot() called twice'

javascript - 使用Java在表单Load上加载select html中的查询结果

javascript - 有没有更简洁的方法来解析我的 HTML 数据?