JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="http://www.jeasyui.com/easyui/themes/default/easyui.css">
<script src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
<div id=1></div>
<ul id="tt1"></ul>
<ul id="tt2"></ul>
JavaScript
var rows = [{"DepId":1,"DepParentId":"","Title":"Departaments"},
{"DepId":2,"DepParentId":1,"Title":"OTT"},
{"DepId":3,"DepParentId":2,"Title":"OTT SUB 1"},
{"DepId":4,"DepParentId":3,"Title":"OTT SUB 2"},
{"DepId":5,"DepParentId":3,"Title":"OTT SUB 3"},
{"DepId":6,"DepParentId":3,"Title":"OTT SUB 4"}];
document.getElementById("1").innerHTML = JSON.stringify(convert(rows));
$('#tt1').tree({
data: convert(rows)
});
$('#tt2').tree({
data: rows,
loadFilter:function(data,parent){
return convert(data);
}
});
function convert(rows) {
function exists(rows, parentId) {
for (var i = 0; i < rows.length; i++) {
if (rows[i].DepId === parentId) return true;
}
return false;
}
var nodes = [];
// get the top level nodes
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
if (!exists(rows, row.DepParentId)) {
nodes.push({
id: row.DepId,
text: row.Title
});
}
}
var toDo = [];
for (var i = 0; i < nodes.length; i++) {
toDo.push(nodes[i]);
}
while (toDo.length) {
var node = toDo.shift(); // the parent node
// get the children nodes
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
if (row.DepParentId === node.id) {
var child = { id: row.DepId, text: row.Title };
if (node.children) {
node.children.push(child);
} else {
node.children = [child];
}
toDo.push(child);
}
}
}
return nodes;
}