element-ui中树状图el-tree的使用(vue2)

热乎的,话不多说上代码
html部分:

  // data:展示的数据
  // accordion:是否每次只打开一个同级树节点展开
  // props:配置选项 默认值
  // expand-on-click-node:默认为true,值为false时点击小箭头才会展开收缩节点
  // filter-node-method:返回true节点可以显示,false节点会被隐藏
  // node-key:每个树节点的唯一标识
  // default-expand-all:是否默认展开所有节点,默认值为false
  // highlight-current: 选中节点是否高亮,默认值为false
  // @node-click:点击目录时的操作
  <el-tree
          :data="outlineContentList"
          accordion
          :props="defaultProps"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          ref="tree"
          node-key="id"
          default-expand-all
          highlight-current
          @node-click="mulucaozuo"
        >
  </el-tree>

2.在script中

<script>
// 引入树状图列表(目录)接口
import { pageOutline } from '@/utils/api.js'
export default{
  data(){
   return{
      defaultProps:{
        children:"children",
        label:"outlineName" 
     },
     //目录Id (接口提供的目录id名称)
     outlineId:null,
     //目录列表(自定义名称)
     outlineList:[],
     courseId:null,// 接口需要的参数
   }
 },
 created(){
 // 初次加载函数调用
  this.getCatague()
 },
 // 方法
 methods:{
 //(树状图列表)目录
   getCatague(){
   // 目录接口(标头传参)
    pageOutline(this.courseId).then(res=>{
     this.outlineList=this.hanldeTree(
       let config={
        id:id || "id",
        parentId: parentId || "parentId",
        childrenList: children || "children",
      };
      var childrenListMap = {};
      var nodeIds = {};
      var tree = [];

      for (let d of data) {
      let parentId = d[config.parentId];
      if (childrenListMap[parentId] == null) {
          childrenListMap[parentId] = [];
         }
         nodeIds[d[config.id]] = d;
         childrenListMap[parentId].push(d);
      }
      
      for (let d of data) {
      let parentId = d[config.parentId];
      if (nodeIds[parentId] == null) {
          tree.push(d);
        }
      }
      
      for (let t of tree) {
        adaptToChildrenList(t);
      }
      // console.log(childrenListMap, "data");
        function adaptToChildrenList(o) {
        if (childrenListMap[o[config.id]] !== null) {
          o[config.childrenList] = childrenListMap[o[config.id]];
        }
        if (o[config.childrenList]) {
          for (let c of o[config.childrenList]) {
            adaptToChildrenList(c);
          }
        }
      }
      return tree;
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点点击事件
    mulucaozuo(row) {
      this.loading = true;
      (this.outlineId = row.outlineId), this.detailedInformation();
      },
     )
   })
  }
 }
}
</script>

实现效果:
在这里插入图片描述
点击左侧目录出现对应的内容
固定方法就放心用吧!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小雪的博客.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值