node-tree-data

tree data structure

npm install node-tree-data
7 downloads in the last week
23 downloads in the last month

tree-data

tree data structure for javascript.

install for node

 npm install node-tree-data

install for component

 component install brighthas/tree-data

API

new Tree(id)

var tree = new Tree();  // if no id , auto create a id.
var two_tree = new Tree("id-002");

tree.json / tree.reborn(json) / Tree.json2obj(json)

var treeData = tree.json();

treeData.id
treeData.rootIds // top node's id, Array.
treeData.nodes   // all nodes , look [about node object]

var tree2 = new Tree();
tree2.reborm(treeData);  // clone tree1 data to -> tree2

var tree3 = Tree.json2obj(treeData); // json data to tree obj.

tree.createNode(label,parentId)

var root1 = tree.createNode("Index 1"); // create root node .
var root2 = tree.createNode("Index 2");
var node1 = tree.createNode("Child 1",root2.id); // create child node for root2.
var node2 = tree.createNode("Child 2",root2.id);

tree.getNode(nodeId)

var node = tree.getNode(node2.id);

about node object

var node = tree.getNode(nodeId);
node.parent  // is parent node's id.
node.id
node.list  // is child's ids , Array.
node.label // label name , see tree.createNode(label).

tree.getChildIds(parentId)

var root_ids = tree.getChildIds();  // null argumets , get root node ids.
var childs_ids = tree.getChildIds(root2.id) // get root2's child node ids.

tree.getChilds(parentId)

var roots = tree.getChilds();  // null argumets , get root nodes.
var childs = tree.getChilds(root2.id) // get root2's child nodes.

tree.move(target,nodeId)

move nodeId's node to targetId's node.
tree.move(root1.id,node2.id); // move node2 to root1.

tree.remove(nodeId)

tree.remove(node2.id) // remove node2 from tree.

tree.isSameRoot(nodeId1,nodeId2)

tree.isRoot(nodeId)

tree.getJoinId(nodeId1,nodeId2)

tree.id

tree.up(nodeId)

move up a node.

tree.down(nodeId)

move down a node.

tree.top(nodeId)

top a node.

tree.changeNodeLabel(label,nodeId)

change node's label.

tree.setNodeData(data,nodeId)

var tree = new Tree();
var node = tree.createNode("index page");
console.log(node.data) // {}
node.data = {htmlclass:".root"}  // error! because this node onlyread.
tree.setNodeData({htmlclass:".root"},node.id);  // ok!

Event

'create' event.

var tree = new Tree();
tree.on("create",function(newID){
    console.log("create a new node.");
})
tree.createNode("about page");
// print 'create a new node.'

'change' event.

var tree = new Tree();
var node = tree.createNode("my page");
tree.on('change',function(id){
   console.log("a node changed");
})
tree.setNodeData({author:"leo"},node.id);
// print 'a node changed'

'remove' event.

var tree = new Tree();
var node = tree.createNode("my page");
tree.on('remove',function(id){
   console.log("a node removed");
})
tree.remove(node.id);
// print 'a node removed'
npm loves you