ツリー グリッド ロードオンデマンド
Ignite UI for Angular IgxTreeGrid
は、サーバーから最小限のデータのみ取得してレンダリングされるため、ユーザーにすばやくデータを表示できます。その後、ユーザーが行を展開した後にのみ、その特定の親行の子がロードされます。このメカニズムは、ロードオンデマンドであらゆるリモートデータとの設定が簡単にできます。
Angular ツリー グリッド ロードオンデマンドの例
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 } from "igniteui-angular";
import { TreeGridLoadOnDemandSampleComponent } from "./tree-grid/tree-grid-load-on-demand-sample/tree-grid-load-on-demand-sample.component";
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
TreeGridLoadOnDemandSampleComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxTreeGridModule
],
providers: [],
entryComponents: [],
schemas: []
})
export class AppModule {}
ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { IgxTreeGridComponent } from 'igniteui-angular';
import { TreeGridLoadOnDemandService } from './remoteService';
@Component({
selector: 'app-tree-grid-load-on-demand-sample',
styleUrls: ['./tree-grid-load-on-demand-sample.component.scss'],
templateUrl: './tree-grid-load-on-demand-sample.component.html'
})
export class TreeGridLoadOnDemandSampleComponent implements OnInit {
@ViewChild('treeGrid', { static: true }) public treeGrid: IgxTreeGridComponent;
public data = [];
private dataService = new TreeGridLoadOnDemandService();
constructor() { }
public ngOnInit() {
this.treeGrid.isLoading = true;
this.dataService.getData(-1, (children) => {
this.data = children;
this.treeGrid.isLoading = false;
});
}
public loadChildren = (parentID: any, done: (children: any[]) => void) => {
this.dataService.getData(parentID, (children) => done(children));
};
}
ts
<div class="grid__wrapper">
<igx-tree-grid [igxPreventDocumentScroll]="true" #treeGrid [data]="data" width="100%" height="400px"
primaryKey="ID" foreignKey="ParentID" [moving]="true"
[loadChildrenOnDemand]="loadChildren" hasChildrenKey="hasEmployees">
<igx-column field="Name" dataType="string" [sortable]="true" [editable]="true" [resizable]="true" ></igx-column>
<igx-column field="JobTitle" dataType="string" [sortable]="true" [editable]="true" [resizable]="true" ></igx-column>
<igx-column field="Age" dataType="number" [sortable]="true" [editable]="true" [resizable]="true" ></igx-column>
</igx-tree-grid>
</div>
html
.grid__wrapper {
margin: 15px;
}
scss
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
使用方法
ロードオンデマンド機能は、ツリーグリッド データソースの両方のタイプ (プライマリと外部キー
、または子コレクション
) と互換性があります。ツリー グリッドにルート レベルのデータをロードし、いずれかのデータソース タイプに必要なキーを指定するだけです。ツリーグリッドは、ユーザーが行を展開したときに子行をロードするためのコールバック入力プロパティ loadChildrenOnDemand
を提供します。
<igx-tree-grid #treeGrid [data]="data" primaryKey="ID" foreignKey="ParentID"
[loadChildrenOnDemand]="loadChildren">
...
</igx-tree-grid>
html
loadChildrenOnDemand
コールバックは、2 つのパラメーターを提供します。
- parentID - 展開されている親行の ID。
- done - 子がサーバーから取得されたときに子と共に呼び出されるコールバック。
public loadChildren = (parentID: any, done: (children: any[]) => void) => {
this.dataService.getData(parentID, (children) => done(children));
}
typescript
ユーザーが展開アイコンをクリックすると、ロード アイコンに変わります。done
コールバックが呼び出されると、ロード インジケーターが消え、子が読み込まれます。ツリーグリッドは子を基になるデータソースに追加し、必要なキーを自動的に設定します。
行がその展開前に子を持つかどうかについての情報を提供する方法がある場合は、hasChildrenKey
入力プロパティを使用できます。このようにして、展開インジケータを表示するかどうかを示すデータオブジェクトからブール値プロパティを提供できます。
<igx-tree-grid #treeGrid [data]="data" primaryKey="ID" foreignKey="ParentID"
[loadChildrenOnDemand]="loadChildren" hasChildrenKey="hasEmployees">
...
</igx-tree-grid>
html
hasChildrenKey
プロパティを設定する必要はありません。指定しなかった場合は、各行に展開インジケーターが表示されます。子を持たない行を展開した後も、未定義または空の配列で done
コールバックを呼び出す必要があります。この場合、ロード インジケーターが消えた後に展開ンジケータは表示されません。
カスタムの読み込みインジケーターを提供したい場合は、ng-template を作成し、それに igxRowLoadingIndicator
ディレクティブを使用してマークを付けます。以下のコード スニペットはカスタム テンプレートを定義する方法を示します。
<igx-tree-grid ...>
...
<ng-template igxRowLoadingIndicator>
<igx-icon fontSet="material">loop</igx-icon>
</ng-template>
</igx-tree-grid>
html
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。