-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
152 lines (138 loc) · 4.06 KB
/
App.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useState } from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';
import './App.css'
import {
TreeList,
ColumnChooser,
ColumnFixing,
Column,
RequiredRule,
FilterRow,
SearchPanel,
Selection,
Editing,
RowDragging,
Paging
} from 'devextreme-react/tree-list';
import { employees } from './employees';
function SelectedEmployee(props) {
if(props.employee) {
return (
<p id="selected-employee">
Selected employee: {props.employee.FullName}
</p>
);
}
return null;
}
function App() {
const [selectedEmployee, setSelectedEmployee] = useState();
const [currentEmployees, setCurrentEmployees] = useState(employees);
const selectEmployee = (e) => {
e.component.byKey(e.currentSelectedRowKeys[0]).done(employee => {
setSelectedEmployee(employee);
});
}
const 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;
}
}
const onReorder = (e) => {
let visibleRows = e.component.getVisibleRows(),
sourceData = e.itemData,
targetData = visibleRows[e.toIndex].data,
employeesReordered = currentEmployees,
sourceIndex = employeesReordered.indexOf(sourceData),
targetIndex = employeesReordered.indexOf(targetData);
if (e.dropInsideItem) {
sourceData = { ...sourceData, HeadID: targetData.ID };
employeesReordered = [...employeesReordered.slice(0, sourceIndex), sourceData, ...employeesReordered.slice(sourceIndex + 1)];
} else {
if (sourceData.HeadID !== targetData.HeadID) {
sourceData = { ...sourceData, HeadID: targetData.HeadID };
if (e.toIndex > e.fromIndex) {
targetIndex++;
}
}
employeesReordered = [...employeesReordered.slice(0, sourceIndex), ...employeesReordered.slice(sourceIndex + 1)];
employeesReordered = [...employeesReordered.slice(0, targetIndex), sourceData, ...employeesReordered.slice(targetIndex)];
}
setCurrentEmployees(employeesReordered);
}
return (
<div className="App">
<TreeList
id="treeList"
dataSource={currentEmployees}
rootValue={-1}
keyExpr="ID"
parentIdExpr="HeadID"
autoExpandAll={true}
allowColumnReordering={true}
allowColumnResizing={true}
columnAutoWidth={true}
onSelectionChanged={selectEmployee}>
<Column dataField="FullName">
<RequiredRule />
</Column>
<Column
dataField="Position">
<RequiredRule />
</Column>
<Column
dataField="BirthDate"
dataType="date"
width={100}>
<RequiredRule />
</Column>
<Column
dataField="HireDate"
dataType="date"
width={100}>
<RequiredRule />
</Column>
<Column dataField="City" />
<Column
dataField="State">
<RequiredRule />
</Column>
<Column dataField="Email" visible={false} />
<Column dataField="MobilePhone" />
<Column dataField="Skype" />
<ColumnFixing enabled={true} />
<ColumnChooser enabled={true} />
<FilterRow visible={true} />
<SearchPanel visible={true} />
<Editing
mode="popup"
allowUpdating={true}
allowDeleting={true}
allowAdding={true}
/>
<Selection mode="single" />
<RowDragging
onDragChange={onDragChange}
onReorder={onReorder}
allowDropInsideItem={true}
allowReordering={true}
showDragIcons={true}
/>
<Paging
enabled={true}
defaultPageSize={10}
/>
</TreeList>
<SelectedEmployee employee={selectedEmployee} />
</div>
);
}
export default App;