テーブルアプリケーションAngular2の行を削除するにはどうすればよいですか?

Eduard Arevshatyan

このメソッドでは、テーブルの行を削除しません。

  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>
rinukkusu

配列内のネストされたオブジェクトで変更検出が実行されていないため、更新は表示されません。2つのオプションがあります。

  1. 変更検出は参照の変更のみを検索するため、配列内の何かを変更するたびに、それを新しいオブジェクトとして割り当てます。

  2. 自分で変更検出をトリガーします(最初のオプションは自明なので、これを紹介します)


インポート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>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SpringMVCアプリケーションでAngular2ページを実行するにはどうすればよいですか?

分類Dev

SpringMVCアプリケーションでAngular2ページを実行するにはどうすればよいですか?

分類Dev

SpringMVCアプリケーションでAngular2ページを実行するにはどうすればよいですか?

分類Dev

Angular2アプリケーションでMediaRecorderオブジェクトを使用するにはどうすればよいですか?

分類Dev

Windowsでアプリケーション固有のルーティングテーブルを設定するにはどうすればよいですか?

分類Dev

Silexアプリケーションのトラブルシューティングを行うにはどうすればよいですか?

分類Dev

Tomcatサーバー(Windows Server 2012)にAngular2アプリケーションをデプロイするにはどうすればよいですか?

分類Dev

Macアプリケーションの設定を削除するにはどうすればよいですか?

分類Dev

PHPアプリケーションでAngular 2を使用するにはどうすればよいですか?

分類Dev

Waylandの下のコンテナでグラフィカルアプリケーションを実行するにはどうすればよいですか?

分類Dev

Angular2 WebアプリケーションでngForを正しく使用するにはどうすればよいですか?

分類Dev

現在のアプリケーションから別のアプリケーションを実行するにはどうすればよいですか?

分類Dev

モデルをXMLModelとして使用した場合、SAPUI5アプリケーションのテーブルから行を削除するにはどうすればよいですか?

分類Dev

Electronアプリケーションの[mat-icon]ボタンからツールチップを削除するにはどうすればよいですか?

分類Dev

インストールまたは削除されたアプリケーションの名前を取得するにはどうすればよいですか?

分類Dev

UWPアプリケーションのリストからアイテムを削除するにはどうすればよいですか?

分類Dev

優先アプリケーションを削除するにはどうすればよいですか

分類Dev

Ubuntuのデュアルブートオプションとパーティションを削除するにはどうすればよいですか?

分類Dev

シェルスクリプトからネイティブアプリケーションを作成するにはどうすればよいですか?

分類Dev

シェルスクリプトからネイティブアプリケーションを作成するにはどうすればよいですか?

分類Dev

アプリケーションにテーマを追加するにはどうすればよいですか?

分類Dev

Boomアプリケーションの出力デバイスをシステムから削除するにはどうすればよいですか?

分類Dev

テーブルの行から値を取得し、MVCアプリケーションのモーダルポップアップ送信クリックでその値を使用するにはどうすればよいですか?

分類Dev

AngularアプリケーションでFirebaseAdmin SDKを使用するにはどうすればよいですか?

分類Dev

MeteorアプリケーションにサードパーティのJavaScriptライブラリを追加するにはどうすればよいですか?

分類Dev

SpringアプリケーションでJFrameを実行するにはどうすればよいですか?

分類Dev

HerokuでSpringMVCアプリケーションを実行するにはどうすればよいですか?

分類Dev

HerokuでSpringMVCアプリケーションを実行するにはどうすればよいですか?

分類Dev

HerokuでSpringMVCアプリケーションを実行するにはどうすればよいですか?

Related 関連記事

  1. 1

    SpringMVCアプリケーションでAngular2ページを実行するにはどうすればよいですか?

  2. 2

    SpringMVCアプリケーションでAngular2ページを実行するにはどうすればよいですか?

  3. 3

    SpringMVCアプリケーションでAngular2ページを実行するにはどうすればよいですか?

  4. 4

    Angular2アプリケーションでMediaRecorderオブジェクトを使用するにはどうすればよいですか?

  5. 5

    Windowsでアプリケーション固有のルーティングテーブルを設定するにはどうすればよいですか?

  6. 6

    Silexアプリケーションのトラブルシューティングを行うにはどうすればよいですか?

  7. 7

    Tomcatサーバー(Windows Server 2012)にAngular2アプリケーションをデプロイするにはどうすればよいですか?

  8. 8

    Macアプリケーションの設定を削除するにはどうすればよいですか?

  9. 9

    PHPアプリケーションでAngular 2を使用するにはどうすればよいですか?

  10. 10

    Waylandの下のコンテナでグラフィカルアプリケーションを実行するにはどうすればよいですか?

  11. 11

    Angular2 WebアプリケーションでngForを正しく使用するにはどうすればよいですか?

  12. 12

    現在のアプリケーションから別のアプリケーションを実行するにはどうすればよいですか?

  13. 13

    モデルをXMLModelとして使用した場合、SAPUI5アプリケーションのテーブルから行を削除するにはどうすればよいですか?

  14. 14

    Electronアプリケーションの[mat-icon]ボタンからツールチップを削除するにはどうすればよいですか?

  15. 15

    インストールまたは削除されたアプリケーションの名前を取得するにはどうすればよいですか?

  16. 16

    UWPアプリケーションのリストからアイテムを削除するにはどうすればよいですか?

  17. 17

    優先アプリケーションを削除するにはどうすればよいですか

  18. 18

    Ubuntuのデュアルブートオプションとパーティションを削除するにはどうすればよいですか?

  19. 19

    シェルスクリプトからネイティブアプリケーションを作成するにはどうすればよいですか?

  20. 20

    シェルスクリプトからネイティブアプリケーションを作成するにはどうすればよいですか?

  21. 21

    アプリケーションにテーマを追加するにはどうすればよいですか?

  22. 22

    Boomアプリケーションの出力デバイスをシステムから削除するにはどうすればよいですか?

  23. 23

    テーブルの行から値を取得し、MVCアプリケーションのモーダルポップアップ送信クリックでその値を使用するにはどうすればよいですか?

  24. 24

    AngularアプリケーションでFirebaseAdmin SDKを使用するにはどうすればよいですか?

  25. 25

    MeteorアプリケーションにサードパーティのJavaScriptライブラリを追加するにはどうすればよいですか?

  26. 26

    SpringアプリケーションでJFrameを実行するにはどうすればよいですか?

  27. 27

    HerokuでSpringMVCアプリケーションを実行するにはどうすればよいですか?

  28. 28

    HerokuでSpringMVCアプリケーションを実行するにはどうすればよいですか?

  29. 29

    HerokuでSpringMVCアプリケーションを実行するにはどうすればよいですか?

ホットタグ

アーカイブ