React Hierarchical Grid 列のサイズ変更の概要
React Hierarchical Grid の Ignite UI for React 列のサイズ変更機能を使用すると、ユーザーは IgrHierarchicalGrid
の列の幅を簡単に調整できます。デフォルトでは、ドラッグによるサイズ変更操作が有効になっている間、一時的なサイズ変更インジケーターが表示されます。利用可能なサイズ変更オプションがいくつかあります - ピクセル/パーセンテージでの列のサイズ変更、列のサイズ変更の制限、ダブルクリック時の列の自動サイズ変更、および初期化時の列の自動サイズ変更。
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, 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({});
}
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"
ref={this.hierarchicalGrid1Ref}>
<IgrColumn
field="Artist"
header="Artist"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Photo"
header="Photo"
dataType="image"
minWidth="115px"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Debut"
header="Debut"
dataType="number"
minWidth="88px"
maxWidth="230px"
resizable={true}>
</IgrColumn>
<IgrColumn
field="GrammyNominations"
header="Grammy Nominations"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="GrammyAwards"
header="Grammy Awards"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrRowIsland
childDataKey="Albums"
autoGenerate={false}>
<IgrColumn
field="Album"
header="Album"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="LaunchDate"
header="Launch Date"
dataType="date"
resizable={true}>
</IgrColumn>
<IgrColumn
field="BillboardReview"
header="Billboard Review"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="USBillboard200"
header="US Billboard 200"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrRowIsland
childDataKey="Songs"
autoGenerate={false}>
<IgrColumn
field="Number"
header="No."
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Title"
header="Title"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Released"
header="Released"
dataType="date"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Genre"
header="Genre"
dataType="string"
resizable={true}>
</IgrColumn>
</IgrRowIsland>
</IgrRowIsland>
<IgrRowIsland
childDataKey="Tours"
autoGenerate={false}>
<IgrColumn
field="Tour"
header="Tour"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="StartedOn"
header="Started on"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Location"
header="Location"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Headliner"
header="Headliner"
dataType="string"
resizable={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ツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
列のサイズ変更は列レベルで有効化にできます。つまり、IgrHierarchicalGrid
にサイズ変更可能な列およびサイズ変更不可の列の両方を含むことが可能です。IgrColumn
の resizable
入力によって制御されます。
<IgrColumn field="Artist" resizable="true"></IgrColumn>
tsx
IgrHierarchicalGrid
の ColumnResized
イベントを処理し、列がサイズ変更されたときにカスタム ロジックを実装できます。以前の列幅、新しい列幅、および IgrColumn
オブジェクトがイベント引数で公開されます。
function onResize(grid: IgrGridBaseDirective, event: IgrColumnResizeEventArgs) {
IgrColumn col = event.detail.column;
string pWidth = event.detail.prevWidth;
string nWidth = event.detail.newWidth;
}
<IgrHierarchicalGrid id="hierarchicalGrid" autoGenerate="false" columnResized={onResize}>
<IgrColumn field="Artist" resizable="true"></IgrColumn>
</IgrHierarchicalGrid>
tsx
ピクセル/パーセンテージで列のサイズを変更する
ユーザーのシナリオに応じて、列の幅はピクセル、パーセンテージ、または両方の組み合わせで定義できます。これらのシナリオはすべて、列のサイズ変更機能でサポートされています。デフォルトでは、列に幅が設定されていない場合、ピクセルで設定された幅の使用可能なスペースに収まります。
つまり、次の構成が可能です。
<IgrHierarchicalGrid id="hierarchicalGrid" columnResized={onResize} autoGenerate="false"
height="600px" width="100%">
<IgrColumn field="Artist" resizable="true" width="10%"></IgrColumn>
<IgrColumn field="GrammyNominations" resizable="true" width="100px"></IgrColumn>
<IgrColumn field="GrammyAwards" resizable="true"></IgrColumn>
</IgrHierarchicalGrid>
tsx
ピクセルとパーセンテージで設定された場合で列のサイズ変更の動作は少々異なります。
ピクセル
幅がピクセルで設定された列のサイズ変更は、マウスの水平移動量を列のサイズに直接足したり引いたりして行なわれます。
パーセンテージ
幅がパーセンテージで設定された列のサイズを変更する場合、ピクセル単位のマウスの水平移動量は、ほぼグリッド幅に対するパーセンテージの量に変換されます。列はレスポンシブな状態のまま、その後のグリッドのサイズ変更は列にも反映されます。
列のサイズ変更の制限
列の最小幅および最大幅の構成も可能です。IgrColumn
の minWidth
および maxWidth
入力によって制御されます。この場合、サイズ変更インジケーターのドラッグ操作が制限されます。列が minWidth
および maxWidth
によって定義される範囲以外にサイズ変更できないことをユーザーに通知します。
<IgrColumn field="Artist" width="100px" resizable="true"
minWidth="60px" maxWidth="230px"></IgrColumn>
tsx
列幅の最小値と最大値のタイプ (ピクセルまたはパーセンテージ) を混在させることができます。最小値と最大値がパーセンテージに設定されている場合、それぞれの列サイズはピクセルと同様の正確なサイズに制限されます。
つまり、次の構成が可能です。
<IgrColumn field="Artist" width="100px" resizable="true"
minWidth="60px" maxWidth="230px"></IgrColumn>
tsx
または
<IgrColumn field="Artist" width="100px" resizable="true"
minWidth="60px" maxWidth="15%"></IgrColumn>
tsx
ダブルクリックで列の自動サイズ調整
各列ヘッダーの右側をダブルクリックして列を自動サイズ調整することができます。列は、現在表示されているヘッダーを含む一番長いセル値にサイズ設定されます。この動作はデフォルトで有効なため、追加で構成する必要はありません。ただし、maxWidth
がその列に設定され、新しい幅が maxWidth
値より大きい場合、列は自動サイズ調整されません。この場合、列が maxWidth
値に設定されます。
また、IgrColumn
の autosize
メソッドで列を動的に自動でサイズ変更できます。
const column = grid.getColumnByName('Artist');
column.autosize();
tsx
初期化時に列を自動サイズ調整
width
を 'auto' に設定することで、初期化時に各列を自動サイズに設定できます。
<IgrColumn width='auto'>
tsx
列がビューで最初に初期化されるとき、その幅は、表示されている最も長いセルまたはヘッダーのサイズに調整されます。表示されている行の外側にあるセルは含まれないことに注意してください。
このアプローチは、初期化後の自動サイズ変更よりもパフォーマンスが最適化されており、特に多数の列のサイズを自動サイズ設定する必要がある場合に推奨されます。
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { IgrHierarchicalGridModule } from "@infragistics/igniteui-react-grids";
import { IgrHierarchicalGrid, IgrColumn, IgrRowIsland } from "@infragistics/igniteui-react-grids";
import HierarchicalCustomers from './HierarchicalCustomers.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({});
}
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}
ref={this.hierarchicalGrid1Ref}>
<IgrColumn
field="CustomerID"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="CompanyName"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ContactName"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ContactTitle"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Address"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="City"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="PostalCode"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Country"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Phone"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Fax"
width="auto"
resizable={true}>
</IgrColumn>
<IgrRowIsland
childDataKey="Orders"
autoGenerate={false}>
<IgrColumn
field="OrderID"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="EmployeeID"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="OrderDate"
width="auto"
dataType="Date"
resizable={true}>
</IgrColumn>
<IgrColumn
field="RequiredDate"
width="auto"
dataType="Date"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ShippedDate"
width="auto"
dataType="Date"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ShipVia"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Freight"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ShipName"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ShipAddress"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ShipCity"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ShipPostalCode"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="ShipCountry"
width="auto"
resizable={true}>
</IgrColumn>
<IgrRowIsland
childDataKey="OrderDetails"
autoGenerate={false}>
<IgrColumn
field="ProductID"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="UnitPrice"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Quantity"
width="auto"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Discount"
width="auto"
resizable={true}>
</IgrColumn>
</IgrRowIsland>
</IgrRowIsland>
</IgrHierarchicalGrid>
</div>
</div>
);
}
private _hierarchicalCustomers: any[] = HierarchicalCustomers;
public get hierarchicalCustomers(): any[] {
return this._hierarchicalCustomers;
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Sample/>);
tsx
スタイル設定
定義済みのテーマに加えて、利用可能な CSS プロパティのいくつかを設定することで、グリッドをさらにカスタマイズできます。
サイズ変更ハンドルの色を変更したい場合は、最初にグリッドのクラスを設定する必要があります。
<IgrHierarchicalGrid className="grid"></IgrHierarchicalGrid>
tsx
次に、そのクラスに関連する CSS プロパティを設定します:
.grid {
--ig-grid-resize-line-color: #f35b04;
}
css
デモ
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { IgrHierarchicalGridModule } from "@infragistics/igniteui-react-grids";
import { IgrHierarchicalGrid, 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 grid: IgrHierarchicalGrid
private gridRef(r: IgrHierarchicalGrid) {
this.grid = r;
this.setState({});
}
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}
ref={this.gridRef}
id="grid"
data={this.singersData}
primaryKey="ID">
<IgrColumn
field="Artist"
header="Artist"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Photo"
header="Photo"
dataType="image"
minWidth="115px"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Debut"
header="Debut"
dataType="number"
minWidth="88px"
maxWidth="230px"
resizable={true}>
</IgrColumn>
<IgrColumn
field="GrammyNominations"
header="Grammy Nominations"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="GrammyAwards"
header="Grammy Awards"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrRowIsland
childDataKey="Albums"
autoGenerate={false}>
<IgrColumn
field="Album"
header="Album"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="LaunchDate"
header="Launch Date"
dataType="date"
resizable={true}>
</IgrColumn>
<IgrColumn
field="BillboardReview"
header="Billboard Review"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="USBillboard200"
header="US Billboard 200"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrRowIsland
childDataKey="Songs"
autoGenerate={false}>
<IgrColumn
field="Number"
header="No."
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Title"
header="Title"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Released"
header="Released"
dataType="date"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Genre"
header="Genre"
dataType="string"
resizable={true}>
</IgrColumn>
</IgrRowIsland>
</IgrRowIsland>
<IgrRowIsland
childDataKey="Tours"
autoGenerate={false}>
<IgrColumn
field="Tour"
header="Tour"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="StartedOn"
header="Started on"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Location"
header="Location"
dataType="string"
resizable={true}>
</IgrColumn>
<IgrColumn
field="Headliner"
header="Headliner"
dataType="string"
resizable={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
#grid {
--ig-grid-resize-line-color: #f35b04;
}
css
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。