This repository was archived by the owner on Jul 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdrag-and-drop.tsx
More file actions
139 lines (137 loc) · 6.89 KB
/
drag-and-drop.tsx
File metadata and controls
139 lines (137 loc) · 6.89 KB
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
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { SampleBase } from '../common/sample-base';
import {TreeViewComponent} from '@syncfusion/ej2-react-navigations';
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
import { closest } from '@syncfusion/ej2-base';
import './drag-and-drop.css';
export class Dragdrop extends SampleBase<{}, {}> {
private listObj: ListViewComponent;
public id:number=1;
// Hierarchical data source for first TreeView component
public productTeam1: { [key: string]: Object }[] = [
{
id: 't1', name: 'ASP.NET MVC Team', expanded: true,
child: [
{ id: 't2' , pid: 't1', name: 'Smith'},
{ id: 't3', pid: 't1', name: 'Johnson' },
{ id: 't4', pid: 't1', name: 'Anderson' },
]
},
{
id: 't5', name: 'Windows Team', expanded: true,
child: [
{ id: 't6', pid: 't5', name: 'Clark' },
{ id: 't7', pid: 't5', name: 'Wright' },
{ id: 't8', pid: 't5', name: 'Lopez' },
]
},
];
// Render the first TreeView by mapping its fields property with data source properties
private field: Object = { dataSource: this.productTeam1, id: 'id', text: 'name', child: 'child' };
private allowDragAndDrop:boolean = true;
// Hierarchical data source for second TreeView component
public productTeam2: { [key: string]: Object }[] = [
{
id: 't9' , name: 'Web Team',expanded: true,
child: [
{ id: 't11' , pid: 't9' , name: 'Joshua' },
{ id: 't12', pid: 't9' , name: 'Matthew' },
{ id: 't13', pid: 't9' , name: 'David' },
]
},
{
id: 't14', name: 'Build Team', expanded: true,
child: [
{ id: 't15' , pid: 't14' , name: 'Ryan' },
{ id: 't16' , pid: 't14' , name: 'Justin' },
{ id: 't17' , pid: 't14', name: 'Robert' },
]
},
];
// Render the second TreeView by mapping its fields property with data source properties
private fields: Object = { dataSource: this.productTeam2, id: 'id', text: 'name', child: 'child', selected: 'isSelected' };
private allowDragAndDrops:boolean = true;
public onDragStop(args: any): void {
let targetEle: any = closest(args.target, '.e-droppable');
targetEle = targetEle ? targetEle : args.target;
// Check the target as ListView or not
if (targetEle && targetEle.classList.contains('custom-list')) {
args.cancel = true;
let newData: { [key: string]: Object }[] = [];
if (args.draggedNode.classList.contains('e-active')) {
var dragNode: any = closest(args.draggedNode, '.e-treeview');
var selNodes = dragNode.ej2_instances[0].selectedNodes;
for (let i: number = 0, len: number = selNodes.length; i < len; i++) {
let nodeEle: Element = document.querySelector('[data-uid="' + selNodes[i] + '"]').querySelector('.e-list-text');
let nodeText: string = nodeEle.textContent;
let newNode: { [key: string]: Object } = { id: 'l' +this.id, text: nodeText, class: 'custom-delete', iconId: 'i' + this.id };
this.id++;
newData.push(newNode);
}
} else {
let text: string = 'text';
let nodeText: string = args.draggedNodeData[text] as string;
let newNode: { [key: string]: Object } = { id: 'l' + this.id, text: nodeText, class: 'custom-delete', iconId: 'i' + this.id };
this.id++;
newData.push(newNode);
}
// Add collection of node to ListView
this.listObj.addItem(newData, undefined);
}
}
// Add the custom action for delete icon in ListView
public onCreate(){
document.getElementById('list').addEventListener('mousedown', (event: any) => {
if (event.target.classList.contains('custom-delete')) {
let node: Element = closest(event.target, 'li');
this.listObj.removeItem(node);
}
});
document.getElementById('overlay').addEventListener('mousedown', (event: any) => {
document.getElementById('overlay').style.display = 'none';
});
}
public actionBegin(): void {
var listObj=this;
}
render() {
return (
<div className = "control-pane">
<div className="col-lg-12 control-section custom-tree">
<div className= "control-wrapper">
<div className="col-lg-4 tree1-data">
<h4>TreeView-1</h4>
<div className="content">
<TreeViewComponent id='tree1' fields={this.field} nodeDragStop={this.onDragStop.bind(this)} created={this.onCreate.bind(this)} allowDragAndDrop={this.allowDragAndDrop} />
</div>
</div>
<div className="col-lg-4 tree2-data">
<h4>TreeView-2</h4>
<div className="content">
<TreeViewComponent id='tree2' fields={this.fields} nodeDragStop={this.onDragStop.bind(this)} allowDragAndDrop={this.allowDragAndDrops} />
</div>
</div>
<div className="col-lg-4 tree3-data">
<h4>ListView</h4>
<div className="content">
<div id="list">
<ListViewComponent id="list" className="e-droppable" dataSource={ [] }ref={(list) => { this.listObj = list }} actionComplete={this.actionBegin.bind(this)} cssClass={'custom-list'} template="<div><span>${text}</span><span id=${iconId} class=${class}></span></div>" />
</div>
</div>
</div>
<div id="overlay">
</div>
</div>
</div>
<div id="action-description">
<p>This sample demonstrates the drag and drop functionality of TreeView. A drag and drop image is present at the top of the sample which hides on clicking the sample. To drag and drop node, select and drag the desired node and drop it on the target node or external container.</p>
</div>
<div id="description">
<p>The <code>TreeView</code> component allows users to drag any node and drop it on any other node in the same or different tree using <code>allowDragAndDrop</code> property. Additionally, it supports dropping a tree node to an external container using <code>nodeDragStop</code> event of the TreeView</p>
<p>For more information, you can refer to the <a href="http://ej2.syncfusion.com/react/documentation/treeview/drag-and-drop.html" target="_blank">Drag and Drop</a> section from the documentation.</p>
</div>
</div>
)
}
}