Hierarchical Grid の列の並べ替えと移動
React Hierarchical Grid の Ignite UI for React 列移動機能を使用すると、列をすばやく簡単に並べ替えることができます。これは、列移動 API を使用するか、マウスまたはタッチ ジェスチャを使用してヘッダーを別の位置にドラッグ アンド ドロップすることによって実行できます。React Hierarchical Grid では、ピン固定された列とピン固定されていない列、および複数列ヘッダー に対しても列の移動を有効にすることができます。
列と列グループ間の順序変更は、それらが階層の同じレベルにあり、両方が同じグループにある場合にのみ許可されます。列/列グループが最上位の列である場合、列/列グループ間を移動できます。
列ヘッダーがテンプレート化され、対応する列がグループ化可能である場合、テンプレート化された要素は draggable 属性を false に設定する必要があります。
ピン固定領域が最大幅 (IgrHierarchicalGrid 幅合計の 80%) を超えた場合、ドロップ操作が禁止されていてピン固定ができないことをヒントの表示でエンドユーザーに通知します。つまり、ピン固定領域に列をドロップできません。
function headerTemplate(ctx: IgrCellTemplateContext) {
return (
<>
<IgrIcon draggable ="false" onClick ={onClick} > </IgrIcon >
</>
);
}
tsx
React Hierarchical Grid 列移動概要の例
import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { IgrHierarchicalGridModule } from "@infragistics/igniteui-react-grids" ;
import { IgrHierarchicalGrid, IgrPaginator, IgrColumn, IgrRowIsland } from "@infragistics/igniteui-react-grids" ;
import HierarchicalCustomers from './HierarchicalCustomers.json' ;
import { IgrColumnTemplateContext } from "@infragistics/igniteui-react-grids" ;
import "@infragistics/igniteui-react-grids/grids/combined" ;
import "@infragistics/igniteui-react-grids/grids/themes/light/bootstrap.css" ;
const mods : any [] = [
IgrHierarchicalGridModule
];
mods.forEach((m) => m.register());
export default class Sample extends React.Component <any, any> {
private hierarchicalGrid1: IgrHierarchicalGrid
private hierarchicalGrid1Ref(r: IgrHierarchicalGrid) {
this .hierarchicalGrid1 = r;
this .setState({});
}
private paginator: IgrPaginator
private column1: IgrColumn
private column2: IgrColumn
private column3: IgrColumn
private column4: IgrColumn
private column5: IgrColumn
private column6: IgrColumn
private column7: IgrColumn
private column8: IgrColumn
private column9: IgrColumn
private column10: IgrColumn
constructor (props: any ) {
super (props);
this .hierarchicalGrid1Ref = this .hierarchicalGrid1Ref.bind(this );
}
public render (): JSX .Element {
return (
<div className ="container sample ig-typography" >
<div className ="container fill" >
<IgrHierarchicalGrid
autoGenerate ={false}
data ={this.hierarchicalCustomers}
moving ={true}
ref ={this.hierarchicalGrid1Ref} >
<IgrPaginator
name ="paginator"
perPage ={15} >
</IgrPaginator >
<IgrColumn
field ="CustomerID"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column1" >
</IgrColumn >
<IgrColumn
field ="Company"
dataType ="string"
width ="150px"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column2" >
</IgrColumn >
<IgrColumn
field ="ContactName"
dataType ="string"
width ="150px"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column3" >
</IgrColumn >
<IgrColumn
field ="ContactTitle"
dataType ="string"
width ="150px"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column4" >
</IgrColumn >
<IgrColumn
field ="Address"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column5" >
</IgrColumn >
<IgrColumn
field ="City"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column6" >
</IgrColumn >
<IgrColumn
field ="PostalCode"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column7" >
</IgrColumn >
<IgrColumn
field ="Country"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column8" >
</IgrColumn >
<IgrColumn
field ="Phone"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column9" >
</IgrColumn >
<IgrColumn
field ="Fax"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column10" >
</IgrColumn >
<IgrRowIsland
childDataKey ="Orders"
autoGenerate ={false}
moving ={true} >
<IgrColumn
field ="OrderID"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="EmployeeID"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="OrderDate"
dataType ="date" >
</IgrColumn >
<IgrColumn
field ="RequiredDate"
dataType ="date" >
</IgrColumn >
<IgrColumn
field ="ShippedDate"
dataType ="date" >
</IgrColumn >
<IgrColumn
field ="ShipVia"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="Freight"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="ShipName"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipAddress"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipCity"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipPostalCode"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipCountry"
dataType ="string" >
</IgrColumn >
<IgrRowIsland
childDataKey ="OrderDetails"
autoGenerate ={false}
moving ={true} >
<IgrColumn
field ="ProductID"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="UnitPrice"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="Quantity"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="Discount"
dataType ="number" >
</IgrColumn >
</IgrRowIsland >
</IgrRowIsland >
</IgrHierarchicalGrid >
</div >
</div >
);
}
private _hierarchicalCustomers: any [] = HierarchicalCustomers;
public get hierarchicalCustomers(): any [] {
return this ._hierarchicalCustomers;
}
public hierarchicalGridPinHeaderTemplate = (props: {dataContext: IgrColumnTemplateContext}) => {
const column = (props.dataContext as any ).column;
return (
<div >
<span style ={{float: 'left '}}> {column.field}</span >
<span style ={{float: 'right '}} onPointerDown ={(e: any ) => this .toggleColumnPin(column)}>📌</span >
</div >
);
}
public toggleColumnPin(field: IgrColumn) {
if (field) {
field.pinned = !field.pinned;
}
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<Sample /> );
tsx コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Reactツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
概要
列移動 機能は各列レベルで有効にできます。つまり、IgrHierarchicalGrid
に移動可能な列または移動不可の列の両方を含むことができます。IgrHierarchicalGrid
の moving
入力によって制御されます。
<IgrHierarchicalGrid moving ="true" >
...
<IgrRowIsland moving ="true" > </IgrRowIsland >
</IgrHierarchicalGrid >
tsx
API
ドラッグアンドドロップ機能に加えて、列の移動機能には、プログラムで列を移動/並べ替えできる 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
列移動機能を使用する場合、操作が成功すると、ColumnMovingEnd
イベントが発生することに注意してください。また、ドラッグアンドドロップ機能と比較して、列移動機能を使用するために moving
プロパティを true に設定する必要がないことにも注意してください。
イベント
列のドラッグアンドドロップ操作を可能にする列移動に関連するイベントが複数あります。ColumnMovingStart
、ColumnMoving
、および ColumnMovingEnd
です。
IgrHierarchicalGrid
の ColumnMovingEnd
イベントを処理し、列が新しい位置にドロップされたときにカスタム ロジックを実装できます。たとえば、以下のスニペットでは、Change On Year(%) 列の後に Category のドロップをキャンセルできます。
function onColumnMovingEnd(grid: IgrGridBaseDirective, event: IgrColumnMovingEventArgs) {
if (event.detail.source.field === "Category" && event.detail.target.field === "Change On Year(%)" ) {
event.detail.cancel = true ;
}
}
<IgrHierarchicalGrid autoGenerate ="false" moving ="true" data ={data} columnMovingEnd ={onColumnMovingEnd} >
<IgrColumn field ="Category" > </IgrColumn >
<IgrColumn field ="Change On Year(%)" dataType ="Number" > </IgrColumn >
</IgrHierarchicalGrid >
tsx
スタイル設定
定義済みのテーマに加えて、利用可能な CSS プロパティ のいくつかを設定することで、グリッドをさらにカスタマイズできます。
色を変更したい場合は、最初にグリッドのクラスを設定する必要があります:
<IgrHierarchicalGrid className ="grid" > </IgrHierarchicalGrid >
tsx
次に、関連する CSS プロパティをこのクラスに設定します:
.grid {
--ig-grid-ghost-header -text-color : #f4d45c ;
--ig-grid-ghost-header -background : #ad9d9d ;
--ig-grid-ghost-header -icon -color : #f4d45c ;
}
css
デモ
import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { IgrHierarchicalGridModule } from "@infragistics/igniteui-react-grids" ;
import { IgrHierarchicalGrid, IgrPaginator, IgrColumn, IgrRowIsland } from "@infragistics/igniteui-react-grids" ;
import HierarchicalCustomers from './HierarchicalCustomers.json' ;
import { IgrColumnTemplateContext } from "@infragistics/igniteui-react-grids" ;
import "@infragistics/igniteui-react-grids/grids/combined" ;
import "@infragistics/igniteui-react-grids/grids/themes/light/bootstrap.css" ;
const mods : any [] = [
IgrHierarchicalGridModule
];
mods.forEach((m) => m.register());
export default class Sample extends React.Component <any, any> {
private grid: IgrHierarchicalGrid
private gridRef(r: IgrHierarchicalGrid) {
this .grid = r;
this .setState({});
}
private paginator: IgrPaginator
private column1: IgrColumn
private column2: IgrColumn
private column3: IgrColumn
private column4: IgrColumn
private column5: IgrColumn
private column6: IgrColumn
private column7: IgrColumn
private column8: IgrColumn
private column9: IgrColumn
private column10: IgrColumn
constructor (props: any ) {
super (props);
this .gridRef = this .gridRef.bind(this );
}
public render (): JSX .Element {
return (
<div className ="container sample ig-typography" >
<div className ="container fill" >
<IgrHierarchicalGrid
autoGenerate ={false}
data ={this.hierarchicalCustomers}
moving ={true}
ref ={this.gridRef}
id ="grid" >
<IgrPaginator
name ="paginator"
perPage ={15} >
</IgrPaginator >
<IgrColumn
field ="CustomerID"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column1" >
</IgrColumn >
<IgrColumn
field ="Company"
dataType ="string"
width ="150px"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column2" >
</IgrColumn >
<IgrColumn
field ="ContactName"
dataType ="string"
width ="150px"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column3" >
</IgrColumn >
<IgrColumn
field ="ContactTitle"
dataType ="string"
width ="150px"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column4" >
</IgrColumn >
<IgrColumn
field ="Address"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column5" >
</IgrColumn >
<IgrColumn
field ="City"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column6" >
</IgrColumn >
<IgrColumn
field ="PostalCode"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column7" >
</IgrColumn >
<IgrColumn
field ="Country"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column8" >
</IgrColumn >
<IgrColumn
field ="Phone"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column9" >
</IgrColumn >
<IgrColumn
field ="Fax"
dataType ="string"
headerTemplate ={this.hierarchicalGridPinHeaderTemplate}
name ="column10" >
</IgrColumn >
<IgrRowIsland
childDataKey ="Orders"
autoGenerate ={false}
moving ={true} >
<IgrColumn
field ="OrderID"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="EmployeeID"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="OrderDate"
dataType ="date" >
</IgrColumn >
<IgrColumn
field ="RequiredDate"
dataType ="date" >
</IgrColumn >
<IgrColumn
field ="ShippedDate"
dataType ="date" >
</IgrColumn >
<IgrColumn
field ="ShipVia"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="Freight"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="ShipName"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipAddress"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipCity"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipPostalCode"
dataType ="string" >
</IgrColumn >
<IgrColumn
field ="ShipCountry"
dataType ="string" >
</IgrColumn >
<IgrRowIsland
childDataKey ="OrderDetails"
autoGenerate ={false}
moving ={true} >
<IgrColumn
field ="ProductID"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="UnitPrice"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="Quantity"
dataType ="number" >
</IgrColumn >
<IgrColumn
field ="Discount"
dataType ="number" >
</IgrColumn >
</IgrRowIsland >
</IgrRowIsland >
</IgrHierarchicalGrid >
</div >
</div >
);
}
private _hierarchicalCustomers: any [] = HierarchicalCustomers;
public get hierarchicalCustomers(): any [] {
return this ._hierarchicalCustomers;
}
public hierarchicalGridPinHeaderTemplate = (props: {dataContext: IgrColumnTemplateContext}) => {
const column = (props.dataContext as any ).column;
return (
<div >
<span style ={{float: 'left '}}> {column.field}</span >
<span style ={{float: 'right '}} onPointerDown ={(e: any ) => this .toggleColumnPin(column)}>📌</span >
</div >
);
}
public toggleColumnPin(field: IgrColumn) {
if (field) {
field.pinned = !field.pinned;
}
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<Sample /> );
tsx コピー
#grid {
--ig-grid-ghost-header -text-color : #f4d45c ;
--ig-grid-ghost-header -background : #ad9d9d ;
--ig-grid-ghost-header -icon -color : #f4d45c ;
}
css コピー
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。