• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

js-tree-list: Convert list to tree, managing a tree and its nodes.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

js-tree-list

开源软件地址:

https://gitee.com/yi-ge/js-tree-list

开源软件介绍:


js-tree-list

npm versionbuild statusCodecovcode coveragenpmlicenseGitHub last commitbitHound

GitHub releaseGithub file sizecodebeat badgeFOSSA Status

JavaScript Style Guide

Convert list to tree, managing a tree and its nodes.

Fork from:https://github.com/DenQ/iron-treehttps://github.com/DenQ/list-to-tree

The author of this project is DenQ. This project has only been improved a little.

Features

  • Convert list to tree.
  • Convert tree to list.
  • Tree sort by last.
  • UUID is support.

Installation

$ npm install js-tree-list

Usage

// JsTreeList.ListToTree Configconst defaultOptions = {  key_id: 'id',  key_parent: 'parent',  key_child: 'child',  key_last: null,  uuid: false,  empty_children: false}
import JsTreeList from "js-tree-list"var list = [  {    id: 1,    parent: 0  },  {    id: 2,    parent: 1  },  {    id: 3,    parent: 1  },  {    id: 4,    parent: 2  },  {    id: 5,    parent: 2  },  {    id: 6,    parent: 0  },  {    id: 7,    parent: 0  },  {    id: 8,    parent: 7  },  {    id: 9,    parent: 8  },  {    id: 10,    parent: 0  }]const tree = new JsTreeList.ListToTree(list, {  key_id: "id",  key_parent: "parent",  key_child: "children",  key_last: "last"}).GetTree()const list = new JsTreeList.TreeToList(tree, {  key_child: "children",  empty_children: true}).GetList()console.log(tree)console.log(list)
Result
[{    "id": 1,    "parent": 0,    "child": [        {            "id": 2,            "parent": 1,            "child": [                {                    "id": 4,                    "parent": 2                }, {                    "id": 5,                    "parent": 2                }            ]        },        {            "id": 3,            "parent": 1        }    ]}, {    "id": 6,    "parent": 0}, {    "id": 7,    "parent": 0,    "child": [        {            "id": 8,            "parent": 7,            "child": [                {                    "id": 9,                    "parent": 8                }            ]        }    ]}, {    "id": 10,    "parent": 0}];

Methods

  • constructor(list, options)
    • params:
      • list - array list with elements. Like { id: 5: parent: 1 }.
      • options - optional parameter. Object for describe flags and field names for tree.
  • .GetTree() This method will be return json tree
    • example:
        tree.GetTree()
  • .sort(callback) The custom sort method
    • callback(a, b) - a and b have Node type and have methods: add, remove, get, set, sort, traversal, etc...
    • example:
      function compareById(vector) {  return (a, b) => {    const aid = Number(a.get("id"))    const bid = Number(b.get("id"))    if (aid > bid) {      return vector ? 1 : -1    } else if (aid < bid) {      return vector ? -1 : 1    } else {      return 0    }  }}ltt.sort(compareById(false))

The Tree and Node Base usage

// create treeimport JsTreeList from "js-tree-list"const object = { id: 1, title: "Root" }const tree = new JsTreeList.Tree(object)// add nodesconst regularObject = { id: 2, title: "Node 2" }tree.add(parentNode => {  return parentNode.get("id") === 1}, regularObject)// contains nodeconst targetNode = tree.contains(currentNode => {  return currentNode.get("id") === 2})// remove nodeconst result = tree.remove(currentNode => {  return currentNode.get("id") === 2})// traversalconst criteria = currentNode => currentNode.get("id") === 1tree.traversal(criteria, currentNode => {  currentNode.set("some", true)})
function compareById(vector) {  return (a, b) => {    const aid = Number(a.get("id"))    const bid = Number(b.get("id"))    if (aid > bid) {      return vector ? 1 : -1    } else if (aid < bid) {      return vector ? -1 : 1    } else {      return 0    }  }}tree.sort(compareById(false)) // desc

The following are the other methods available.


Tree

This is the class of tree management

Properties

  • rootNode Root tree node
    • type Node

Methods

  • contstructor(object)

    • params
      • object - json object. Optional
    • return Three
    • example
    const object = { id: 1, title: "Root" }const tree = new JsTreeList.Tree(object)
  • .add(criteria, object) Adds a node to the tree if the criterion is true

    • params
      • criteria(Node) - function or string. If string then criteria is "root"
      • object - content for the node
    • return Three
    • examples
    const object = { id: 1, title: "Root" }const tree = new JsTreeList.Tree()const resultTree = tree.add("root", object)
    const regularObject = { id: 2, title: "Node 2" }const resultTree = tree.add(parentNode => {  return parentNode.get("id") === 1}, regularObject)
  • .remove(criteria) Removes a node from a tree if the criterion is true

    • params
      • criteria(Node) - return boolean
    • return boolean
    • examples
    const result = tree.remove(currentNode => {  return currentNode.get("id") === 7})
  • .contains(criteria) Searches for a node in a tree according to the criterion

    • params
      • criteria(Node) - return boolean
    • return Node
    • examples
    const targetNode = tree.contains(currentNode => {  return currentNode.get("id") === 7})
  • .sort(compare) Sorts a tree

    • params
      • compare(a:Node, b:Node) - comparison function
    • return null
    • examples
    function compareById(vector) {  return (a, b) => {    const aid = Number(a.get("id"))    const bid = Number(b.get("id"))    if (aid > bid) {      return vector ? 1 : -1    } else if (aid < bid) {      return vector ? -1 : 1    } else {      return 0    }  }}tree.sort(compareById(false)) //Desc
  • .move(criteria, destination) Moves the desired branch or node to the node or branch of the destination, according to the criteria

    • params
      • criteria(Node) - callback
      • destination(Node) - callback
    • return boolean
    • examples
    const search = currentNode => currentNode.get("id") === 7const destination = currentNode => currentNode.get("id") === 3const result = tree.move(search, destination)
  • .traversal(criteria, callback) Bypasses the tree and, according to the criterion, calls a function for each node

    • params
      • criteria(Node) - return boolean
      • callback(Node)
    • return null
    • examples
    const criteria = currentNode => currentNode.get("id") === 7tree.traversal(criteria, currentNode => {  currentNode.set("some", true)})
    tree.traversal(null, currentNode => {  if (currentNode.get("id") % 2 === 0) {    currentNode.set("some", true)  }})
  • .toJson(options) Represents a tree in the form of a json format

    • params
      • options - object. Optional
        • empty_children - Type boolean. Allow empty children. Default true
        • key_children - Type string. Field name for children. Default children
    • return object
    • examples
    const json = tree.toJson()

Node

This is the node management class

Properties

  • content Content of the node
    • type object
  • children Children of the node
    • type array
  • length Number children of the node
    • type number

Methods

  • constructor(json)

    • params
      • json - simple json object
    • examples
    import JsTreeList from "js-tree-list"const rootContent = {  id: 1,  name: "Root"}let node = new JsTreeList.Node(rootContent)
  • .add(child) Adding a child to the node

    • return Node - created node
    • params
      • child - type object/json
    • examples
    const rootContent = {  id: 1,  name: "Root"}let node = new JsTreeList.Node(rootContent)const childNode = node.add({ id: 2, name: "Two node" })
  • .remove(criteria) Removing a child node according to the criterion

    • return - removed Node
    • params
      • criteria - criteria function for removing nodes
    • examples
    const removedNodes = node.remove(itemNode => {  return itemNode.get("id") === 3})
  • .get(path) Access to node content by field name

    • return mixed
    • params
      • path - key name for object in node. For example id or fullname, etc...
    • examples
    node.get("id") // 1node.get("name") // "Some name"
  • .set(path, value) Setting a value or creating a new field in the contents of a node

    • return boolean
    • params
      • path - String field name
      • value - mixed
    • examples
    node.set('id', 100)); // returned `true`. Node.content.id = 100node.get('id'); // 100
  • .sort(compare) Sorting child nodes

    • return null
    • params
      • compare - custom function for sorting
    • examples
    function compareById(vector) {  return (a, b) => {    const aid = Number(a.get("id"))    const bid = Number(b.get("id"))    if (aid > bid) {      return vector ? 1 : -1    } else if (aid < bid) {      return vector ? -1 : 1    } else {      return 0    }  }}node.sort(compareById(false))
  • .traversal(criteria, callback) Bypassing child nodes according to the criterion and applying function to them

    • return null
    • params
      • criteria - function criteria each nodes
      • callback - function fire when criteria is true for node
    • examples
    // for all nodesnode.traversal(null, currentNode => {  const name = currentNode.get("name")  currentNode.set("name", `${name}!`) // Last symbol "!"})
    // only for node.id == 3node.traversal(  currentNode => currentNode.get("id") === 3,  currentNode => {    const name = currentNode.get("name")    currentNode.set("name", `${name}!`) // Last symbol "!"  })


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap