In Ignite UI for Angular Hierarchical Grid, RowDrag is initialized on the root igx-hierarchical-grid component and is configurable via the rowDraggable input. Enabling row dragging provides users with a row drag-handle with which they can initiate dragging of a row.
最速で機能豊富な Angular Data Grid は、ページング、ソート、フィルタリング、グループ化、PDF および Excel へのエクスポートなどの機能を提供します。究極のアプリ構築エクスペリエンスとデータ操作に必要なすべてが揃っています。
Like this sample? Get access to our complete Ignite UI for Angular toolkit and start building your own apps in minutes. Download it for free.
Configuration
In order to enable row-dragging for your igx-hierarchical-grid, all you need to do is set the grid's rowDraggable to true. Once this is enabled, a row-drag handle will be displayed on each row. This handle can be used to initiate row dragging.
Clicking on the drag-handle and moving the cursor while holding down the button will cause the grid's rowDragStart event to fire. Releasing the click at any time will cause rowDragEnd event to fire.
Below, you can find a walkthrough on how to configure an igx-hierarchical-grid to support row dragging and how to properly handle the drop event.
In this example, we'll handle dragging a row from a grid to a designated area and, when dropping it, removing it from the grid.
Drop Areas
Enabling row-dragging was pretty easy, but now we have to configure how we'll handle row-dropping.
We can define where we want our rows to be dropped using the igxDrop directive.
First we need to import the IgxDragDropModule in our app module:
Then, in our template, we define a drop-area using the directive's selector:
<divclass="drop-area"igxDrop (enter)="onEnterAllowed($event)" (leave)="onLeaveAllowed($event)"
(dropped)="onDropAllowed($event)"><igx-icon>delete</igx-icon><div>Drag a row here to delete it</div></div>html
You may enable animation when a row is dropped on a non-droppable area using the animation parameter of the rowDragEnd event. If set to true, the dragged row will animate back to its' original position when dropped over a non-droppable area.
Once we've defined our drop-area in the template, we have to declare our handlers for the igxDrop's enter, leave and dropped events in our component's .ts file.
First, let's take a look at our enter and leave handlers. In those methods, we just want to change the icon of the drag's ghost so we can indicate to the user that they are above an area that allows them to drop the row:
The changeGhostIconprivate method just changes the icon inside of the drag ghost. The logic in the method finds the element that contains the icon (using the igx-grid__drag-indicator class that is applied to the drag-indicator container), changing the element's inner text to the passed one.
The icons themselves are from the material font set and are defined in a separate enum:
Once the row is dropped, we just call the row's delete() method
When using row data from the event arguments (args.dragData.data) or any other row property, note that the entire row is passed in the arguments as a reference, which means that you must clone the data you need, if you want to distinguish it from the one in the source grid.
Templating the drag ghost
The drag ghost can be templated using the IgxRowDragGhost directive, applied to a <ng-template> inside of the igx-hierarchical-grid's body:
The result of the configuration can be seem below in a igx-hierarchical-grid with row dragging and multiple selection enabled. The demo shows the count of the currently dragged rows:
Example Demo
The drag ghost can be templated on every grid level, making it possible to have multiple ghost templates or to only provide a template for a single row island.
The drag handle icon can be templated using the grid's dragIndicatorIconTemplate. In the example we're building, let's change the icon from the default one (drag_indicator) to drag_handle.
To do so, we can use the igxDragIndicatorIcon to pass a template inside of the igx-hierarchical-grid's body:
Make sure that there is a primaryKey specified for the grid! The logic needs an unique identifier for the rows so they can be properly reordered
Once rowDraggable is enabled and a drop zone has been defined, you need to implement a simple handler for the drop event. When a row is dragged, check the following:
Is the row expanded? If so, collapse it.
Was the row dropped inside of the grid?
If so, on which other row was the dragged row dropped?
Once you've found the target row, swap the records' places in the data array
Was the row initially selected? If so, mark it as selected.
Below, you can see this implemented in the component's .ts file:
With these few easy steps, you've configured a grid that allows reordering rows via drag/drop! You can see the above code in action in the following demo.
Notice that we also have row selection enabled and we preserve the selection when dropping the dragged row.
EXAMPLE
TS
HTML
SCSS
import { Component, ViewChild } from'@angular/core';
import { IDropDroppedEventArgs, IgxHierarchicalGridComponent, RowType, Point, GridSelectionMode, IgxDropDirective, IgxColumnComponent, IgxRowIslandComponent } from'igniteui-angular';
import { createData, IDrive } from'../../data/files.data';
import { IgxPreventDocumentScrollDirective } from'../../directives/prevent-scroll.directive';
@Component({
selector: 'app-hierarchical-grid-row-reorder',
styleUrls: ['./hierarchical-grid-row-reorder.component.scss'],
templateUrl: 'hierarchical-grid-row-reorder.component.html',
imports: [IgxHierarchicalGridComponent, IgxDropDirective, IgxPreventDocumentScrollDirective, IgxColumnComponent, IgxRowIslandComponent]
})
exportclassHGridRowReorderComponent{
@ViewChild(IgxHierarchicalGridComponent, { read: IgxHierarchicalGridComponent, static: true })
public hGrid: IgxHierarchicalGridComponent;
public localData: IDrive[] = [];
public selectionMode: GridSelectionMode = 'multiple';
constructor() {
this.localData = createData(3, 12, 8);
}
public rowDragStart(args: any): void {
const targetRow: RowType = args.dragData;
// if the row-to-be-dragged is expanded - collapse itif (targetRow.expanded) {
targetRow.expanded = false;
}
}
public rowDrop(args: IDropDroppedEventArgs): void {
const targetRow = args.dragData;
const event = args.originalEvent;
const cursorPosition: Point = { x: event.clientX, y: event.clientY };
this.moveRow(targetRow, cursorPosition);
}
private moveRow(draggedRow: RowType, cursorPosition: Point): void {
// const parent: IgxHierarchicalGridComponent = (draggedRow as any).grid;// const parent = args.drag.ghostContext.grid;const parent = this.hGrid;
const rowIndex: number = this.getTargetRowIndex(parent.rowList.toArray(), cursorPosition);
if (rowIndex === -1) { return; }
// delete the dragged row and then insert it at its new positionconst wasSelected = draggedRow.selected;
draggedRow.delete();
parent.data.splice(rowIndex, 0, draggedRow.data);
if (wasSelected) {
// find the row that has the same ID as the dragged row and select it
parent.selectRows([parent.rowList.toArray()
.find((r) => r.key === draggedRow.key).key], false);
}
}
private getTargetRowIndex(rowListArr: any[], cursorPosition: Point): number {
const targetElem = this.catchCursorPosOnElem(rowListArr, cursorPosition);
// get the index of the row that has the same ID as the dragged rowreturn rowListArr.indexOf(rowListArr.find((r) => r.key === targetElem.key));
}
private catchCursorPosOnElem(rowListArr: any[], cursorPosition: Point): any {
// get the row which the dragged row was dropped onfor (const row of rowListArr) {
const rowRect = row.nativeElement.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 row;
} elseif (row === rowListArr[rowListArr.length - 1] && cursorPosition.y > rowRect.bottom) {
return row;
}
}
}
}
ts