0.1.1 • Published 10 years ago

simple-tree v0.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
10 years ago

SimpleTree

An easy way to create a tree structure in JavaScript

structure

Usage

First, you need to create the tree structure.

var tree = {
  root: { value: 'root',
    sons:[
      { value: 'A',
        sons: [
          { value: 'AA',
            sons: [
              { value: 'AA1', sons: [] }
            ]
          },
          { value: 'AAA', sons:[] }
        ]
      },

      { value: 'B',
        sons:[
          { value: 'BB', sons: [] }
        ]
      }
    ]
  }
}

Then just create your tree.

  Tree.create(tree, function(branch, sons){
    //manipule branch and sons
  });

The create method receives two parameters: The tree structure and a callback.

Callback

The function passed as the callback takes two parameters: the parent and son. It is used to interact as each element of the tree.

  Tree.create(tree, function(obj, son){
    console.log(obj.value);
    console.log("\t => ", son.value);
  });

License

simple-tree is available under the MIT license.