Git Product home page Git Product logo

js-treeview's Introduction

Vanilla JavaScript TreeView

Build Statusnpm version

A stupid, simple tree view written with vanilla JS. I needed a lightweight control that just displayed data in a tree form and out popped this. I wrote a post on my blog that goes into more depth.

Dependencies

None. I built this using only plain JavaScript so there's no external dependencies. Other than the CSS required for styling.

Example Usage

If used outside of NPM/require, it will attach a global TreeVew object to window. If done using NPM, then it can be included using require like everything else. To install it via NPM:

npm install js-treeview

HTML

<div id="tree"></div>

JavaScript

// NPM
var TreeView = require('js-treeview');

var tree = new TreeView([
    { name: 'Item 1', children: [] },
    { name: 'Item 2', expanded: true, children: [
            { name: 'Sub Item 1', children: [] },
            { name: 'Sub Item 2', children: [] }
        ]
    }
], 'tree');

Options

Name Type Description
data array The array of items to populate the tree with. Each item is required to have a name and a children array. An optional expanded option allows you to default the child to be expanded when created.
id `string object`

Events

Name Arguments Description
expand target - The DOM node that initiated the expand
leaves - Array of leaf DOM nodes under the target
Fires when a leaf is expanded.
expandAll No arguments Fires after the expandAll method is called.
collapse target - The DOM node that initiated the collapse
leaves - Array of leaf DOM nodes under the target
Fires when a leaf is collapsed.
collapseAll No arguments Fires after the collapseAll method is called.
select target - The DOM node selected
data - Data node associated with the selected element
Fires when a outermost leaf is selected. Contains data item of the leaf selected.

Usage

tree.on('select', function (e) {
    console.log(JSON.stringify(e));
});

CodePen Example

License

This plugin is available under the MIT license.

js-treeview's People

Contributors

chiragchoudhary66 avatar justinchmura avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.