React Hierarchical Grid ソート
React Hierarchical Grid の Ignite UI for React データ ソート機能は列ごとのレベルで有効になっています。つまり、IgrHierarchicalGrid
にはソート可能な列とソート不可能な列を混在させることができます。React でソートを実行すると、指定した条件に基づいてレコードの表示順序を変更できます。
React Hierarchical Grid ソート概要の例
以下のデモは、IgrHierarchicalGrid
の ContextMenu
出力を使用してカスタム コンテキスト メニューを追加しています。
import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { IgrHierarchicalGridModule } from "@infragistics/igniteui-react-grids" ;
import { IgrHierarchicalGrid, IgrSortingExpression, SortingDirection, IgrColumn, IgrRowIsland } from "@infragistics/igniteui-react-grids" ;
import SingersData from './SingersData.json' ;
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 _sortingExpression1: IgrSortingExpression[] | null = null ;
public get sortingExpression1(): IgrSortingExpression[] {
if (this ._sortingExpression1 == null )
{
let sortingExpression1 : IgrSortingExpression [] = [];
var sortingExpression2: IgrSortingExpression = {} as IgrSortingExpression;
sortingExpression2.fieldName = "Artist" ;
sortingExpression2.dir = SortingDirection.Asc;
sortingExpression2.ignoreCase = true ;
sortingExpression1.push(sortingExpression2)
this ._sortingExpression1 = sortingExpression1;
}
return this ._sortingExpression1;
}
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.singersData}
primaryKey ="ID"
sortingExpressions ={this.sortingExpression1}
ref ={this.hierarchicalGrid1Ref} >
<IgrColumn
field ="Artist"
header ="Artist"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Photo"
header ="Photo"
dataType ="image" >
</IgrColumn >
<IgrColumn
field ="Debut"
header ="Debut"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="GrammyNominations"
header ="Grammy Nominations"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="GrammyAwards"
header ="Grammy Awards"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrRowIsland
childDataKey ="Albums"
autoGenerate ={false} >
<IgrColumn
field ="Album"
header ="Album"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="LaunchDate"
header ="Launch Date"
dataType ="date"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="BillboardReview"
header ="Billboard Review"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="USBillboard200"
header ="US Billboard 200"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrRowIsland
childDataKey ="Songs"
autoGenerate ={false} >
<IgrColumn
field ="Number"
header ="No."
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Title"
header ="Title"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Released"
header ="Released"
dataType ="date"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Genre"
header ="Genre"
dataType ="string"
sortable ={true} >
</IgrColumn >
</IgrRowIsland >
</IgrRowIsland >
<IgrRowIsland
childDataKey ="Tours"
autoGenerate ={false} >
<IgrColumn
field ="Tour"
header ="Tour"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="StartedOn"
header ="Started on"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Location"
header ="Location"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Headliner"
header ="Headliner"
dataType ="string"
sortable ={true} >
</IgrColumn >
</IgrRowIsland >
</IgrHierarchicalGrid >
</div >
</div >
);
}
private _singersData: any [] = SingersData;
public get singersData(): any [] {
return this ._singersData;
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<Sample /> );
tsx コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Reactツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
以下のように sortable
入力を使用します。IgrHierarchicalGrid
のソートで、sortingIgnoreCase
プロパティを設定して大文字と小文字を区別するソートができます。
<IgrColumn field ="ProductName" header ="Product Name" dataType ="string" sortable ="true" > </IgrColumn >
tsx
ソート インジケーター
ソートされた列数が一定数以上ある場合、ソート順の指定がないと混乱する可能性があります。
IgrHierarchicalGrid
は、ソートされた各列のインデックスを示すことにより、この問題の解決策を提供します。
import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { IgrHierarchicalGridModule } from "@infragistics/igniteui-react-grids" ;
import { IgrHierarchicalGrid, IgrSortingExpression, SortingDirection, IgrColumn, IgrRowIsland } from "@infragistics/igniteui-react-grids" ;
import SingersData from './SingersData.json' ;
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 _sortingExpression1: IgrSortingExpression[] | null = null ;
public get sortingExpression1(): IgrSortingExpression[] {
if (this ._sortingExpression1 == null )
{
let sortingExpression1 : IgrSortingExpression [] = [];
var sortingExpression2: IgrSortingExpression = {} as IgrSortingExpression;
sortingExpression2.dir = SortingDirection.Asc;
sortingExpression2.fieldName = "Artist" ;
sortingExpression2.ignoreCase = true ;
sortingExpression1.push(sortingExpression2)
var sortingExpression3: IgrSortingExpression = {} as IgrSortingExpression;
sortingExpression3.dir = SortingDirection.Desc;
sortingExpression3.fieldName = "Debut" ;
sortingExpression3.ignoreCase = true ;
sortingExpression1.push(sortingExpression3)
var sortingExpression4: IgrSortingExpression = {} as IgrSortingExpression;
sortingExpression4.dir = SortingDirection.Asc;
sortingExpression4.fieldName = "GrammyNominations" ;
sortingExpression4.ignoreCase = true ;
sortingExpression1.push(sortingExpression4)
var sortingExpression5: IgrSortingExpression = {} as IgrSortingExpression;
sortingExpression5.dir = SortingDirection.Asc;
sortingExpression5.fieldName = "GrammyAwards" ;
sortingExpression5.ignoreCase = true ;
sortingExpression1.push(sortingExpression5)
this ._sortingExpression1 = sortingExpression1;
}
return this ._sortingExpression1;
}
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.singersData}
primaryKey ="ID"
sortingExpressions ={this.sortingExpression1}
ref ={this.hierarchicalGrid1Ref} >
<IgrColumn
field ="Artist"
header ="Artist"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Photo"
header ="Photo"
dataType ="image" >
</IgrColumn >
<IgrColumn
field ="Debut"
header ="Debut"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="GrammyNominations"
header ="Grammy Nominations"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="GrammyAwards"
header ="Grammy Awards"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrRowIsland
childDataKey ="Albums"
autoGenerate ={false} >
<IgrColumn
field ="Album"
header ="Album"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="LaunchDate"
header ="Launch Date"
dataType ="date"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="BillboardReview"
header ="Billboard Review"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="USBillboard200"
header ="US Billboard 200"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrRowIsland
childDataKey ="Songs"
autoGenerate ={false} >
<IgrColumn
field ="Number"
header ="No."
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Title"
header ="Title"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Released"
header ="Released"
dataType ="date"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Genre"
header ="Genre"
dataType ="string"
sortable ={true} >
</IgrColumn >
</IgrRowIsland >
</IgrRowIsland >
<IgrRowIsland
childDataKey ="Tours"
autoGenerate ={false} >
<IgrColumn
field ="Tour"
header ="Tour"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="StartedOn"
header ="Started on"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Location"
header ="Location"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Headliner"
header ="Headliner"
dataType ="string"
sortable ={true} >
</IgrColumn >
</IgrRowIsland >
</IgrHierarchicalGrid >
</div >
</div >
);
}
private _singersData: any [] = SingersData;
public get singersData(): any [] {
return this ._singersData;
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<Sample /> );
tsx コピー
API でのソート
IgrHierarchicalGrid
sort
メソッドを使用し、列または複数の列を IgrHierarchicalGrid
API でソートできます。
import { SortingDirection } from "igniteui-react-grids" ;
tsx
hierarchicalGridRef.current.sort([{ fieldName: 'ProductName' , dir: SortingDirection.Asc, ignoreCase: true }]);
hierarchicalGridRef.current.sort([
{ fieldName: 'ProductName' , dir: SortingDirection.Asc, ignoreCase: true },
{ fieldName: 'Price' , dir: SortingDirection.Desc }
]);
tsx
Sorting は、DefaultSortingStrategy アルゴリズムを使用して実行されます。IgrColumn または ISortingExpression は、代替アルゴリズムとして ISortingStrategy のカスタム実装を使用できます。たとえば複雑なテンプレート列や画像列にユーザー定義のソートを定義する必要がある場合に便利です。
フィルター動作と同様に、ソート状態をクリアするには clearSort
メソッドを使用します。
hierarchicalGridRef.current.clearSort('ProductName' );
hierarchicalGridRef.current.clearSort();
tsx
IgrHierarchicalGrid の sortStrategy は IgrColumn の sortStrategy と比較して異なるタイプです。異なるスコープで機能し、異なるパラメーターを公開するためです。
ソート操作で IgrHierarchicalGrid の基になるデータ ソースは変更しません。
初期のソート状態
IgrHierarchicalGrid
でソート状態を初期設定するには、ソート式の配列を IgrHierarchicalGrid
の sortingExpressions
プロパティに渡します。
useEffect(() => {
hierarchicalGridRef.current.sortingExpressions = [
{ fieldName: 'UnitsInStock' , dir: SortingDirection.Asc, ignoreCase: true },
{ fieldName: 'ProductName' , dir: SortingDirection.Desc }
];
}, [])
tsx
string 型の値が dataType Date の列で使用される場合、IgrHierarchicalGrid が値を Date オブジェクトに解析しないため IgrHierarchicalGrid Sorting が正しく動作しません。string オブジェクトを使用する場合、値を Date オブジェクトに解析するためのロジックをアプリケーション レベルで実装する必要があります。
ソート インジケーター テンプレート
列ヘッダーのソート インジケーター アイコンは、テンプレートを使用してカスタマイズできます。次のプロパティは、任意のソート状態 (昇順、降順、なし) のソート インジケーターをテンプレート化するために使用できます。
function sortHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
return (
<>
<IgrIcon name ='unfold_more' > </IgrIcon >
</>
);
}
<IgrHierarchicalGrid sortHeaderIconTemplate ={sortHeaderIconTemplate} > </IgrHierarchicalGrid >
tsx
function sortAscendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
return (
<>
<IgrIcon name ='expand_less' > </IgrIcon >
</>
);
}
<IgrHierarchicalGrid sortAscendingHeaderIconTemplate ={sortAscendingHeaderIconTemplate} > </IgrHierarchicalGrid >
tsx
function sortDescendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
return (
<>
<IgrIcon name ='expand_more' > </IgrIcon >
</>
);
}
<IgrHierarchicalGrid sortDescendingHeaderIconTemplate ={sortDescendingHeaderIconTemplate} > </IgrHierarchicalGrid >
tsx
スタイル設定
定義済みのテーマに加えて、利用可能な CSS プロパティ のいくつかを設定することで、グリッドをさらにカスタマイズできます。
一部の色を変更したい場合は、最初にグリッドのクラスを設定する必要があります。
<IgrHierarchicalGrid className ="grid" >
</IgrHierarchicalGrid >
tsx
次に、そのクラスに関連する CSS プロパティを設定します。
.grid {
--ig-grid-sorted-header -icon -color : #ffb06a ;
--ig-grid-sortable-header -icon -hover-color : black;
}
css
デモ
import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { IgrHierarchicalGridModule } from "@infragistics/igniteui-react-grids" ;
import { IgrHierarchicalGrid, IgrSortingExpression, SortingDirection, IgrColumn, IgrRowIsland } from "@infragistics/igniteui-react-grids" ;
import SingersData from './SingersData.json' ;
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 hierarchicalGrid: IgrHierarchicalGrid
private hierarchicalGridRef(r: IgrHierarchicalGrid) {
this .hierarchicalGrid = r;
this .setState({});
}
private _sortingExpression1: IgrSortingExpression[] | null = null ;
public get sortingExpression1(): IgrSortingExpression[] {
if (this ._sortingExpression1 == null )
{
let sortingExpression1 : IgrSortingExpression [] = [];
var sortingExpression2: IgrSortingExpression = {} as IgrSortingExpression;
sortingExpression2.fieldName = "Artist" ;
sortingExpression2.dir = SortingDirection.Asc;
sortingExpression2.ignoreCase = true ;
sortingExpression1.push(sortingExpression2)
this ._sortingExpression1 = sortingExpression1;
}
return this ._sortingExpression1;
}
constructor (props: any ) {
super (props);
this .hierarchicalGridRef = this .hierarchicalGridRef.bind(this );
}
public render (): JSX .Element {
return (
<div className ="container sample ig-typography" >
<div className ="container fill" >
<IgrHierarchicalGrid
autoGenerate ={false}
data ={this.singersData}
ref ={this.hierarchicalGridRef}
id ="hierarchicalGrid"
primaryKey ="ID"
sortingExpressions ={this.sortingExpression1} >
<IgrColumn
field ="Artist"
header ="Artist"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Photo"
header ="Photo"
dataType ="image" >
</IgrColumn >
<IgrColumn
field ="Debut"
header ="Debut"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="GrammyNominations"
header ="Grammy Nominations"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="GrammyAwards"
header ="Grammy Awards"
dataType ="number"
sortable ={true} >
</IgrColumn >
<IgrRowIsland
childDataKey ="Albums"
autoGenerate ={false} >
<IgrColumn
field ="Album"
header ="Album"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="LaunchDate"
header ="Launch Date"
dataType ="date"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="BillboardReview"
header ="Billboard Review"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="USBillboard200"
header ="US Billboard 200"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrRowIsland
childDataKey ="Songs"
autoGenerate ={false} >
<IgrColumn
field ="Number"
header ="No."
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Title"
header ="Title"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Released"
header ="Released"
dataType ="date"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Genre"
header ="Genre"
dataType ="string"
sortable ={true} >
</IgrColumn >
</IgrRowIsland >
</IgrRowIsland >
<IgrRowIsland
childDataKey ="Tours"
autoGenerate ={false} >
<IgrColumn
field ="Tour"
header ="Tour"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="StartedOn"
header ="Started on"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Location"
header ="Location"
dataType ="string"
sortable ={true} >
</IgrColumn >
<IgrColumn
field ="Headliner"
header ="Headliner"
dataType ="string"
sortable ={true} >
</IgrColumn >
</IgrRowIsland >
</IgrHierarchicalGrid >
</div >
</div >
);
}
private _singersData: any [] = SingersData;
public get singersData(): any [] {
return this ._singersData;
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<Sample /> );
tsx コピー
#hierarchicalGrid {
--ig-grid-sorted-header -icon -color : #ffb06a ;
--ig-grid-sortable-header -icon -hover-color : black;
}
css コピー
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。