gpt4 book ai didi

angular - 如何删除表应用程序 Angular 2 中的任何行?

转载 作者:太空狗 更新时间:2023-10-29 18:17:12 29 4
gpt4 key购买 nike

我不会用这种方法删除我表中的任何行:

  deleteRowAdressForm(){
this.rowDataMainForm.splice(1, 1);
}

rowDataMainForm = [{
name: '',
email: '',
tel: '',
adress: '',
delete: '',
}];

此方法删除行中的第一个元素,我不会删除表中的任何行

更新并使用 ChangeDetectorRef 后,但这还不起作用

完整代码:

import {Component, OnInit, Input, ChangeDetectorRef} from '@angular/core';


@Component({
selector: 'tabletree',
templateUrl:'app/tabletree.component.html',
})
export class TableTreeComponent{

constructor(private changeDetectorRef: ChangeDetectorRef){

}


rowDataMainForm = [{
name: '',
email: '',
tel: '',
adress: '',
delete: '',
}];

rowDataAdressForm = [{
country: '',
city: '',
zipcode: '',
street: '',
delete: '',
}];

rowDataHomeForm = [{
home: '',
campus: '',
province: '',
area: '',
delete: '',
}];

addRowMainForm(mainFormIndex){
this.rowDataMainForm.push({
name: '',
email: '',
tel: '',
adress: '',
delete: '',
})
}

addRowAdressForm(){
this.rowDataAdressForm.push({
country: '',
city: '',
zipcode: '',
street: '',
delete: '',
})
}

addRowHomeCampusProvinceAreaForm(){
this.rowDataHomeForm.push({
home: '',
campus: '',
province: '',
area: '',
delete: '',
})
}

deleteRowAdressForm(addressFormIndex: number){
this.rowDataMainForm.splice(addressFormIndex, 1);
this.changeDetectorRef.detectChanges();
}

deleteRowStreetCityZipcodeForm(delRowStCZFormIndex: number){
this.rowDataAdressForm.splice(delRowStCZFormIndex, 1);
this.changeDetectorRef.detectChanges();
}

deleteRowHomeForm(homeFormIndex: number){
this.rowDataHomeForm.splice(homeFormIndex, 1);
this.changeDetectorRef.detectChanges();
}

}

和这个 html

<div class="panel panel-default">
<div class="panel-heading text-center">
<h4 class="panel-title">
Подразделение
</h4>
</div>
<table class="table table-bordered">
<tr>
<th>name</th>
<th>email</th>
<th>tel</th>
<th>adress</th>
<th>delete</th>
</tr>
<tr *ngFor="let row of rowDataMainForm; let mainFormIndex = index">
<td><input type="text" class="form-control"></td>
<td><input type="text" class="form-control"></td>
<td><input type="text" class="form-control"></td>
<td>
<div class="panel panel-default smaller">
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>country</th>
<th>city</th>
<th>zipcode</th>
<th>street</th>
<th></th>
</tr>
</thead>
<tr *ngFor="let newrow of rowDataAdressForm; let addressFormIndex = index">
<td></td>
<td></td>
<td></td>
<td>
<div class="panel panel-default smaller">
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>home</th>
<th>campus</th>
<th>province</th>
<th>area</th>
<th></th>
</tr>
</thead>
<tr *ngFor="let suchnewrow of rowDataHomeForm; let homeFormIndex = index">
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<button (click)="deleteRowHomeForm(homeFormIndex)" type="button" class="btn btn-default" style="padding: 2px">Delete</button>
</td>
</tr>
</table>
<div class="panel-footer">
<div class="container-build">
<button (click)='addRowHomeCampusProvinceAreaForm()' type="button" class="btn btn-default" style="padding: 2px">Add row</button>
</div>
</div>
</div>
</td>
<td>
<button (click)='deleteRowStreetCityZipcodeForm(delRowStCZFormIndex)' type="button" class="btn btn-default" style="padding: 2px">Delete</button>
</td>
</tr>
</table>
<div class="panel-footer">
<div class="container-build">
<button (click)='addRowAdressForm()' type="button" class="btn btn-default" style="padding: 2px">Add row</button>
</div>
</div>
</div>
</td>
<td>
<button (click)='deleteRowAdressForm(addressFormIndex)' type="button" class="btn btn-default">Delete</button>
</td>
</tr>
</table>
<div class="panel-footer">
<div class="container-build">
<button (click)='addRowMainForm(mainFormIndex)' type="button" class="btn btn-default">Add row</button>
</div>
</div>
</div>

最佳答案

您看不到任何更新,因为更改检测未在数组中的嵌套对象上运行。您有两个选择:

  1. 每次您更改数组中的某些内容时,将其分配为新对象,因为更改检测仅查找引用更改。

  2. 自己触发变更检测(这就是我将向您展示的内容,因为第一个选项不言自明)


导入ChangeDetectorRef:

import {ChangeDetectorRef} from '@angular/core';

ChangeDetectorRef 注入(inject)到您的组件中:

constructor(private changeDetectorRef: ChangeDetectorRef, /* other injections here */) { 
// ...
}

在您的方法中使用它来检测变化:

deleteRowAdressForm(rowNumber: number){
this.rowDataMainForm.splice(rowNumber, 1);
this.changeDetectorRef.detectChanges();
}

您需要对模板进行的更改:

在您使用的每个 *ngFor 上,您需要像这样添加 index 变量:

*ngFor="let row of table; let rowIndex = index"

然后在您的 (click) 删除事件中,您可以像这样利用它:

(click)="deleteRow(rowIndex)"

我在这里使用用法编辑了你的模板,但不要忘记像我上面那样将参数添加到你的删除函数中!

<div class="panel panel-default">
<div class="panel-heading text-center">
<h4 class="panel-title">
Подразделение
</h4>
</div>
<table class="table table-bordered">
<tr>
<th>name</th>
<th>email</th>
<th>tel</th>
<th>adress</th>
<th>delete</th>
</tr>
<tr *ngFor="let row of rowDataMainForm; let mainFormIndex = index">
<td><input type="text" class="form-control"></td>
<td><input type="text" class="form-control"></td>
<td><input type="text" class="form-control"></td>
<td>
<div class="panel panel-default smaller">
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>country</th>
<th>city</th>
<th>zipcode</th>
<th>street</th>
<th></th>
</tr>
</thead>
<tr *ngFor="let newrow of rowDataAdressForm; let addressFormIndex = index">
<td></td>
<td></td>
<td></td>
<td>
<div class="panel panel-default smaller">
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>home</th>
<th>campus</th>
<th>province</th>
<th>area</th>
<th></th>
</tr>
</thead>
<tr *ngFor="let suchnewrow of rowDataHomeForm; let homeFormIndex = index">
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<button (click)="deleteRowHomeForm(homeFormIndex)" type="button" class="btn btn-default" style="padding: 2px">Delete</button>
</td>
</tr>
</table>
<div class="panel-footer">
<div class="container-build">
<button (click)='addRowHomeCampusProvinceAreaForm()' type="button" class="btn btn-default" style="padding: 2px">Add row</button>
</div>
</div>
</div>
</td>
<td>
<button (click)='deleteRowStreetCityZipcodeForm(/* ??? */)' type="button" class="btn btn-default" style="padding: 2px">Delete</button>
</td>
</tr>
</table>
<div class="panel-footer">
<div class="container-build">
<button (click)='addRowAdressForm()' type="button" class="btn btn-default" style="padding: 2px">Add row</button>
</div>
</div>
</div>
</td>
<td>
<button (click)='deleteRowAdressForm(addressFormIndex)' type="button" class="btn btn-default">Delete</button>
</td>
</tr>
</table>
<div class="panel-footer">
<div class="container-build">
<button (click)='addRowMainForm(mainFormIndex)' type="button" class="btn btn-default">Add row</button>
</div>
</div>
</div>

关于angular - 如何删除表应用程序 Angular 2 中的任何行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38009780/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com