关于el-tree树形结构的理解

根据数据结构可划分为两种方式进行树形构建。特别地,还有一种节点可选形式。

(1)数据data是一个由多个对象组成的数组,对象含有children属性(里面是多个对象组成的数组)

  • 获取树形结构的数据data="treeData"
  • 设置树形配置项props="treeProps",对应着数据每一个对象的属性
  • 设置默认展开项数组default-expanded-keys="default_expanded_keys",默认展开第一项所有子树
  •  点击节点(分等级)@node-click="node_click(obj,node)",获取对应的(列表)需要的数据
<!--树形结构:data -->
<template>
  <div>
    <el-tree ref="el_tree"
             :data="treeData"
             :props="treeProps"
             node-key="id"
             :default-expanded-keys="default_expanded_keys"
             @node-click="node_click">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span class="cust-label">{
  { node.label }}</span>
      </span>
    </el-tree>
  </div>

</template>

<script>
export default {
  name: "index",
  data() {
    return {
      //树形的配置项
      treeProps: {
        //节点标签---节点对象的label属性
        label: 'label',
        //子树----节点对象的children属性
        children: 'children',
        //是否为叶子节点--节点对象的leaf属性
        // isLeaf:'leaf'
      },
      //默认展开节点数据
      default_expanded_keys: [],
      treeData: [
        {//根节点
          id: 1,
          label: '全部',
          children: [{//一级节点
            id: 2,
            label: '一级 1',
            children: [{//二级节点
              id: 3,
              label: '二级 1-1',
              children: [{//三级节点
                id: 4,
                label: '三级 1-1-1'
              }]
            }]
          },
            {
              id: 5,
              label: '一级 2',
              children: [{
                id: 6,
                label: '二级 2-1',
                children: [{
                  id: 7,
                  label: '三级 2-1-1'
                }]
              },
                {
                  id: 8,
                  label: '二级 2-2',
                  child
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值