vue遞歸生成樹狀結(jié)構(gòu)的示例代碼
更新時間:2024年07月24日 11:08:27 作者:前端小葛
這篇文章主要介紹了vue遞歸生成樹狀結(jié)構(gòu)的示例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
生成前
生成后
console.log打印
全部實(shí)現(xiàn)代碼片段(附備注
<template> <div class="container" style="font-size: 20px; color: red; text-align: center" > 樹狀結(jié)構(gòu)遞歸生成 </div> <div style="text-align: center"> <el-button type="primary" @click="createTree">點(diǎn)擊生成樹狀結(jié)構(gòu)</el-button> </div> <!-- 結(jié)構(gòu)表格 --> <el-table :data="tableData" style="width: 100%" row-key="id" default-expand-all > <el-table-column prop="id" label="id" width="200" /> <el-table-column prop="fatherId" label="fatherId" width="100" /> <el-table-column prop="name" label="備注" /> </el-table> </template> <script setup> import { create } from "lodash"; import { reactive, ref } from "vue"; const state = reactive({ newTree: [], }); // id 數(shù)據(jù)id // fatherId 子節(jié)點(diǎn)所對應(yīng)的父節(jié)點(diǎn)的id,即如果fatherId = 某一項(xiàng)的id。那他就是那一項(xiàng)的子項(xiàng) // name 此處用作備注 const tableData = ref([ { id: 1, name: "我是父節(jié)點(diǎn)1", fatherId: 0, }, { id: 2, name: "我是父節(jié)點(diǎn)2", fatherId: 0, }, { id: 3, name: "我是父節(jié)點(diǎn)1的子節(jié)點(diǎn)1-1", fatherId: 1, }, { id: 4, name: "我是父節(jié)點(diǎn)2的子節(jié)點(diǎn)2-1", fatherId: 2, }, { id: 5, name: "我是父節(jié)點(diǎn)2的子節(jié)點(diǎn)2-2", fatherId: 2, }, { id: 6, name: "我是父節(jié)點(diǎn)2的子節(jié)點(diǎn)2-3", fatherId: 2, }, { id: 7, name: "我是父節(jié)點(diǎn)2-3的子節(jié)點(diǎn)2-3-1", fatherId: 6, }, { id: 8, name: "我是父節(jié)點(diǎn)2-3的子節(jié)點(diǎn)2-3-2", fatherId: 6, }, { id: 9, name: "我是父節(jié)點(diǎn)2-3-1的子節(jié)點(diǎn)2-3-1-1", fatherId: 7, }, { id: 10, name: "我是父節(jié)點(diǎn)2-1的子節(jié)點(diǎn)2-1-1", fatherId: 4, }, ]); //生成樹狀結(jié)構(gòu) const createTree = () => { console.log("我是生成前的結(jié)構(gòu)", tableData.value); state.newTree = handleTree(tableData.value, "id", "fatherId"); console.log("我是生成后的結(jié)構(gòu)", state.newTree); setTimeout(() => { tableData.value = state.newTree; }, 2000); }; //_________________________________ // data 數(shù)據(jù)源 // id id字段 默認(rèn) 'id' // parentId 父節(jié)點(diǎn)字段 默認(rèn) 'parentId' // children 孩子節(jié)點(diǎn)字段 默認(rèn) 'children // rootId 根Id 默認(rèn) 0 const handleTree = (data, id, parentId, children, rootId) => { id = id || "id"; parentId = parentId || "parentId"; children = children || "children"; rootId = rootId || 0; //對源數(shù)據(jù)深度克隆 const cloneData = JSON.parse(JSON.stringify(data)); //循環(huán)所有項(xiàng) const treeData = cloneData.filter((father) => { let branchArr = cloneData.filter((child) => { //返回每一項(xiàng)的子級數(shù)組 return father[id] === child[parentId]; }); branchArr.length > 0 ? (father.children = branchArr) : ""; //返回第一層 return father[parentId] === rootId; }); return treeData != "" ? treeData : data; }; </script> <style scoped lang="scss"></style>
到此這篇關(guān)于vue遞歸生成樹狀結(jié)構(gòu)的文章就介紹到這了,更多相關(guān)vue遞歸生成樹狀結(jié)構(gòu)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問題
這篇文章主要介紹了Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07vue監(jiān)聽sessionStorage中值的變化方式
這篇文章主要介紹了vue監(jiān)聽sessionStorage中值的變化方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07Vue引入jquery實(shí)現(xiàn)平滑滾動到指定位置
這篇文章主要介紹了Vue引入jquery實(shí)現(xiàn)平滑滾動到指定位置的效果,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05Vue自定義指令實(shí)現(xiàn)點(diǎn)擊右鍵彈出菜單示例詳解
這篇文章主要為大家介紹了Vue自定義指令實(shí)現(xiàn)點(diǎn)擊右鍵彈出菜單示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法
今天小編就為大家分享一篇在vue項(xiàng)目中,將juery設(shè)置為全局變量的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09