Giter Club home page Giter Club logo

siwi-tree's Introduction

node Build Status npm npm Github file size

siwi-tree

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

install

use npm

npm install siwi-tree

use yarn

yarn add siwi-tree

Example

实例化参数

参数 类型 是否必选 默认值 含义
id_index String id 指定id下标
parent_index String parent_id 指定parent_id下标
sub_index String children 指定sub下标

默认

默认情况下 数据如下

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()

siwi-tree's People

Contributors

siwilizhao avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.