Json 在NGF中显示/隐藏各种元素以获取角度
我有一个表,它以ngFor格式显示数据。当我删除一个元素时,我想隐藏它。 我想我必须在我的记录组件hide:boolean中做一个变量,默认为false,但当我单击od delete按钮时,它会变为true。我不知道如何在我的表中捕捉这个变量Json 在NGF中显示/隐藏各种元素以获取角度,json,angular,html-table,json-server,Json,Angular,Html Table,Json Server,我有一个表,它以ngFor格式显示数据。当我删除一个元素时,我想隐藏它。 我想我必须在我的记录组件hide:boolean中做一个变量,默认为false,但当我单击od delete按钮时,它会变为true。我不知道如何在我的表中捕捉这个变量 <table> <thead> <tr> <th> Id </th> <th> Name
<table>
<thead>
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Surname
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
<tr sl-record *ngFor="let recordElement of records" [record]="recordElement" [hidden]="recordElement.hide"></tr>
</tbody>
</table>
您只有对象的id。这样做:
removeRecord(id: number){
var url = this.data_url + "/" + id;
this.http.delete(url).subscribe();
this.records.forEach(element => {
if(element.id === id){
element.hide = true;
}
});
}
这里有两个基本选项: 将整个记录对象传递给delete函数,并将hide切换为true
removeRecord(record) {
record.hide = true;
let id = record.id;
// do whatever to delete on server
}
最好从列表中删除记录,而不是试图隐藏它
removeRecord(id) {
//do whatever to delete the record on server
let recordIndex = this.records.findIndex(r => r.id === id);
this.records = this.records.splice(recordIndex, 1);
}
我已经有一段时间没有接触过angular了,但这听起来像是可以解决的问题apply@Huangism有什么提示我如何使用这个应用程序吗?我找不到任何关于它的文档…你的RemoveRecord.id方法是什么样子的?您是否将对象的隐藏字段设置为true?@DiabolicWords更新了我的帖子:您只是在删除服务器上的记录。我想,您需要将记录重新分配给删除调用的结果。我在记录到表组件之间创建了一个事件发射器,但您对splice有一个很好的想法,我忘记了它的存在:因此,我采用了与您建议不同的方式创建了它,但您是我的灵感来源:
removeRecord(record) {
record.hide = true;
let id = record.id;
// do whatever to delete on server
}
removeRecord(id) {
//do whatever to delete the record on server
let recordIndex = this.records.findIndex(r => r.id === id);
this.records = this.records.splice(recordIndex, 1);
}