Interactive Tree View In Vanilla JavaScript – vanillatree

Category: Javascript | May 30, 2018
Author: finom
Views Total: 4,080
Official Page: Go to website
Last Update: May 30, 2018
License: MIT

Preview:

Interactive Tree View In Vanilla JavaScript – vanillatree

Description:

vanillatree is a simple, collapsible interactive tree view component with context menu support, implemented in vanilla JavaScript.

How to use it:

Include both the vanillatree’s JavaScript and CSS files on the html page.

<link href="vanillatree.css" rel="stylesheet">
<script src="vanillatree.js"></script>

Create a container where you want to present the tree view.

<main></main>

Initialize the tree view and set the right click context menu as this:

var main = document.querySelector( 'main' ),
    tree = new VanillaTree( main, {
      contextmenu: [{
          label: 'Menu 1',
          action: function(id) {
            alert('Menu 1 ' + id);
          }
        }, {
          label: 'Menu 2',
          action: function(id) {
            alert('Menu 2 ' + id);
          }
        }]
    });

Add custom nodes to the tree view.

tree.add({
  label: 'Label A',
  id: 'a',
  opened: true
});

tree.add({
  label: 'Label B',
  id: 'b'
});

tree.add({
  label: 'Label A.A',
  parent: 'a',
  id: 'a.a',
  opened: true,
  selected: true
});

tree.add({
  label: 'Label A.A.A',
  parent: 'a.a'
});
tree.add({
  label: 'Label A.A.B',
  parent: 'a.a'
});

tree.add({
  label: 'Label B.A',
  parent: 'b'
});

API methods.

// add new nodes
tree.add(options);

// move a node to parent
tree.move(id, parentId);

// remove a node
tree.remove(id);

// open a node
tree.open(id);

// close a node
tree.close(id);

// toggle a node
tree.toggle(id);

// select a node
tree.select(id);

Events.

main.addEventListener('vtree-add', function(evt) {
  // when added
});

main.addEventListener('vtree-remove', function(evt) {
  // when closed
});

main.addEventListener('vtree-move', function(evt) {
  // when moved
});

main.addEventListener('vtree-open', function(evt) {
  // when opened
});

main.addEventListener('vtree-close', function(evt) {
  // when closed
});

main.addEventListener('vtree-select', function(evt) {
  // when selected
});

Changelog:

05/30/2018

  • Various fixes

You Might Be Interested In:


Leave a Reply