1.8.3 • Published 4 years ago

@widgetjs/tree v1.8.3

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

@widgetjs/tree

NPM version David deps npm downloads gzip size

A lightweight flexible tree widget.

Features

  • 🚀 Compatible with VanillaJS / React / Vue
  • ✂️ Tiny size after gzip
  • 🎊 Zero dependence
  • 🎉 Events supported

Demo

Online Demo

demo.gif

Install

npm i -S @widgetjs/tree

Usage

React/Vue usage

import Tree from '@widgetjs/tree';

VanillaJS usage

<script src="path/to/tree.min.js"></script>

Initialize

new Tree(treeContainer, parameters), returns initialized Tree instance.

  • treeContainer - string - css selector of the tree container(document.querySelector inside).
  • parameters - object - options of the tree.

Basic Node Format

{
  "id": "unique_ID",
  "text": "node-0",
  "attributes": {},
  "children": [],
  "check": true
}
NameTypeDescriptionRequired
idanyunique idRequired
textstringtree node labelRequired
attributesobjectcustom attributes of the nodeOptional
childrenarraychildren of current nodeOptional
checkbooleanwhether the node is selected or notOptional

Example

const myTree = new Tree('#container', {
  url: '/api/treeJson',
});

Parameters

NameTypeDescription
urlstringa URL to retrieve remote data,or use data
methodstringhttp method(GET/POST), default 'GET'
dataarraythe json tree data
valuesarrayids which you want to check
closeDepthintegerexpand level
beforeLoadfunctioninvoke before the tree load data. Format raw data in this function.
loadedfunctioninvoke after the tree load data
onChangefunctioninvoke when the node status change

Example

const treeData = [
  {
    id: '0',
    text: 'node-0',
    children: [
      {
        id: '0-0',
        text: 'node-0-0',
        children: [
          {id: '0-0-0', text: 'node-0-0-0'},
          {id: '0-0-1', text: 'node-0-0-1'},
          {id: '0-0-2', text: 'node-0-0-2'},
        ],
      },
      {id: '0-1', text: 'node-0-1'},
    ],
  },
  {
    id: '1',
    text: 'node-1',
    children: [{id: '1-0', text: 'node-1-0'}, {id: '1-1', text: 'node-1-1'}],
  },
];

const myTree = new Tree('#container', {
  data: treeData,
});
const myTree = new Tree('#container', {
  url: '/api/treeJson',
  method: 'GET',

  values: ['1', '2', '3'],

  // only expand level 1 node
  closeDepth: 1,

  beforeLoad: function(rawData) {
    function formatData() {
      // do some format
    }
    return formatData(rawData);
  },

  loaded: function() {
    // do something or set values after Tree loaded callback
    // do not use arrow function `()=>` , if you use `this`, use function instead.
    // this context bind current tree instance
    this.values = ['0-1'];
  },

  onChange: function() {
    console.log(this.values);
  },
});

Properties

PropertyTypeOperationDescription
valuesarrayget/setselected values.
selectedNodesarraygetselected nodes data with attributes.
disablesarrayget/setget disabled values, or set disable nodes.
disabledNodesarraygetdisabled nodes data with attributes.

myTree.values

// get
const values = myTree.values;

// set
tree.values = ['0-1'];

myTree.selectedNodes

// get
const selectedNodes = myTree.selectedNodes;

myTree.disables

// get
const disables = myTree.disables;

// set
tree.disables = ['0-1'];

myTree.disabledNodes

// get
const disabledNodes = myTree.disabledNodes;

Events

EventParametersDescription
beforeLoadcurrent datainvoke before the tree load data
loadednullinvoke after the tree load data
onChangenullinvoke when the node status change

License

MIT


Like @widgetjs/tree? just 🌟 star the project! Create issues if you find bug.

1.8.3

4 years ago

1.8.2

4 years ago

1.8.0

5 years ago

1.7.6

6 years ago

1.7.4

6 years ago

1.7.3

6 years ago

1.7.2

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.4

6 years ago

1.6.3

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.1

6 years ago

1.3.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago