npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

element-tree-js

v3.7.11

Published

This is for processing 'data' prop of Element Tree component

Downloads

15

Readme

element-tree-js v3.7.11

The element-tree-js library support esm and cjs module

Installation

npm install element-tree-js
//or
yarn add element-tree-js

Usage

let tree = [
  {
    id: 32,
    name: 'level-1',
    key: '2',
    children: [
      {
        id: 79,
        name: 'level-1-1',
        key: '1',
      },
      {
        id: 77,
        name: 'level-1-2',
        key: '2',
        children: [
          {
            id: 179,
            name: 'level-1-2-1',
            key: '2',
          },
        ],
      },
    ],
  },
];

let tree1 = [
  {
    id: 32,
    name: 'level-1',
    key: '2',
    children: [
      {
        id: 79,
        name: 'level-1-1',
        key: '1',
      },
      {
        id: 77,
        name: 'level-1-2',
        key: '2',
        children: [
          {
            id: 32,
            name: 'level-1-2-1',
            key: '2',
          },
        ],
      },
    ],
  },
];
import treeJs from 'element-tree-js';

//设置默认的 子节点 字段名,因为有的人在element中定义成了chidren,有的人定义成了son
//Set the default child node field name,like 'chidren' or 'son'.
treeJs.setDefaultChildName('child');
//let tree = [
//    {
//      id: 32,
//      name: "level-1",
//      key: "2",
//      son: []
//    }
//];

//In your function or method

// 获取 树 的 最大深度.
// Gets the maximum depth of the tree,
treeJs.getTreeMaxDeep(tree); // 3

// 获取 树中节点 的 深度.
// Gets the depth of the node in the tree,
treeJs.getNodeLevel(tree, 'id', 3); // 3

// 查找 树 中id等于179的节点。
// Find the node in the tree when id is '179'. return the node.
treejs.getTreeNodeByField(tree, 'id', 179);
//{id: 179, name: "level-1-2-1", key: "2"}

//将树中id为179的节点的 name 修改为 hello world, 返回修改后的整棵 树
//Change the name of the node in the tree when id is '179' to 'hello world', return the new tree.
treejs.modifyTreeNodeByField(tree, 'id', 179, 'name', 'hello world');

//将树中id为["179",'77'] 的节点的 设置为禁用, 返回修改后的整棵 树
//Set the node in the tree when id belong to ["179", "77"] to disabled, return the new tree
treejs.modifyTreeNodeDisabled(tree, 'id', ['179', '77'], true); //最后一位为是否禁用,如果为false,则表示取消禁用

//禁用整颗树, 最后一位如果为false, 则表示启用整颗树. 返回修改后的整颗树。
// Disable the whole tree, enable the whole tree when last param is "false", return the new tree.
treejs.modifyAllTreeDisabled(tree, true);

// 查找 树中id为179所对应的节点的 父节点
// Find the parent node when id is '179' in the tree.  return the parent node.
treejs.getTreeParentNode(tree, 'id', 179);
//{
//  id: 77,
//  name: 'level-1-2,
//  key: "2",
//  children: [
//    {
//      id: 179,
//      name: "level-1-2-1",
//      key: "2"
//    }
//  ]
//}

// 查找 树中id为[79,32]时,分别对应的 name 的列表.  默认只查叶子节点
// Find the list of names in the tree when the id belong to [79,32].  default the node is leaf.
treejs.getTreeDestList(tree1, 'id', [79, 32], 'name');
//["level-1-1", "level-1-2-1"]

//遍历所有的节点
// search all node of the tree.
treejs.getTreeDestList(tree1, 'id', [79, 32], 'name', false);
//["level-1", "level-1-1", "level-1-2-1"]

// 向 树中 id为179的节点,添加一个  子节点(添加的子节点在所有子节点的最后一位) ,返回整颗树
// add a child node when id is '179' in the tree. return the new tree.
treejs.addNodeToTree(tree, 'id', 179, {
  id: 120,
  name: 'hello world',
  key: 'd1',
});

// 向 树中 id为179的节点,添加一个兄弟节点(添加的节点在目标节点的相邻位置) ,返回整颗树
treejs.addNodeToTargetAfter(tree, 'id', 179, {
  id: 120,
  name: 'hello world',
  key: 'd1',
});

// 将 树中 id为179的节点,替换为另一个节点 {id: 120,name: "hello world",key: "d1",},并返回整颗树
// replace a child node that id is '179' in the tree. return the new tree.
treejs.replaceNodeFromTree(tree, 'id', 179, {
  id: 120,
  name: 'hello world',
  key: 'd1',
});

// 获取节点id为179的下一级节点,返回下一级的节点数组。类似于文件夹中的子文件夹或者文件
treejs.getChildLevel(tree, 'id', 179);

// 获取当id为179的上一级节点,返回上一级的节点数组。类似于从子文件夹返回上一层文件夹
treejs.getParentLevel(tree, 'id', 179);

//删除 树 中id为77的 子节点(子节点的后代也会被全部删除), 返回修改后的树
// Delete the child node in the tree when id is '77', the offspring of child node will be deleted. return the new tree.
treejs.removeNodeFromTree(tree, 'id', 77);

//获取 叶子节点的isWho属性值为0的树
treeJs.getFiltersTreeLeaf(tree, 'isWho', 0);