Angular Hierarchical Grid の行ドラッグ
Ignite UI for Angular Hierarchical Grid では、RowDrag がルート igx-hierarchical-grid
コンポーネントで初期化されて、rowDraggable
入力で設定できます。行ドラッグを有効にすると、ユーザーは行ドラッグ ハンドルを使用して行のドラッグを開始できます。
Angular Hierarchical Grid 行ドラッグの例
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 {
IgxHierarchicalGridModule,
IgxDragDropModule,
IgxButtonModule
} from "igniteui-angular" ;
import { HGridRowDragBaseComponent } from "./hierarchical-grid/hierarchical-grid-row-drag-base/hierarchical-row-drag-base.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
HGridRowDragBaseComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxHierarchicalGridModule,
IgxDragDropModule,
IgxButtonModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー
import { Component, ViewChild } from '@angular/core' ;
import { IDropDroppedEventArgs, IgxHierarchicalGridComponent, RowType } from 'igniteui-angular' ;
import { createData, IDrive } from '../../data/files.data' ;
enum DragIcon {
DEFAULT = 'drag_indicator' ,
ALLOW = 'remove'
}
@Component ({
selector : 'app-hierarchical-row-drag-base' ,
styleUrls : ['./hierarchical-row-drag-base.component.scss' ],
templateUrl : 'hierarchical-row-drag-base.component.html'
})
export class HGridRowDragBaseComponent {
@ViewChild (IgxHierarchicalGridComponent, { read : IgxHierarchicalGridComponent, static : true })
public hGrid: IgxHierarchicalGridComponent;
public localData: IDrive[] = [];
constructor ( ) {
this .localData = createData(3 , 12 , 8 );
}
public onRowDragEnd (args ) {
args.animation = true ;
}
public onDropAllowed (args: IDropDroppedEventArgs ) {
const draggedRow: RowType = args.dragData;
draggedRow.delete();
}
public onEnterAllowed (args ) {
this .changeGhostIcon(args.drag.ghostElement, DragIcon.ALLOW);
}
public onLeaveAllowed (args ) {
this .changeGhostIcon(args.drag.ghostElement, DragIcon.DEFAULT);
}
private changeGhostIcon (ghost, icon: string ) {
if (ghost) {
const currentIcon = ghost.querySelector('.igx-grid__drag-indicator > igx-icon' );
if (currentIcon) {
currentIcon.innerText = icon;
}
}
}
}
ts コピー <div class ="drop-area" igxDrop (enter )="onEnterAllowed($event)" (leave )="onLeaveAllowed($event)"
(dropped )="onDropAllowed($event)" >
<igx-icon > delete</igx-icon >
<div > Drag a row here to delete it</div >
</div >
<igx-hierarchical-grid [igxPreventDocumentScroll ]="true" class ="hgrid" [allowFiltering ]='true' #grid1 [data ]="localData" [primaryKey ]="'id'" [rowDraggable ]="true"
[height ]="'540px'" [width ]="'100%'" #hGrid (rowDragEnd )="onRowDragEnd($event)" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="system" header ="Is System Drive" > </igx-column >
<igx-row-island [height ]="null" [rowDraggable ]="true" [primaryKey ]="'id'" igxDrop [key ]="'folders'" #layout1 >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="createdOn" header ="Created On" > </igx-column >
<igx-column field ="createdBy" header ="Created By" > </igx-column >
<igx-row-island [height ]="null" [rowDraggable ]="true" [primaryKey ]="'id'" igxDrop [key ]="'files'" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="extension" header ="Extension" > </igx-column >
<igx-column field ="size" header ="Size" dataType ="number" [hasSummary ]="true" > </igx-column >
<igx-column field ="createdBy" header ="createdBy" > </igx-column >
</igx-row-island >
</igx-row-island >
</igx-hierarchical-grid >
html コピー .drop-area {
width : 160px ;
height : 160px ;
background-color : #d3d3d3 ;
border : 1px dashed #131313 ;
display : flex;
justify-content : center;
align-items : center;
flex-flow : column;
text-align : center;
margin : 8px ;
}
:host {
display : flex;
justify-content : space-evenly;
align-items : center;
flex-flow : row;
width : 100% ;
padding-top : 10px ;
}
.igx-grid {
margin : 0 1rem ;
}
scss コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
構成
igx-hierarchical-grid
の行ドラッグを有効にするには、グリッドの rowDraggable
を true
に設定します。これが有効になると、行ドラッグ ハンドルが各行に表示されます。このハンドルは行ドラッグを開始するために使用できます。
<igx-hierarchical-grid [rowDraggable ]="true" >
...
</igx-hierarchical-grid >
html
ドラッグ ハンドルをクリックしてボタンを押しながらカーソルを動かすと、グリッドの rowDragStart
イベントが発生します。クリックをリリースすると、rowDragEnd
イベントが発生します。
以下は、行ドラッグをサポートするための igx-hierarchical-grid
の設定方法と、ドロップイベントの適切な処理方法についてのチュートリアルです。
この例では、グリッドから指定された領域に行をドラッグし、ドロップするとグリッドから削除します。
ドロップ エリア
行ドラッグを簡単に有効にできました。次は行ドロップを処理する方法を設定する必要があります。
igxDrop
ディレクティブ を使用して、行をドロップする場所を定義できます。
はじめに、アプリ モジュールに IgxDragDropModule
をインポートする必要があります。
import { ..., IgxDragDropModule } from 'igniteui-angular' ;
...
@NgModule ({
imports : [..., IgxDragDropModule]
})
typescript
次にテンプレートでディレクティブのセレクターを使ってドロップ エリアを定義します。
<div class ="drop-area" igxDrop (enter )="onEnterAllowed($event)" (leave )="onLeaveAllowed($event)"
(dropped )="onDropAllowed($event)" >
<igx-icon > delete</igx-icon >
<div > Drag a row here to delete it</div >
</div >
html
rowDragEnd
イベントの animation
パラメーターを使用して、ドロップできない領域に行がドロップされたときにアニメーションを有効にできます。true に設定されている場合、ドラッグされた行は、ドロップできない領域の上にドロップされると元の位置に戻ります。
以下はアニメーションを有効にする方法です。
export class IgxHierarchicalGridRowDragComponent {
public onRowDragEnd (args ) {
args.animation = true ;
}
}
typescript
ドロップ エリア イベント ハンドラー
テンプレートでドロップ領域を定義したら、コンポーネントの .ts
ファイルで igxDrop
の enter
、leave
、dropped
イベントを宣言する必要があります。
はじめに、enter
と leave
ハンドラーを見てみましょう。これらのメソッドでは、ドラッグの ghost のアイコンを変更して、行をドロップできる領域の上にあることをユーザーに示すことができます。
export class IgxHierarchicalGridRowDragComponent {
public onEnterAllowed (args ) {
this .changeGhostIcon(args.drag.ghostElement, DragIcon.ALLOW);
}
public onLeaveAllowed (args ) {
this .changeGhostIcon(args.drag.ghostElement, DragIcon.DEFAULT);
}
private changeGhostIcon (ghost, icon: string ) {
if (ghost) {
const currentIcon = ghost.querySelector('.igx-grid__drag-indicator > igx-icon' );
if (currentIcon) {
currentIcon.innerText = icon;
}
}
}
}
typescript
changeGhostIcon
private メソッドは、ドラッグ ゴースト内のアイコンを変更するだけです。メソッドのロジックは、アイコンを含む要素を検索し (ドラッグ インジケーター コンテナーに適用される igx-grid__drag-indicator
クラスを使用)、要素の内部テキストを渡されたものに変更します。
アイコンは material
フォントセット からのもので、別の enum
で定義されています。
enum DragIcon {
DEFAULT = 'drag_indicator' ,
ALLOW = 'remove'
}
typescript
次に、ユーザーが実際にドロップ領域内に行をドロップ したときに何が起こるかを定義する必要があります。
export class IgxHierarchicalGridRowDragComponent {
public onDropAllowed (args: IDropDroppedEventArgs ) {
const draggedRow: RowType = args.dragData;
draggedRow.delete();
}
}
typescript
行が削除されたら、行の delete()
メソッドを呼び出すだけです。
イベント引数 (args.dragData.data
) または他の行プロパティからの行データを使用する場合、行全体が参照として引数に渡されることに注意してください。つまり、ソースグリッドのデータと区別する必要がある場合は、必要なデータを複製する必要があります。
ドラッグ ゴーストのテンプレート化
ドラッグゴーストは、igx-hierarchical-grid
の本文内の <ng-template>
に適用される IgxRowDragGhost
ディレクティブを使用してテンプレート化できます。
<igx-hierarchical-grid >
...
<ng-template igxRowDragGhost >
<div >
<igx-icon fontSet ="material" > arrow_right_alt</igx-icon >
</div >
</ng-template >
...
</igx-hierarchical-grid >
html
以下は、行ドラッグと複数選択を有効にした igx-hierarchical-grid
で確認できる設定の結果です。以下のデモでは、現在ドラッグされている行の数を示します。
デモ
ドラッグ ゴーストは各グリッド レベルでテンプレート化できます。複数のゴース トテンプレートを作成できて、または単一の行アイランドにのみテンプレートを提供できます。
<igx-hierarchical-grid >
...
<ng-template igxRowDragGhost >
<div >
<igx-icon fontSet ="material" > arrow_right_alt</igx-icon >
</div >
</ng-template >
<igx-row-island >
...
<ng-template IgxRowDragGhost >
<img src ="smile.gif" height ="42" width ="42" >
</ng-template >
</igx-row-island >
...
</igx-hierarchical-grid >
html
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 {
IgxHierarchicalGridModule,
IgxDragDropModule,
IgxButtonModule
} from "igniteui-angular" ;
import { HGridMultiRowDragComponent } from "./hierarchical-grid/hierarchical-grid-multi-row-drag/hierarchical-grid-multi-row-drag.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
HGridMultiRowDragComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxHierarchicalGridModule,
IgxDragDropModule,
IgxButtonModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, ViewChild } from '@angular/core' ;
import { GridSelectionMode, IDropDroppedEventArgs, IgxHierarchicalGridComponent, RowType } from 'igniteui-angular' ;
import { createData, IDrive } from '../../data/files.data' ;
@Component ({
selector : 'app-hierarchical-grid-multi-row-drag' ,
styleUrls : ['./hierarchical-grid-multi-row-drag.component.scss' ],
templateUrl : 'hierarchical-grid-multi-row-drag.component.html'
})
export class HGridMultiRowDragComponent {
@ViewChild (IgxHierarchicalGridComponent, { read : IgxHierarchicalGridComponent, static : true })
public hGrid: IgxHierarchicalGridComponent;
public localData: IDrive[] = [];
public selectionMode: GridSelectionMode = 'multiple' ;
public ids;
public grid;
public selected = false ;
public countIcon = 'drag_indicator' ;
public dragIcon = 'keyboard_backspace' ;
constructor ( ) {
this .localData = createData(3 , 12 , 8 );
}
public onRowDragEnd (args ) {
args.animation = true ;
}
public handleRowSelectionChange (args ) {
this .ids = args.newSelection;
this .grid = args.owner;
this .selected = this .ids.length !== 0 ;
}
public onDropAllowed (args: IDropDroppedEventArgs ) {
if (this .selected === false ) {
const draggedRow: RowType = args.dragData;
draggedRow.delete();
} else {
if (this .grid == null ) {
this .ids.forEach((rowData ) => {
this .hGrid.deleteRow(rowData);
});
this .selected = false ;
} else {
this .ids.forEach((rowData ) => {
this .grid.deleteRow(rowData);
});
this .selected = false ;
}
}
}
public onRowDragStart (args ) {
if (this .selected === false ) {
this .countIcon = `filter_1` ;
} else {
const count = this .ids.length;
this .countIcon = `filter_${count > 9 ? '9_plus' : `${count} ` } ` ;
}
}
public onLeaveAllowed (args ) {
this .onRowDragStart(args);
this .dragIcon = 'keyboard_backspace' ;
}
public onEnterAllowed (args ) {
this .dragIcon = 'remove' ;
}
}
ts コピー <div class ="drop-area" igxDrop (enter )="onEnterAllowed($event)" (leave )="onLeaveAllowed($event)"
(dropped )="onDropAllowed($event)" >
<div > Drag a row here to delete it</div >
</div >
<igx-hierarchical-grid [igxPreventDocumentScroll ]="true" class ="hgrid" [allowFiltering ]='true' #grid1 [data ]="localData" [primaryKey ]="'id'" [rowDraggable ]="true"
[height ]="'580px'" [width ]="'100%'" #hGrid (rowDragStart )="onRowDragStart($event)" (rowDragEnd )="onRowDragEnd($event)" [rowSelection ]="selectionMode"
(rowSelectionChanging )="handleRowSelectionChange($event)" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="system" header ="Is System Drive" > </igx-column >
<igx-row-island [height ]="null" [rowDraggable ]="true" [primaryKey ]="'id'" igxDrop [key ]="'folders'" #layout1 [rowSelection ]="selectionMode"
(rowSelectionChanging )="handleRowSelectionChange($event)" (rowDragStart )="onRowDragStart($event)" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="createdOn" header ="Created On" > </igx-column >
<igx-column field ="createdBy" header ="Created By" > </igx-column >
<ng-template let-data igxRowDragGhost >
<div class ="allow-drop" >
<igx-icon family ="material" > {{dragIcon}}{{countIcon}}</igx-icon >
</div >
</ng-template >
<igx-row-island [height ]="null" [rowDraggable ]="true" [primaryKey ]="'id'" igxDrop [key ]="'files'" [rowSelection ]="selectionMode"
(rowSelectionChanging )="handleRowSelectionChange($event)" (rowDragStart )="onRowDragStart($event)" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="extension" header ="Extension" > </igx-column >
<igx-column field ="size" header ="Size" dataType ="number" [hasSummary ]="true" > </igx-column >
<igx-column field ="createdBy" header ="createdBy" > </igx-column >
<ng-template let-data igxRowDragGhost >
<div class ="allow-drop" >
<igx-icon family ="material" > {{dragIcon}}{{countIcon}}</igx-icon >
</div >
</ng-template >
</igx-row-island >
</igx-row-island >
<ng-template let-data igxRowDragGhost >
<div class ="allow-drop" >
<igx-icon family ="material" > {{dragIcon}}{{countIcon}}</igx-icon >
</div >
</ng-template >
</igx-hierarchical-grid >
html コピー .drop-area {
width : 160px ;
height : 160px ;
background-color : #d3d3d3 ;
border : 1px dashed #131313 ;
display : flex;
justify-content : center;
align-items : center;
flex-flow : column;
text-align : center;
margin : 8px ;
}
:host {
display : flex;
justify-content : space-evenly;
align-items : center;
flex-flow : row;
width : 100% ;
padding-top : 10px ;
}
.allow-drop {
z-index : 1 ;
}
.igx-grid {
margin : 0 1rem ;
}
scss コピー
ドラッグ アイコンのテンプレート化
ドラッグ ハンドル アイコンは、グリッドの dragIndicatorIconTemplate
を使用してテンプレート化できます。作成している例で、アイコンをデフォルトのもの (drag_indicator
) から drag_handle
に変更します。
igxDragIndicatorIcon
を使用して igx-hierarchical-grid
の本文内にテンプレートを渡して変更できます。
<igx-hierarchical-grid >
...
<ng-template igxDragIndicatorIcon >
<igx-icon > drag_handle</igx-icon >
</ng-template >
...
</igx-hierarchical-grid >
html
新しいアイコン テンプレートの設定後、DragIcon enum
の DEFAULT
アイコンも調整する必要があるため、changeIcon
メソッドによって適切に変更されます。
enum DragIcon {
DEFAULT = "drag_handle" ,
...
}
typescript
ドロップ エリアのスタイル
ドロップ ハンドラが正しく設定されたら、次にドロップ領域をスタイル設定します。
.drop-area {
width : 160px ;
height : 160px ;
background-color : #d3d3d3 ;
border : 1px dashed #131313 ;
display : flex;
justify-content : center;
align-items : center;
flex-flow : column;
text-align : center;
margin : 8px ;
}
:host {
display : flex;
justify-content : center;
align-items : center;
flex-flow : column;
width : 100% ;
}
css
結果は以下のデモで確認できます。
デモ
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 {
IgxHierarchicalGridModule,
IgxDragDropModule,
IgxIconModule,
IgxButtonModule
} from "igniteui-angular" ;
import { HGridDragSampleComponent } from "./hierarchical-grid/hierarchical-grid-row-drag/hierarchical-grid-row-drag.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
HGridDragSampleComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxHierarchicalGridModule,
IgxDragDropModule,
IgxIconModule,
IgxButtonModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー
import { Component, ViewChild } from '@angular/core' ;
import { IDropDroppedEventArgs, IgxHierarchicalGridComponent, RowType } from 'igniteui-angular' ;
import { createData, IDrive } from '../../data/files.data' ;
enum DragIcon {
DEFAULT = 'drag_handle' ,
ALLOW = 'remove'
}
@Component ({
selector : 'app-hierarchical-grid-row-drag' ,
styleUrls : ['./hierarchical-grid-row-drag.component.scss' ],
templateUrl : 'hierarchical-grid-row-drag.component.html'
})
export class HGridDragSampleComponent {
@ViewChild (IgxHierarchicalGridComponent, { read : IgxHierarchicalGridComponent, static : true })
public hGrid: IgxHierarchicalGridComponent;
public localData: IDrive[] = [];
constructor ( ) {
this .localData = createData(3 , 12 , 8 );
}
public onRowDragEnd (args ) {
args.animation = true ;
}
public onDropAllowed (args: IDropDroppedEventArgs ) {
const draggedRow: RowType = args.dragData;
draggedRow.delete();
}
public onEnterAllowed (args ) {
this .changeGhostIcon(args.drag.ghostElement, DragIcon.ALLOW);
}
public onLeaveAllowed (args ) {
this .changeGhostIcon(args.drag.ghostElement, DragIcon.DEFAULT);
}
private changeGhostIcon (ghost, icon: string ) {
if (ghost) {
const currentIcon = ghost.querySelector('.igx-grid__drag-indicator > igx-icon' );
if (currentIcon) {
currentIcon.innerText = icon;
}
}
}
}
ts コピー <div class ="drop-area" igxDrop (enter )="onEnterAllowed($event)" (leave )="onLeaveAllowed($event)"
(dropped )="onDropAllowed($event)" >
<igx-icon > delete</igx-icon >
<div > Drag a row here to delete it</div >
</div >
<igx-hierarchical-grid [igxPreventDocumentScroll ]="true" class ="hgrid" [allowFiltering ]='true' #grid1 [data ]="localData"
[primaryKey ]="'id'" [rowDraggable ]="true" [height ]="'540px'" [width ]="'100%'" #hGrid
(rowDragEnd )="onRowDragEnd($event)" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="system" header ="Is System Drive" > </igx-column >
<igx-row-island [height ]="null" [primaryKey ]="'id'" igxDrop [key ]="'folders'" #layout1 (rowDragEnd )="onRowDragEnd($event)" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="createdOn" header ="Created On" > </igx-column >
<igx-column field ="createdBy" header ="Created By" > </igx-column >
<igx-row-island [height ]="null" [rowDraggable ]="true" [primaryKey ]="'id'" igxDrop [key ]="'files'" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="extension" header ="Extension" > </igx-column >
<igx-column field ="size" header ="Size" dataType ="number" [hasSummary ]="true" > </igx-column >
<igx-column field ="createdBy" header ="createdBy" > </igx-column >
</igx-row-island >
</igx-row-island >
<ng-template igxDragIndicatorIcon >
<igx-icon > drag_handle</igx-icon >
</ng-template >
</igx-hierarchical-grid >
html コピー .drop-area {
width : 160px ;
height : 160px ;
background-color : #d3d3d3 ;
border : 1px dashed #131313 ;
display : flex;
justify-content : center;
align-items : center;
flex-flow : column;
text-align : center;
margin : 8px ;
}
:host {
display : flex;
justify-content : space-evenly;
align-items : center;
flex-flow : row;
width : 100% ;
padding-top : 10px ;
}
.igx-grid {
margin : 0 1rem ;
}
scss コピー
アプリケーション デモ
行の並べ替えデモ
グリッドの行ドラッグ イベントと igxDrop
ディレクティブを使用して、ドラッグよる行の並べ替えるが可能なグリッドを作成できます。
すべてのアクションはグリッド本体の内側で発生するため、ここで igxDrop
ディレクティブをアタッチする必要があります:
<igx-hierarchical-grid #grid [data ]="localData" [primaryKey ]="'id'"
[rowDraggable ]="true" (rowDragStart )="rowDragStart($event)" igxDrop (dropped )="rowDrop($event)" >
...
</igx-hierarchical-grid >
html
グリッドに primaryKey
が指定されていることを確認してください!ロジックが行を適切に並べ替えられるように、行には一意の識別子が必要です。
rowDraggable
が有効になり、ドロップ エリアが定義されたら、ドロップ イベントの単純なハンドラーを実装する必要があります。行をドラッグするときは、以下を確認してください:
行が展開されていますか? そうであれば、行を縮小します。
行はグリッド内にドロップされましたか?
そうであれば、ドラッグされた行が他のどの行にドロップされましたか?
ターゲット行が見つかれば、data
配列内のレコードの位置を入れ替えます。
行は最初に選択されてましたか? そうであれば、選択済みとしてマークします。
以下では、コンポーネントの .ts
ファイルに実装されていることがわかります。
export class HGridRowReorderComponent {
public rowDragStart(args: any ): void {
const targetRow = args.dragData;
if (targetRow.expanded) {
targetRow.expanded = false ;
}
}
public rowDrop(args: IDropDroppedEventArgs): void {
const targetRow = args.dragData;
const event = args.originalEvent;
const cursorPosition: Point = { x : event.clientX, y : event.clientY };
this .moveRow(targetRow, cursorPosition);
}
private moveRow(draggedRow: RowType, cursorPosition : Point): void {
const parent = this .hGrid;
const rowIndex: number = this .getTargetRowIndex(parent.rowList.toArray(), cursorPosition);
if (rowIndex === -1 ) { return ; }
const wasSelected = draggedRow.selected;
draggedRow.delete();
parent.data.splice(rowIndex, 0 , draggedRow.data);
if (wasSelected) {
parent.selectRows([parent.rowList.toArray()
.find((r ) => r.rowID === draggedRow.key).rowID], false );
}
}
private getTargetRowIndex(rowListArr: RowType[], cursorPosition : Point): number {
const targetElem: IgxHierarchicalRowComponent = this .catchCursorPosOnElem(rowListArr, cursorPosition);
return rowListArr.indexOf(rowListArr.find((r ) => r.rowData.id === targetElem.rowData.id));
}
private catchCursorPosOnElem(rowListArr: any [], cursorPosition : Point)
: IgxHierarchicalRowComponent {
for (const row of rowListArr) {
const rowRect = row.nativeElement.getBoundingClientRect();
if (cursorPosition.y > rowRect.top + window .scrollY && cursorPosition.y < rowRect.bottom + window .scrollY &&
cursorPosition.x > rowRect.left + window .scrollX && cursorPosition.x < rowRect.right + window .scrollX) {
return row;
} else if (row === rowListArr[rowListArr.length - 1 ] && cursorPosition.y > rowRect.bottom) {
return row;
}
}
}
}
typescript
これらの簡単な手順で、ドラッグ/ドロップで行を並べ替えることができるグリッドを構成しました! 次のデモで、上記コードの動作を確認できます。
行の選択も有効で、ドラッグした行をドロップしても選択が保持されます。
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 {
IgxHierarchicalGridModule,
IgxDragDropModule
} from "igniteui-angular" ;
import { HGridRowReorderComponent } from "./hierarchical-grid/hierarchical-grid-row-reorder/hierarchical-grid-row-reorder.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
HGridRowReorderComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxHierarchicalGridModule,
IgxDragDropModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, ViewChild } from '@angular/core' ;
import {
IDropDroppedEventArgs,
IgxHierarchicalGridComponent,
RowType,
Point,
GridSelectionMode
} from 'igniteui-angular' ;
import { createData, IDrive } from '../../data/files.data' ;
@Component ({
selector : 'app-hierarchical-grid-row-reorder' ,
styleUrls : ['./hierarchical-grid-row-reorder.component.scss' ],
templateUrl : 'hierarchical-grid-row-reorder.component.html'
})
export class HGridRowReorderComponent {
@ViewChild (IgxHierarchicalGridComponent, { read : IgxHierarchicalGridComponent, static : true })
public hGrid: IgxHierarchicalGridComponent;
public localData: IDrive[] = [];
public selectionMode: GridSelectionMode = 'multiple' ;
constructor ( ) {
this .localData = createData(3 , 12 , 8 );
}
public rowDragStart(args: any ): void {
const targetRow: RowType = args.dragData;
if (targetRow.expanded) {
targetRow.expanded = false ;
}
}
public rowDrop(args: IDropDroppedEventArgs): void {
const targetRow = args.dragData;
const event = args.originalEvent;
const cursorPosition: Point = { x : event.clientX, y : event.clientY };
this .moveRow(targetRow, cursorPosition);
}
private moveRow(draggedRow: RowType, cursorPosition : Point): void {
const parent = this .hGrid;
const rowIndex: number = this .getTargetRowIndex(parent.rowList.toArray(), cursorPosition);
if (rowIndex === -1 ) { return ; }
const wasSelected = draggedRow.selected;
draggedRow.delete();
parent.data.splice(rowIndex, 0 , draggedRow.data);
if (wasSelected) {
parent.selectRows([parent.rowList.toArray()
.find((r ) => r.key === draggedRow.key).key], false );
}
}
private getTargetRowIndex(rowListArr: any [], cursorPosition : Point): number {
const targetElem = this .catchCursorPosOnElem(rowListArr, cursorPosition);
return rowListArr.indexOf(rowListArr.find((r ) => r.key === targetElem.key));
}
private catchCursorPosOnElem(rowListArr: any [], cursorPosition : Point): any {
for (const row of rowListArr) {
const rowRect = row.nativeElement.getBoundingClientRect();
if (cursorPosition.y > rowRect.top + window .scrollY && cursorPosition.y < rowRect.bottom + window .scrollY &&
cursorPosition.x > rowRect.left + window .scrollX && cursorPosition.x < rowRect.right + window .scrollX) {
return row;
} else if (row === rowListArr[rowListArr.length - 1 ] && cursorPosition.y > rowRect.bottom) {
return row;
}
}
}
}
ts コピー <igx-hierarchical-grid [igxPreventDocumentScroll ]="true" class ="hgrid" igxDrop #grid1 [data ]="localData" [primaryKey ]="'id'"
[rowDraggable ]="true" [rowSelection ]="selectionMode" (rowDragStart )="rowDragStart($event)" (dropped )="rowDrop($event)"
[height ]="'540px'" [width ]="'100%'" #hGrid >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="system" header ="Is System Drive" > </igx-column >
<igx-row-island [height ]="null" [primaryKey ]="'id'" [key ]="'folders'" #layout1 >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="createdOn" header ="Created On" > </igx-column >
<igx-column field ="createdBy" header ="Created By" > </igx-column >
<igx-row-island [height ]="null" [primaryKey ]="'id'" igxDrop [key ]="'files'" [rowDraggable ]="true" [rowSelection ]="selectionMode"
(rowDragStart )="rowDragStart($event)" >
<igx-column field ="name" header ="Name" > </igx-column >
<igx-column field ="extension" header ="Extension" > </igx-column >
<igx-column field ="size" header ="Size" dataType ="number" [hasSummary ]="true" > </igx-column >
<igx-column field ="createdBy" header ="createdBy" > </igx-column >
</igx-row-island >
</igx-row-island >
</igx-hierarchical-grid >
html コピー :host {
display : flex;
flex-flow : row;
align-items : center;
justify-content : center;
padding : 8px 0px ;
}
.igx-grid {
margin : 0 1rem ;
}
scss コピー
制限
現在、rowDraggable
ディレクティブに既知の制限はありません。
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。