Convert list to tree, managing a tree and its nodes.
Fork from: https://github.com/DenQ/iron-tree https://github.com/DenQ/list-to-tree
The author of this project is DenQ. This project has only been improved a little.
$ npm install js-tree-list
// JsTreeList.ListToTree Config
const 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)
[{
"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
}];
list
- array list with elements. Like { id: 5: parent: 1 }
.options
- optional parameter. Object for describe flags and field names for tree. tree.GetTree()
Node
type and have methods: add, remove, get, set, sort, traversal, etc...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))
// create tree
import JsTreeList from "js-tree-list"
const object = { id: 1, title: "Root" }
const tree = new JsTreeList.Tree(object)
// add nodes
const regularObject = { id: 2, title: "Node 2" }
tree.add(parentNode => {
return parentNode.get("id") === 1
}, regularObject)
// contains node
const targetNode = tree.contains(currentNode => {
return currentNode.get("id") === 2
})
// remove node
const result = tree.remove(currentNode => {
return currentNode.get("id") === 2
})
// traversal
const criteria = currentNode => currentNode.get("id") === 1
tree.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.
This is the class of tree management
Node
contstructor(object)
object
. OptionalThree
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
function
or string
. If string
then criteria is "root"Three
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
boolean
boolean
const result = tree.remove(currentNode => {
return currentNode.get("id") === 7
})
.contains(criteria) Searches for a node in a tree according to the criterion
boolean
Node
const targetNode = tree.contains(currentNode => {
return currentNode.get("id") === 7
})
.sort(compare) Sorts a tree
null
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
boolean
const search = currentNode => currentNode.get("id") === 7
const destination = currentNode => currentNode.get("id") === 3
const result = tree.move(search, destination)
.traversal(criteria, callback) Bypasses the tree and, according to the criterion, calls a function for each node
boolean
null
const criteria = currentNode => currentNode.get("id") === 7
tree.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
object
. Optional
boolean
. Allow empty children. Default true
string
. Field name for children. Default children
object
const json = tree.toJson()
This is the node management class
object
array
number
constructor(json)
json
objectimport 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
Node
- created nodeobject
/jsonconst 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
Node
const removedNodes = node.remove(itemNode => {
return itemNode.get("id") === 3
})
.get(path) Access to node content by field name
mixed
id
or fullname
, etc...node.get("id") // 1
node.get("name") // "Some name"
.set(path, value) Setting a value or creating a new field in the contents of a node
boolean
String
field namemixed
node.set('id', 100)); // returned `true`. Node.content.id = 100
node.get('id'); // 100
.sort(compare) Sorting child nodes
null
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
null
function
criteria each nodesfunction
fire when criteria is true for node// for all nodes
node.traversal(null, currentNode => {
const name = currentNode.get("name")
currentNode.set("name", `${name}!`) // Last symbol "!"
})
// only for node.id == 3
node.traversal(
currentNode => currentNode.get("id") === 3,
currentNode => {
const name = currentNode.get("name")
currentNode.set("name", `${name}!`) // Last symbol "!"
}
)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型