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

时间:2016-06-24 09:18:28

标签: angular

我不会删除表格中关于此方法的任何行:

  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>

2 个答案:

答案 0 :(得分:5)

您看不到更新,因为更改检测未在阵列中的嵌套对象上运行。您有两种选择:

  1. 每次更改数组中的内容时,都会将其指定为新对象,因为更改检测仅查找引用更改。

  2. 自己触发更改检测(这是我将向您展示的内容,因为第一个选项是自解释的)

  3. 导入ChangeDetectorRef

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

    ChangeDetectorRef注入您的组件:

    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>
    

答案 1 :(得分:0)

只需添加一个函数参数?

deleteAnyRowAdressForm(x){
    this.rowDataMainForm.splice(x, 1);
}