-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.component.ts
66 lines (56 loc) · 1.75 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Component } from '@angular/core';
import { Employee, EmployeesService } from './employees.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Getting Started with TreeList';
employees: Employee[] = [];
selectedEmployee: Employee;
constructor(service: EmployeesService) {
this.employees = service.getEmployees();
this.selectEmployee = this.selectEmployee.bind(this);
this.onReorder = this.onReorder.bind(this);
}
selectEmployee(e) {
e.component.byKey(e.currentSelectedRowKeys[0]).done(employee => {
if(employee) {
this.selectedEmployee = employee;
}
});
}
onDragChange(e) {
let visibleRows = e.component.getVisibleRows(),
sourceNode = e.component.getNodeByKey(e.itemData.ID),
targetNode = visibleRows[e.toIndex].node;
while(targetNode && targetNode.data) {
if (targetNode.data.ID === sourceNode.data.ID) {
e.cancel = true;
break;
}
targetNode = targetNode.parent;
}
}
onReorder(e) {
let visibleRows = e.component.getVisibleRows(),
sourceData = e.itemData,
targetData = visibleRows[e.toIndex].data;
if (e.dropInsideItem) {
e.itemData.HeadID = targetData.ID;
e.component.refresh();
} else {
let sourceIndex = this.employees.indexOf(sourceData),
targetIndex = this.employees.indexOf(targetData);
if (sourceData.HeadID !== targetData.HeadID) {
sourceData.HeadID = targetData.HeadID;
if (e.toIndex > e.fromIndex) {
targetIndex++;
}
}
this.employees.splice(sourceIndex, 1);
this.employees.splice(targetIndex, 0, sourceData);
}
}
}