Blazor Hierarchical Grid の行ドラッグ
Blazor Hierarchical Grid の Ignite UI for Blazor 行ドラッグ機能は簡単に構成でき、マウスを使用して行を新しい位置にドラッグ アンド ドロップすることで、グリッド内の行を再配置するために使用されます。これはルートの IgbHierarchicalGrid コンポーネントで初期化され、IgbHierarchicalGrid.RowDraggable 入力を介して構成できます。
Blazor Hierarchical Grid 行ドラッグの例
構成
IgbHierarchicalGrid の行ドラッグを有効にするには、グリッドの IgbHierarchicalGrid.RowDraggable を true に設定するだけです。これが有効になると、行ドラッグ ハンドルが各行に表示されます。このハンドルを使用して行ドラッグを開始できます。ドラッグ ハンドルをクリックし、マウスボタンを押したままカーソルを移動すると、グリッドの IgbHierarchicalGrid.RowDragStart イベントが発生します。ドラッグ中にマウスボタンを離すと IgbHierarchicalGrid.RowDragEnd イベントが発生します。
<IgbHierarchicalGrid RowDraggable="true">
</IgbHierarchicalGrid>
ドラッグ アイコンのテンプレート化
ドラッグ ハンドル アイコンは、グリッドの DragIndicatorIconTemplate を使用してテンプレート化できます。作成中の例では、デフォルトのアイコン (drag_indicator) を drag_handle に変更してみましょう。
<IgbHierarchicalGrid Data="CustomersData" PrimaryKey="ID" RowDraggable="true" DragIndicatorIconTemplate="dragIndicatorIconTemplate" @ref="grid">
</IgbHierarchicalGrid>
private RenderFragment<IgbGridEmptyTemplateContext> dragIndicatorIconTemplate = (context) =>
{
return @<div>
<IgbIcon IconName="drag_handle" Collection="material"></IgbIcon>
</div>;
};
デモ
アプリケーション デモ
行の並べ替えデモ
グリッドの行ドラッグ イベントを使用すると、行をドラッグして並べ替えられるグリッドを作成できます。
<IgbHierarchicalGrid Data="CustomersData" PrimaryKey="ID" RowDraggable="true" RowDragEndScript="WebHierarchicalGridReorderRowHandler"></IgbHierarchicalGrid>
// In JavaScript
igRegisterScript("WebHierarchicalGridReorderRowHandler", (args) => {
const ghostElement = args.detail.dragDirective.ghostElement;
const dragElementPos = ghostElement.getBoundingClientRect();
const grid = document.getElementsByTagName["igc-hierarchical-grid"](0);
const rows = Array.prototype.slice.call(document.getElementsByTagName("igx-hierarchical-grid-row"));
const currRowIndex = this.getCurrentRowIndex(rows,
{ x: dragElementPos.x, y: dragElementPos.y });
if (currRowIndex === -1) { return; }
// remove the row that was dragged and place it onto its new location
grid.deleteRow(args.detail.dragData.key);
grid.data.splice(currRowIndex, 0, args.detail.dragData.data);
}, false);
function getCurrentRowIndex(rowList, cursorPosition) {
for (const row of rowList) {
const rowRect = row.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 the index of the targeted row
return parseInt(row.attributes["data-rowindex"].value);
}
}
return -1;
}
グリッドに IgbHierarchicalGrid.PrimaryKey が指定されていることを確認してください。行を正しく並べ替えるには、一意の識別子が必要です。
IgbHierarchicalGrid.RowDraggable が有効になり、ドロップ エリアが定義されたら、ドロップ イベント用の簡単なハンドラーを実装する必要があります。行がドラッグされたときは、次を確認します。
- 行は展開されているか。展開されている場合は折りたたむ
- 行はグリッド内にドロップされたか
- ドロップされた場合、ドラッグされた行が別のどの行にドロップされたか
- ターゲット行が見つかったら、
Data配列内でレコードの位置を入れ替える - 行は元々選択されていたか。選択されていた場合は、選択済みとしてマークする
以下は、その実装例です。
<IgbHierarchicalGrid Data="SingersData" PrimaryKey="ID" RowDraggable="true" RowDragEndScript="WebGridReorderRowHandler"></IgbHierarchicalGrid>
//in JavaScript
igRegisterScript("WebGridReorderRowHandler", (args) => {
const ghostElement = args.detail.dragDirective.ghostElement;
const dragElementPos = ghostElement.getBoundingClientRect();
const grid = document.getElementsByTagName["igc-hierarchical-grid"](0);
const rows = Array.prototype.slice.call(document.getElementsByTagName("igx-hierarchical-grid-row"));
const currRowIndex = this.getCurrentRowIndex(rows,
{ x: dragElementPos.x, y: dragElementPos.y });
if (currRowIndex === -1) { return; }
// remove the row that was dragged and place it onto its new location
grid.deleteRow(args.detail.dragData.key);
grid.data.splice(currRowIndex, 0, args.detail.dragData.data);
}, false);
function getCurrentRowIndex(rowList, cursorPosition) {
for (const row of rowList) {
const rowRect = row.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 the index of the targeted row
return parseInt(row.attributes["data-rowindex"].value);
}
}
return -1;
}
これらの簡単な手順で、ドラッグ アンド ドロップによる行の並べ替えが可能なグリッドを設定できました。上記のコードの動作は、以下のデモで確認できます。
行選択も有効になっており、ドラッグされた行をドロップしたときに選択状態が保持されることに注目してください。
制限
現在、IgbHierarchicalGrid.RowDraggable に関する既知の制限事項はありません。
API リファレンス
IgbHierarchicalGrid
その他のリソース
コミュニティは常に新しいアイデアを歓迎しています。