JSFiddle - React, Tailwind, and code Playground

by In Roam

HTML

<link rel="stylesheet" href="https://cdn.bootcss.com/iview/2.13.1/styles/iview.css">
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script src="https://cdn.bootcss.com/iview/2.13.1/iview.min.js"></script>
<div id="app">
  <i-row>
    <i-col span="12" offset="6">
      <Cascader :data="tree"></Cascader>
    </i-col>
    
    <i-col span="12" offset="6">
      <pre>{{ JSON.stringify(tree, null, 4) }}</pre>
    </i-col>
  </i-row>
</div>

JavaScript

const rawTree = [
    {
        "department_id": 1,
        "department_name": "Test",
        "super_department_id": "0",
        "child_departments": [
            {
                "department_id": "34",
                "department_name": "修图",
                "super_department_id": "1",
                "child_departments": []
            },
            {
                "department_id": "35",
                "department_name": "系统研发",
                "super_department_id": "1",
                "child_departments": [
                    {
                        "department_id": "48",
                        "department_name": "测试组",
                        "super_department_id": "35",
                        "child_departments": []
                    },
                    {
                        "department_id": "49",
                        "department_name": "产品组",
                        "super_department_id": "35",
                        "child_departments": []
                    },
                    {
                        "department_id": "50",
                        "department_name": "运营",
                        "super_department_id": "35",
                        "child_departments": []
                    },
                    {
                        "department_id": "51",
                        "department_name": "技术开发组",
                        "super_department_id": "35",
                        "child_departments": []
                    }
                ]
            }
        ]
    }
]

function convertTree (tree) {
    const result = []

    tree.forEach((item) => {
        let {
            department_id: value,
            department_name: label,
            child_departments: children
        } = item

        if (children) {
            children = convertTree(children)
        }

        result.push({
            value,
            label,
            children
        })
    })

    return result
}

const vm = new...