Show/hide ngFor 中的各种元素 Angular
Show/hide various elements in ngFor Angular
我有一个table,它在ngFor中显示数据。当我删除一个元素时,我想隐藏它。
我想我必须在我的记录组件 hide:boolean
中做一个变量,默认情况下它是 false,但是当我单击 od delete 按钮时,它变为 true。我不知道如何在 table.
中 "catch" 这个变量
<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>
还有我的记录组件:
<td>{{record.id}}</td>
<td>{{record.name}}</td>
<td>{{record.surname}}</td>
<td>
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">
<button type="button" class="btn btn-danger" (click)="removeRecord(record.id)">
<i class="fa fa-trash-o" aria-hidden="true"></i> Remove
</button>
</div>
</td>
我做错了什么?我的数据在 json 文件中,由 json-server 连接。我用 http.delete()
函数删除记录。它从我的文件中删除,但 table 不会自行重新加载。
记录组件,removeRecod函数:
removeRecord(id) {
var url = this.data_url + "/" + id;
this.http.delete(url).subscribe();
this.hide = true;
}
你只有对象的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;
}
});
}
此处有两个基本选项:
将整个记录对象传递给删除函数并将"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);
}
我有一个table,它在ngFor中显示数据。当我删除一个元素时,我想隐藏它。
我想我必须在我的记录组件 hide:boolean
中做一个变量,默认情况下它是 false,但是当我单击 od delete 按钮时,它变为 true。我不知道如何在 table.
<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>
还有我的记录组件:
<td>{{record.id}}</td>
<td>{{record.name}}</td>
<td>{{record.surname}}</td>
<td>
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">
<button type="button" class="btn btn-danger" (click)="removeRecord(record.id)">
<i class="fa fa-trash-o" aria-hidden="true"></i> Remove
</button>
</div>
</td>
我做错了什么?我的数据在 json 文件中,由 json-server 连接。我用 http.delete()
函数删除记录。它从我的文件中删除,但 table 不会自行重新加载。
记录组件,removeRecod函数:
removeRecord(id) {
var url = this.data_url + "/" + id;
this.http.delete(url).subscribe();
this.hide = true;
}
你只有对象的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;
}
});
}
此处有两个基本选项:
将整个记录对象传递给删除函数并将"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); }