VUE element-ui之el-tree树形控件循环遍历渲染dom节点;子节点横向排列;控件添加指示线_两个el-tree的循环遍历(2)

总结

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

                  // label: '页面查看权限'
                  // }
                  // ]
                },
                {
                  id: 7,
                  label: '查询',
                  // children: [
                  // {
                  // id: 7,
                  // label: '页面查看权限'
                  // }
                  // ]
                }
              ]
            }
          ]
        },
        {
          id: 111,
          label: '销售管理',
          children: [
            {
              id: 112,
              label: '订单管理',
              children: [
                {
                  id: 113,
                  label: '列表查看'
                },
                {
                  id: 114,
                  label: '查询'
                },
                {
                  id: 114,
                  label: '重置'
                },
                {
                  id: 114,
                  label: '复选框'
                },
                {
                  id: 114,
                  label: '新增订单'
                },
                {
                  id: 114,
                  label: '导出'
                },
                {
                  id: 114,
                  label: '打印'
                },
                {
                  id: 114,
                  label: '操作'
                },
                {
                  id: 114,
                  label: '订单详情'
                },
                {
                  id: 114,
                  label: '编辑'
                },
                {
                  id: 114,
                  label: '作废'
                },
                {
                  id: 114,
                  label: '出库'
                },
                {
                  id: 114,
                  label: '结束'
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  defaultProps: {
    children: 'children',
    label: 'label'
  }
}

}


**js部分:**



mounted() {
this.changeCss() //默认调用此方法将子节点渲染为横向排列
},



methods: {
handleExpand() { // 节点被展开时触发的事件
// 因为该函数执行在renderContent函数之前,所以得加this. n e x t T i c k ( ) t h i s . nextTick() this. nextTick()this.nextTick(() => {
this.changeCss()
})
},
renderContent(h, { node, data, store }) { // 树节点的内容区的渲染 Function
let classname = ‘’
// 由于项目中有三级菜单也有四级级菜单,就要在此做出判断
if (node.level === 4) {
classname = ‘foo’
}
if (node.level === 3 && node.childNodes.length === 0) {
classname = ‘foo’
}
return h(
‘p’,
{
class: classname
},
node.label
)
},
changeCss() { //将子节点横向排列方法
var levelName = document.getElementsByClassName(‘foo’) // levelname是上面的最底层节点的名字
for (var i = 0; i < levelName.length; i++) {
// cssFloat 兼容 ie6-8 styleFloat 兼容ie9及标准浏览器
levelName[i].parentNode.style.cssFloat = ‘left’ // 最底层的节点,包括多选框和名字都让他左浮动
levelName[i].parentNode.style.styleFloat = ‘left’
levelName[i].parentNode.onmouseover = function() {
this.style.backgroundColor = ‘#fff’
}
}
}
}


**指示线样式部分:**



  • 21
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `el-select` 和 `el-tree` 配合使用来实现树形结构下拉单选。 首先,需要将数据转换为 `el-tree` 的节点格式,例如以下数据: ```javascript const treeData = [ { id: 1, label: '节点1', children: [ { id: 2, label: '节点1-1' }, { id: 3, label: '节点1-2' } ] }, { id: 4, label: '节点2', children: [ { id: 5, label: '节点2-1' }, { id: 6, label: '节点2-2' } ] } ] ``` 需要转换为 `el-tree` 的节点格式: ```javascript const treeNodes = [ { id: 1, label: '节点1', children: [ { id: 2, label: '节点1-1', isLeaf: true }, { id: 3, label: '节点1-2', isLeaf: true } ] }, { id: 4, label: '节点2', children: [ { id: 5, label: '节点2-1', isLeaf: true }, { id: 6, label: '节点2-2', isLeaf: true } ] } ] ``` 其中,每个节点需要添加 `isLeaf` 属性,用于标记该节点是否为叶子节点。 接着,在 `el-select` 中使用 `el-tree` 作为下拉选项。代码示例: ```html <template> <el-select v-model="selectedItem" placeholder="请选择" clearable> <el-tree :data="treeNodes" :props="treeProps" :node-key="treeProps.id" :highlight-current="true" :default-expand-all="true" :expand-on-click-node="false" v-if="treeVisible"></el-tree> <el-option v-else v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option> </el-select> </template> <script> export default { data() { return { selectedItem: null, treeNodes: [ { id: 1, label: '节点1', children: [ { id: 2, label: '节点1-1', isLeaf: true }, { id: 3, label: '节点1-2', isLeaf: true } ] }, { id: 4, label: '节点2', children: [ { id: 5, label: '节点2-1', isLeaf: true }, { id: 6, label: '节点2-2', isLeaf: true } ] } ], treeProps: { children: 'children', label: 'label', isLeaf: 'isLeaf' }, options: [ { label: '选项1', value: '1' }, { label: '选项2', value: '2' }, { label: '选项3', value: '3' } ], treeVisible: false } }, watch: { selectedItem(val) { if (val && val.children && val.children.length > 0) { this.treeVisible = true } else { this.treeVisible = false } } } } </script> ``` 上述代码中,`el-select` 的下拉选项分为两部分:`el-tree` 和 `el-option`。根据当前选中的节点是否有子节点,判断显示 `el-tree` 还是 `el-option`。 在 `el-tree` 中,需要设置 `props` 属性,指定节点数据中的属性名,以及 `node-key` 属性,用于标识节点的唯一属性名。其他属性根据实际需求进行设置。 在 `watch` 中,根据当前选中的节点是否有子节点,控制 `el-tree` 的显示与隐藏。 注意:以上代码只实现了树形结构下拉单选的基本功能,还需要根据实际需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值