Git Product home page Git Product logo

buildtree.js's Introduction

buildTree.js

建立父、子階層的樹狀結構。

階層轉換

轉換前:

const sidebarList = [
     { number: 1, parent_number: null, title: '基本管理'},
     { number: 2, parent_number: null, title: '系統管理' },
     { number: 3, parent_number: 2 ,title: '使用者管理' },
     { number: 4, parent_number: 3 ,title: '權限管理' },
     { number: 5, parent_number: 3 ,title: '帳號管理' }
]

轉換後:

[{
    "number": 1,
    "parent_number": null,
    "title": "基本管理",
    "children": []
}, {
    "number": 2,
    "parent_number": null,
    "title": "系統管理",
    "children": [{
        "number": 3,
        "parent_number": 2,
        "title": "使用者管理",
        "children": [{
            "number": 4,
            "parent_number": 3,
            "title": "權限管理",
            "children": []
        }, {
            "number": 5,
            "parent_number": 3,
            "title": "帳號管理",
            "children": []
        }]
    }]
}]

使用方法

1.引入buildTree.js

<script src="buildTree.js"></script>

2.實作

const sidebarList = [
     { number: 1, parent_number: null, title: '基本管理'},
     { number: 2, parent_number: null, title: '系統管理' },
     { number: 3, parent_number: 2 ,title: '使用者管理' },
     { number: 4, parent_number: 3 ,title: '權限管理' },
     { number: 5, parent_number: 3 ,title: '帳號管理' }
]

// 產生樹
const tree = buildTree({
    data:sidebarList,
    id:'number', // PK
    parent_id:'parent_number' // FK
})

buildtree.js's People

Contributors

yuhsiang237 avatar

Watchers

 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.