Tree Grid の列の並べ替えと移動
Ignite UI for Angular の Tree Grid のコンポーネントは、標準ドラッグ/ドロップのマウス/タッチによるジェスチャ、または列移動 API を使用した順序変更のための列移動 機能を提供します。列の移動は、固定列と固定されていない列、および複数列ヘッダーの両方で機能します。列を固定領域に移動すると列が固定され、または逆に固定領域の外に列を移動すると、列の固定が解除されます。
列と列グループ間の順序変更は、それらが階層の同じレベルにあり、両方が同じグループにある場合にのみ許可されます。列/列グループが最上位の列である場合、列/列グループ間を移動できます。
列ヘッダーがテンプレート化され、対応する列が移動可能 (またはグループ化可能) である場合、テンプレート化された要素は draggable 属性を false に設定する必要があります。これにより、要素によって発行されたすべてのイベントのハンドラーをアタッチできます。それ以外の場合、イベントは igxDrag
ディレクティブによって消費されます。
ピン固定領域が最大幅 (Tree Grid 幅合計の 80%) を超えた場合、ドロップ操作が禁止されていてピン固定ができないことをヒントの表示でエンドユーザーに通知します。つまり、ピン固定領域に列をドロップできません。
<ng-template igxHeader >
<igx-icon [attr.draggable ]="false" (click )="onClick()" > </igx-icon >
</ng-template >
html
Angular Tree 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 { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
import {
IgxTreeGridModule,
IgxIconModule
} from "igniteui-angular" ;
import { TreeGridColumnMovingSampleComponent } from "./tree-grid-column-moving-sample/tree-grid-column-moving-sample.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
TreeGridColumnMovingSampleComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxTreeGridModule,
IgxIconModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, ViewChild } from '@angular/core' ;
import { ColumnType, IgxTreeGridComponent } from 'igniteui-angular' ;
import { generateEmployeeDetailedFlatData } from '../data/employees-flat-detailed' ;
@Component ({
selector : 'app-tree-grid-column-moving-sample' ,
styleUrls : ['./tree-grid-column-moving-sample.component.scss' ],
templateUrl : './tree-grid-column-moving-sample.component.html'
})
export class TreeGridColumnMovingSampleComponent {
@ViewChild ('treeGrid' , { static : true }) public treeGrid: IgxTreeGridComponent;
public data: any [];
constructor ( ) {
this .data = generateEmployeeDetailedFlatData();
}
public toggleColumnPinning (column: ColumnType ) {
column.pinned ? column.unpin() : column.pin();
}
}
ts コピー <ng-template igxHeader let-column #pinTemplate >
<div class ="title-inner" >
<span style ="float:left" > {{column.field}}</span >
<igx-icon class ="pin-icon" [class.pinned ]="column.pinned" [class.unpinned ]="!column.pinned" family ="fas" name ="fa-thumbtack"
[attr.draggable ]="false" (click )="toggleColumnPinning(column)" > </igx-icon >
</div >
</ng-template >
<div class ="grid__wrapper" >
<igx-tree-grid [igxPreventDocumentScroll ]="true" #treeGrid [data ]="data" [moving ]="true" primaryKey ="ID" foreignKey ="ParentID" [autoGenerate ]="false" height ="620px"
width ="100%" [perPage ]="10" >
<igx-paginator > </igx-paginator >
<igx-column [field ]="'Name'" dataType ="string" [headerTemplate ]="pinTemplate" width ="250px" > </igx-column >
<igx-column [field ]="'Title'" dataType ="string" [headerTemplate ]="pinTemplate" width ="250px" > </igx-column >
<igx-column [field ]="'ID'" dataType ="number" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'HireDate'" dataType ="date" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Age'" dataType ="number" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Address'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'City'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Country'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Fax'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'PostalCode'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Phone'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
</igx-tree-grid >
</div >
html コピー @import url("https://unpkg.com/@fortawesome/fontawesome-free-webfonts@^1.0.9/css/fontawesome.css" );
@import url("https://unpkg.com/@fortawesome/fontawesome-free-webfonts@^1.0.9/css/fa-regular.css" );
@import url("https://unpkg.com/@fortawesome/fontawesome-free-webfonts@^1.0.9/css/fa-solid.css" );
.grid__wrapper {
padding : 16px ;
}
.pin-icon {
margin-left : 8px ;
font-size : 14px ;
cursor : pointer;
display : flex;
align-items : center;
}
.pinned {
color : #444 ;
&:hover {
color : #999 ;
}
}
.unpinned {
color : #999 ;
&:hover {
color : #444 ;
}
}
.title-inner {
display : flex;
justify-content : space-between;
align-items : center;
}
scss コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
概要
列移動 は各列レベルで有効にできます。つまり、igx-tree-grid に移動可能な列と移動不可の列の両方を含むことができます。moving
の igx-grid
入力によって制御されます。
<igx-tree-grid [moving ]="true" > </igx-tree-grid >
html
API
ドラッグアンドドロップ機能に加えて、列の移動機能には、プログラムで列を移動/並べ替えできる 2 つの API メソッドも用意されています。
moveColumn
- 列を別の列 (ターゲット) の前または後に移動します。最初のパラメーターは移動する列で、2 番目のパラメーターはターゲット列です。オプションの 3 番目のパラメーター position
(DropPosition
値を表す) でターゲット列の前または後に列を配置するかどうかを決定します。
const idColumn = grid.getColumnByName("ID" );
const nameColumn = grid.getColumnByName("Name" );
grid.moveColumn(idColumn, nameColumn, DropPosition.AfterDropTarget);
typescript
move
- 列を指定された表示インデックスに移動します。渡されたインデックス パラメーターが無効である場合 (負である/列数を超える場合)、または列がこのインデックスに移動できない場合 (別のグループ内にある場合)、操作は実行されません。
const idColumn = grid.getColumnByName("ID" );
idColumn.move(3 );
typescript
API を使用する時、操作が成功した場合、columnMovingEnd
イベントのみが発行されることに注意してください。また、ドラッグアンドドロップ機能と比較して、API を使用するために moving
プロパティを true に設定する必要がないことにも注意してください。
イベント
列のドラッグアンドドロップ操作をカスタマイズするための列移動に関連するイベントが複数あります。columnMovingStart
、columnMoving
、columnMovingEnd
があります。
igx-tree-grid
の columnMovingEnd
イベントを処理し、列が新しい位置にドロップされたときにカスタム ロジックを実装できます。たとえば、Change On Year(%) 列の後に Category のドロップをキャンセルできます。
<igx-tree-grid #treeGrid [data ]="data" primaryKey ="ID" foreignKey ="ParentID" [autoGenerate ]="false" [moving ]="true" (columnMovingEnd )="onColumnMovingEnd($event)" >
<igx-column [field ]="'Name'" dataType ="string" width ="250px" > </igx-column >
<igx-column [field ]="'Title'" dataType ="string" width ="250px" > </igx-column >
</igx-tree-grid >
html
public onColumnMovingEnd (event ) {
if (event.source.field === "Name" && event.target.field === "Title" ) {
event.cancel = true ;
}
}
typescript
スタイル設定
Tree Grid 列移動ヘッダーのスタイル設定は、すべてのテーマ関数とコンポーネント ミックスインが存在する index
ファイルをインポートする必要があります。
@use "igniteui-angular/theming" as *;
scss
最も簡単な方法は、grid-theme
を拡張して $ghost-header-background
、$ghost-header-text-color
、$ghost-header-icon-color
パラメーターを受け入れる新しいテーマを作成します。
$dark-grid-column-moving-theme : grid-theme(
$ghost-header-text-color : #f4d45c ,
$ghost-header-background : #575757 ,
$ghost-header-icon-color : #f4bb5c
);
scss
上記のようにカラーの値をハードコーディングする代わりに、palette
および color
関数を使用してカラーに関してより高い柔軟性を実現することができます。使い方の詳細についてはパレット
のトピックをご覧ください。
最後の手順は、それぞれのテーマを持つコンポーネント ミックスインを含める ことです。
@include css-vars($dark-grid-column-moving-theme );
scss
デモ
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,
IgxIconModule
} from "igniteui-angular" ;
import { TreeGridColumnMovingStyledSampleComponent } from "./tree-grid-column-moving-styled-sample/tree-grid-column-moving-styled-sample.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
TreeGridColumnMovingStyledSampleComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxTreeGridModule,
IgxIconModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, ViewChild } from '@angular/core' ;
import { ColumnType, IgxTreeGridComponent } from 'igniteui-angular' ;
import { generateEmployeeDetailedFlatData } from '../data/employees-flat-detailed' ;
@Component ({
selector : 'app-tree-grid-column-moving-styled-sample' ,
styleUrls : ['./tree-grid-column-moving-styled-sample.component.scss' ],
templateUrl : './tree-grid-column-moving-styled-sample.component.html'
})
export class TreeGridColumnMovingStyledSampleComponent {
@ViewChild ('treeGrid' , { static : true }) public treeGrid: IgxTreeGridComponent;
public data: any [];
constructor ( ) {
this .data = generateEmployeeDetailedFlatData();
}
public toggleColumnPinning (column: ColumnType ) {
column.pinned ? column.unpin() : column.pin();
}
}
ts コピー <ng-template igxHeader let-column #pinTemplate >
<div class ="title-inner" >
<span style ="float:left" > {{column.field}}</span >
<igx-icon class ="pin-icon" [class.pinned ]="column.pinned" [class.unpinned ]="!column.pinned" family ="fas" name ="fa-thumbtack"
[attr.draggable ]="false" (click )="toggleColumnPinning(column)" > </igx-icon >
</div >
</ng-template >
<div class ="grid__wrapper" >
<igx-tree-grid [igxPreventDocumentScroll ]="true" #treeGrid [data ]="data" primaryKey ="ID" [moving ]="true" foreignKey ="ParentID" [autoGenerate ]="false" height ="620px"
width ="100%" >
<igx-paginator [perPage ]="10" > </igx-paginator >
<igx-column [field ]="'Name'" dataType ="string" [headerTemplate ]="pinTemplate" width ="250px" > </igx-column >
<igx-column [field ]="'Title'" dataType ="string" [headerTemplate ]="pinTemplate" width ="250px" > </igx-column >
<igx-column [field ]="'ID'" dataType ="number" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'HireDate'" dataType ="date" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Age'" dataType ="number" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Address'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'City'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Country'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Fax'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'PostalCode'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
<igx-column [field ]="'Phone'" dataType ="string" [headerTemplate ]="pinTemplate" width ="200px" > </igx-column >
</igx-tree-grid >
</div >
html コピー @use '../../variables' as *;
@import url("https://unpkg.com/@fortawesome/fontawesome-free-webfonts@^1.0.9/css/fontawesome.css" );
@import url("https://unpkg.com/@fortawesome/fontawesome-free-webfonts@^1.0.9/css/fa-regular.css" );
@import url("https://unpkg.com/@fortawesome/fontawesome-free-webfonts@^1.0.9/css/fa-solid.css" );
$yellow-color : #F4D45C ;
$black-color : #575757 ;
$dark-palette : palette($primary : $yellow-color , $secondary : $black-color , $surface : #222 );
$dark-grid-column-moving-theme : grid-theme(
$ghost-header-text-color : color($dark-palette , "primary" , 400 ),
$ghost-header-background : color($dark-palette , "secondary" , 200 ),
$ghost-header-icon-color : color($dark-palette , "primary" , 500 )
);
:host {
@include palette($dark-palette );
::ng-deep {
@include css-vars($dark-grid-column-moving-theme );
}
}
.grid__wrapper {
padding : 16px ;
}
.pin-icon {
margin-left : 8px ;
font-size : 14px ;
cursor : pointer;
display : flex;
align-items : center;
}
.pinned {
color : #444 ;
&:hover {
color : #999 ;
}
}
.unpinned {
color : #999 ;
&:hover {
color : #444 ;
}
}
.title-inner {
display : flex;
justify-content : space-between;
align-items : center;
}
scss コピー
このサンプルは、Change Theme
(テーマの変更) から選択したグローバル テーマに影響を受けません。
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。