广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >js如何构造elementUI树状菜单的数据结构详解
  • 547
分享到

js如何构造elementUI树状菜单的数据结构详解

2024-04-02 19:04:59 547人浏览 八月长安
摘要

背景说明 elementUI中自带树状菜单,就是数据结构有点复杂,偏向JSON风格。 数据库中菜单数据是二维表格,通过parentPk定义上下级,是list型。 需要把list转换

背景说明

elementUI中自带树状菜单,就是数据结构有点复杂,偏向JSON风格。

数据库中菜单数据是二维表格,通过parentPk定义上下级,是list型。

需要把list转换成tree的结构。

elementUI树状菜单的数据结构

每个节点有4个属性,id、label、newVal、children数组

通过children数组包含关系标示上下级。


var treeData={
        id: 1,
        label: '一级 1',
        newVal: "",
        children: [{
          id: 4,
          label: '二级 1-1',
          newVal: "",
          children: [{
            id: 9,
            label: '三级 1-1-1',
            newVal: "",
          }, {
            id: 10,
            label: '三级 1-1-2',
            newVal: "",
            children:[{
              id: 4444,
            label: '四级 1-1-1-4',
            newVal: "",
            }]
          }]
        },{
          id:22,
          label:'二级 22',
          newVal:''
        }]
      }

数据库返回的list


var itemlist =[
        {itemCode:'11', itemName:'材料11',itemType:'2',parentPk:'1'},
        {itemCode:'111', itemName:'材料111',itemType:'3',parentPk:'11'},  
        {itemCode:'1111', itemName:'材料1111',itemType:'3',parentPk:'111'},
        {itemCode:'1112', itemName:'材料1112',itemType:'3',parentPk:'111'} 
      ]

设计思路

递归方法;

  1. 从list中遍历,找parentPk是当前节点的id的对象,组装成node,放到当前节点的children数组;同时,把list的对象删除。
  2. 对新的node,递归执行找子节点的过程。
  3. 退出条件:list为空或者循环list完毕。

具体代码


//root节点
全局对象,因为不同的递归执行,要访问的一个tree对象
var itemtree ={
    id:'1',
    label:'物料名称_整机',
    children:[]
}

//数据库返回的菜单list
全局对象,因为不同的递归执行,要访问的一个list对象
var itemlist =[
        {itemCode:'11', itemName:'材料11',itemType:'2',parentPk:'1'},
        {itemCode:'12', itemName:'材料12',itemType:'2',parentPk:'1'},
        {itemCode:'111', itemName:'材料111',itemType:'3',parentPk:'11'},  
        {itemCode:'1111', itemName:'材料1111',itemType:'3',parentPk:'111'},
        {itemCode:'1112', itemName:'材料1112',itemType:'3',parentPk:'111'} 
      ]

function buildtree(itemtreenode,itemlist){
  if (itemlist.length===0) {
    console.log('条件结束')
    return 
  }
  var j=0   
//   var len=0
  for(j=0,len=itemlist.length;j<len;j++){
    console.log(new Date(),'j==>:',j,'len==>:',len,itemtreenode,itemlist)
    if (itemtreenode.id===itemlist[j].parentPk){
      var node={id:itemlist[j].itemCode,label:itemlist[j].itemName,children:[]}
      itemtreenode.children.push(node)    
//       itemlist.splice(j,1)  
      buildtree(node,itemlist)      
  }    
  
}
  console.log('循环结束')
}

console.log('begin')
buildtree(itemtree,itemlist)  
console.log(itemtree)

代码执行结果

可以看到组装树是正确的。

总结

ps:和设计方案对比,代码不是很完美,list中被引用的元素没有成功移除;移除后,后边会报错。暂时没找到好方法,对性能有点影响。

树data转list代码

与此相反的操作。


var treeData={
        id: 1,
        label: '一级 1',
        newVal: "",
        children: [{
          id: 4,
          label: '二级 1-1',
          newVal: "",
          children: [{
            id: 9,
            label: '三级 1-1-1',
            newVal: "",
          }, {
            id: 10,
            label: '三级 1-1-2',
            newVal: "",
            children:[{
              id: 4444,
            label: '四级 1-1-1-4',
            newVal: "",
            }]
          }]
        },{
          id:22,
          label:'二级 22',
          newVal:'',
          children:[{id:'2-2-1',label:'三级221',newVal:'',children:[],}]
        }]
      }

var exp=undefined

var itemlist=[]
function tree2list(itemnode){
  if(typeof(itemnode)=="undefined"){
    console.log('返回:',itemnode)
    return
  }
  
  if(itemnode.children && itemnode.children.length>0){
  	var i=0  
    for(i=0;i<itemnode.children.length;i++){
      itemnode.children[i].parentPk=itemnode.id
      console.log(itemnode.children[i])
      itemlist.push(itemnode.children[i])       
      this.tree2list(itemnode.children[i])
    }
  }
}
 

console.log('begin')
tree2list(treeData,itemlist)
console.log(itemlist)  

到此这篇关于js如何构造elementUI树状菜单的数据结构的文章就介绍到这了,更多相关js构造elementUI树状菜单内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: js如何构造elementUI树状菜单的数据结构详解

本文链接: https://www.lsjlt.com/news/125843.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • js如何构造elementUI树状菜单的数据结构详解
    背景说明 elementUI中自带树状菜单,就是数据结构有点复杂,偏向json风格。 数据库中菜单数据是二维表格,通过parentPk定义上下级,是list型。 需要把list转换...
    99+
    2022-11-12
  • JavaScript如何处理树状结构数据的增删改查
    这篇文章主要介绍“JavaScript如何处理树状结构数据的增删改查”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“JavaScript如何处理树状结构数据的增删改查...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作