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

siwi-tree

v1.0.6

Published

无限级分类 生成菜单

Downloads

26

Readme

node Build Status npm npm Github file size

siwi-tree

无限级分类 家谱树 子孙树 生成菜单 提供递归和迭代两种方式实现

install

use npm

npm install siwi-tree

use yarn

yarn add siwi-tree

Example

实例化参数

默认

默认情况下 数据如下

const data = [{
        id: 1,
        name: '中国',
        parent_id: 0
    },
    {
        id: 2,
        name: '台湾',
        parent_id: 1
    },
    {
        id: 3,
        name: '台北',
        parent_id: 2
    },
    {
        id: 4,
        name: '钓鱼岛',
        parent_id: 3
    },
    {
        id: 5,
        name: '日本',
        parent_id: 0
    },
    {
        id: 6,
        name: '东京',
        parent_id: 5
    },
]
const Tree = require('siwi-tree')
const tree = new Tree() // 这里不用传入参数

class Example {
    constructor() {
        this.init()
    }
    async init() {
        const familyTree = await tree.familyTree(data, 4)
        console.log(familyTree)
        const subTree = await tree.subTree(data, 2)
        console.log(subTree)
        const res = await tree.getTree(data)
        console.log(JSON.stringify(res))
    }
}

module.exports = new Example()

自定义下标

通常情况下 树的数据源是多样的 可以通过传入 id_index parent_index sub_index 指定数据源的对应下标

const data = [{
        id: 1,
        name: '中国',
        pid: 0
    },
    {
        id: 2,
        name: '台湾',
        pid: 1
    },
    {
        id: 3,
        name: '台北',
        pid: 2
    },
    {
        id: 4,
        name: '钓鱼岛',
        pid: 3
    },
    {
        id: 5,
        name: '日本',
        pid: 0
    },
    {
        id: 6,
        name: '东京',
        pid: 5
    },
]
const Tree = require('siwi-tree')
const id_index = 'id'
const parent_index = 'pid'
const sub_index = 'sub'
const tree = new Tree(id_index, parent_index, sub_index)

class Example {
    constructor() {
        this.init()
    }
    async init() {
        const familyTree = await tree.familyTree(data, 4)
        console.log(familyTree)
        const subTree = await tree.subTree(data, 2)
        console.log(subTree)
        const res = await tree.getTree(data)
        console.log(JSON.stringify(res))
    }
}

module.exports = new Example()

迭代方式

目前迭代实现 家谱树 子孙树

const data = [{
        id: 1,
        name: '中国',
        parent_id: 0
    },
    {
        id: 2,
        name: '台湾',
        parent_id: 1
    },
    {
        id: 3,
        name: '台北',
        parent_id: 2
    },
    {
        id: 4,
        name: '钓鱼岛',
        parent_id: 3
    },
    {
        id: 5,
        name: '日本',
        parent_id: 0
    },
    {
        id: 6,
        name: '东京',
        parent_id: 5
    },
]
const Tree = require('siwi-tree')
const tree = new Tree() // 这里不用传入参数

class Example {
    constructor() {
        this.init()
    }
    async init() {
        const familyTree = await tree.familyTreeIteration(data, 4)
        console.log(familyTree)
        const subTree = await tree.subTreeIteration(data, 2)
        console.log(subTree)
    }
}

module.exports = new Example()