Angular Tree Grid 一括編集とトランザクション
IgxTreeGrid の一括編集機能は、HierarchicalTransactionService
に基づいています。トランザクション サービス クラス階層
トピックに従って、igxHierarchicalTransactionService
の概要と、その実装方法の詳細を確認してください。
以下は、Tree Grid コンポーネントに対して一括編集を有効にする方法の詳細な例です。
Angular Tree Grid 一括編集とトランザクションの例
以下のサンプルは、treeGrid にプロバイダーのトランザクションがあり、batchEditing
と行編集が有効にされています。行編集全体を確定後にトランザクションが追加されるようにします。
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive";
import {
IgxTreeGridModule,
IgxButtonModule,
IgxDialogModule,
IgxGridModule
} from "igniteui-angular";
import { TreeGridBatchEditingSampleComponent } from "./tree-grid/tree-grid-batch-editing/tree-grid-batch-editing-sample.component";
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
TreeGridBatchEditingSampleComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxTreeGridModule,
IgxButtonModule,
IgxDialogModule,
IgxGridModule
],
providers: [],
entryComponents: [],
schemas: []
})
export class AppModule {}
ts
import { Component, OnInit, ViewChild } from '@angular/core';
import {
IgxDialogComponent,
IgxGridComponent,
IgxTreeGridComponent,
Transaction
} from 'igniteui-angular';
import { generateRandomInteger } from '../../data/utils';
import { generateEmployeeFlatData, IEmployee } from '../data/employees-flat';
@Component({
selector: 'app-tree-grid-batch-editing-sample',
styleUrls: ['tree-grid-batch-editing-sample.component.scss'],
templateUrl: 'tree-grid-batch-editing-sample.component.html'
})
export class TreeGridBatchEditingSampleComponent implements OnInit {
@ViewChild('treeGrid', { static: true }) public treeGrid: IgxTreeGridComponent;
@ViewChild(IgxDialogComponent, { static: true }) public dialog: IgxDialogComponent;
@ViewChild('dialogGrid', { read: IgxGridComponent, static: true }) public dialogGrid!: IgxGridComponent;
public data: IEmployee[];
public transactionsData: Transaction[] = [];
private nextRow = 1;
public ngOnInit(): void {
this.data = generateEmployeeFlatData();
}
public addRow() {
const addedData: IEmployee = {
Age: 32,
HireDate: new Date(generateRandomInteger(2008, 2015),
generateRandomInteger(0, 12), generateRandomInteger(5, 25)),
ID: this.data.length + this.nextRow++,
Name: 'John Doe',
Phone: '(91) 745 6200',
OnPTO: false,
ParentID: -1,
Title: 'Junior Sales Representative'
};
this.treeGrid.addRow(addedData);
}
public addChildRow(id) {
const addedData: IEmployee = {
Age: generateRandomInteger(18, 55),
HireDate: new Date(generateRandomInteger(2008, 2015),
generateRandomInteger(0, 12), generateRandomInteger(5, 25)),
ID: this.data.length + this.nextRow++,
Name: 'Added Addedington',
Phone: '(91) 745 6200',
OnPTO: false,
ParentID: -1,
Title: 'Intern'
};
this.treeGrid.addRow(
addedData,
id);
}
public deleteRow(id) {
this.treeGrid.deleteRow(id);
}
public undo() {
this.treeGrid.endEdit(true);
this.treeGrid.transactions.undo();
}
public redo() {
this.treeGrid.endEdit(true);
this.treeGrid.transactions.redo();
}
public commit() {
this.treeGrid.transactions.commit(this.data);
this.dialog.close();
}
public cancel() {
this.dialog.close();
}
public discard() {
this.treeGrid.transactions.clear();
this.dialog.close();
}
public openCommitDialog() {
this.dialog.open();
this.transactionsData = this.treeGrid.transactions.getAggregatedChanges(true);
this.dialogGrid.reflow();
}
public stateFormatter(value: string) {
return value ? JSON.stringify(value) : '';
}
public typeFormatter(value: string) {
return value ? value.toUpperCase() : '';
}
public classFromType(type: string): string {
return type ? `transaction--${type.toLowerCase()}` : '';
}
}
ts
<div class="grid__wrapper">
<igx-tree-grid [igxPreventDocumentScroll]="true" #treeGrid [batchEditing]="true" [data]="data" primaryKey="ID" foreignKey="ParentID"
[width]="'100%'" [height]="'500px'" [rowEditable]="true">
<igx-column [filterable]="false" width="150" [editable]="false">
<ng-template igxCell let-cell="cell" let-val>
<button igxButton (click)="deleteRow(cell.id.rowID)"
[disabled]="cell.row.deleted">Delete</button>
</ng-template>
</igx-column>
<igx-column [filterable]="false" width="180" [editable]="false">
<ng-template igxCell let-cell="cell" let-val>
<button igxButton (click)="addChildRow(cell.id.rowID)" [disabled]="cell.row.deleted">Add Child
Row</button>
</ng-template>
</igx-column>
<igx-column field="ID" header="ID" dataType="number" width="200"></igx-column>
<igx-column field="Age" header="Age" dataType="number" width="120"></igx-column>
<igx-column field="Name" header="Full Name" dataType="string" width="240"></igx-column>
<igx-column field="Title" header="Title" dataType="string" width="180"></igx-column>
<igx-column field="HireDate" header="Hire Date" dataType="date" width="150"></igx-column>
<igx-column field="OnPTO" header="On PTO" dataType="boolean" width="80"></igx-column>
</igx-tree-grid>
<div class="buttons-row">
<button igxButton (click)="addRow()">Add Root Level Row</button>
<div class="buttons-wrapper">
<button igxButton [disabled]="!treeGrid.transactions.canUndo" (click)="undo()">Undo</button>
<button igxButton [disabled]="!treeGrid.transactions.canRedo" (click)="redo()">Redo</button>
<button igxButton [disabled]="treeGrid.transactions.getAggregatedChanges(false).length < 1"
(click)="openCommitDialog()">Commit</button>
</div>
</div>
<igx-dialog title="Submit the following transactions?">
<igx-grid [igxPreventDocumentScroll]="true" #dialogGrid [data]="transactionsData" [rowHeight]="64" [primaryKey]="'id'"
width="600px" height="300px" [emptyGridMessage]="'No available transactions'">
<igx-column field="id" header="ID" [dataType]="'string'" width="100px"></igx-column>
<igx-column field="type" header="Type" width="150px" [sortable]="true">
<ng-template igxCell let-cell="cell" let-val>
<span [class]="classFromType(val)">{{ typeFormatter(val) }}</span>
</ng-template>
</igx-column>
<igx-column field="newValue" header="Value" width="900px">
<ng-template igxCell let-cell="cell" let-val>
<span class="transaction-log">{{ stateFormatter(val) }}</span>
</ng-template>
</igx-column>
</igx-grid>
<div class="buttons-wrapper">
<button igxButton (click)="commit()">Commit</button>
<button igxButton (click)="discard()">Discard</button>
<button igxButton (click)="cancel()">Cancel</button>
</div>
</igx-dialog>
</div>
html
.grid__wrapper {
margin: 15px;
}
h4 {
text-align: center;
padding-top: 2%;
padding-bottom: 2%;
}
.buttons-row {
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 5px;
}
.buttons-wrapper {
display: flex;
flex-direction: row;
justify-content: center;
padding: 10px 0;
}
.list-container {
width: 600px;
height: 300px;
overflow-y: visible;
}
.transaction--update, .transaction--delete, .transaction--add {
font-weight: 600;
}
.transaction--add {
color: #6b3;
}
.transaction--update {
color: #4a71b9;
}
.transaction--delete {
color: #ee4920;
}
.transaction-log {
word-wrap: none;
}
scss
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
トランザクション ステートは、すべての更新、追加、削除された行、そして最後のステートで構成されます。
使用方法
IgxTreeGridModule
を app.module.ts ファイルにインポートします。
...
import { IgxTreeGridModule } from 'igniteui-angular';
@NgModule({
...
imports: [..., IgxTreeGridModule],
...
})
export class AppModule {}
typescript
次に、Tree Grid から batchEditing
を有効にします。
<igx-tree-grid [data]="data" [batchEditing]="true">
...
</igx-tree-grid>
html
これにより、igx-tree-grid に Transaction
サービスの適切なインスタンスが提供されます。適切な TransactionService
は TransactionFactory
を通じて提供されます。この内部実装の詳細については、トランザクション トピックを参照してください。
一括編集を有効にした後、バインドされたデータ ソースと rowEditable
を true に設定して IgxTreeGrid
を定義し、バインドします。
<igx-tree-grid #treeGrid [batchEditing]="true" [data]="data" primaryKey="employeeID" foreignKey="PID"
width ="100%" height ="500px" rowEditable=true>
...
</igx-tree-grid>
...
<button igxButton (click)="addRow()">Add Root Level Row</button>
<button igxButton [disabled]="!treeGrid.transactions.canUndo" (click)="undo()">Undo</button>
<button igxButton [disabled]="!treeGrid.transactions.canRedo" (click)="redo()">Redo</button>
<button igxButton [disabled]="treeGrid.transactions.getAggregatedChanges(false).length < 1"
(click)="openCommitDialog()">Commit</button>
...
html
以下のコード例は、HierarchicalTransactionService
API (undo、redo、commit) の使用方法を示します。
export class TreeGridBatchEditingSampleComponent {
@ViewChild('treeGrid', { read: IgxTreeGridComponent }) public treeGrid: IgxTreeGridComponent;
public undo() {
this.treeGrid.endEdit(true);
this.treeGrid.transactions.undo();
}
public redo() {
this.treeGrid.endEdit(true);
this.treeGrid.transactions.redo();
}
public commit() {
this.treeGrid.transactions.commit(this.data);
this.dialog.close();
}
}
typescript
トランザクション API は編集の終了を処理しないので、自分で行う必要があります。そうしないと、Tree Grid
は編集モードのままになります。これを行う 1 つの方法は、それぞれのメソッドで endEdit
を呼び出すことです。
Tree Grid
内の親ノードの削除にはいくつかの特徴があります。階層データを使用している場合、親を削除すると子も削除されます。フラットデータを使用している場合、Tree Grid
の cascadeOnDelete
プロパティを使用して必要な動作を設定できます。このプロパティは、親が削除されたときに子レコードを削除するかどうかを示します (デフォルトでは true に設定されています)。
rowEditable
プロパティを無効にすると Tree Grid
を変更してセル変更でトランザクションを作成し、UI で行編集オーバーレイを公開しません。
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。