If you are on Angular, which I assume because of using Nebular, check out this page about using Angular cdk drag and drop directives:
https://material.angular.io/cdk/drag-drop/overview
It plays well and fairly easy with Nebular Tree Grid, here is how code to add drag and drop to a nbTreeGrid
looks like:
<table [nbTreeGrid]="_tableData" cdkDropList
(cdkDropListDisabled)="!draggable" cdkDropListLockAxis="y"
(cdkDropListSorted)="onDrag($event)
(cdkDropListDropped)="onDrop($event)">
<tr cdkDrag nbTreeGridRow
*nbTreeGridRowDef="let row; let i = index; columns: _tableDataColumns"
[cdkDragData]="{index: i, data: row.data}">
</tr>
edit: Note the [cdkDragData]
on the tr
. I use this in my controller to know which element is being dropped in place of which other. Since all we get from drop events are the current and previous index of the dragged item, we need to keep track of the current index of each row in the table. I couldn't find another way to get this info from NbTreeGridDataSource
. Here is some controller code to give you an idea
import { CdkDrag } from '@angular/cdk/drag-drop'
@Component()
export class DropListComponent {
// we'll need to access all drag items to get their dragData
@ViewChildren(CdkDrag) private rows: QueryList<CdkDrag>;
// this is called during drag every time list elements are rearranged
onDrag(event) {
const {index: sourceId, data: sourceData} = event.item.data
const target = this.rows.find(el=> el.data.index == event.currentIndex).data
const {index: targetId, data: targetData} = target
console.log(`Dropped items ${sourceId} to ${targetId}.`)
console.log('SOURCE', sourceData)
console.log('TARGET', targetData)
}
}