根据条件控制el-tree节点显示隐藏

实现el-tree组件根据后台提供的全量数据,前端通过filterNode方法动态控制树形结构中包含特定关键词如VS的节点显示,其他节点隐藏。

需求:

后台返回el-tree全量数据,前端根据条件控制每个节点显示隐藏

实现:

获取后台数据后,调用this.$refs.componentTree.filter(“VS”);触发filterNode 方法,按照条件控制显示

效果:(条件:只显示name中包含 "VS"的节点,)

在这里插入图片描述

代码:

<template>
  <div style="padding: 20px">
    <el-tree
      ref="componentTree"
      style="height: 100%"
      class="hx-scroll"
      :data="componentTreeData"
      highlight-current
      show-checkbox
      node-key="id"
      :expand-on-click-node="false"
      default-expand-all
      :filter-node-method="filterNode"
      :props="{ label: 'name', children: 'children' }"
    >
    </el-tree>
  </div>
</template>

<script>
export default {
   
   
  name: "test",
  data() {
   
   
    return {
   
   
      componentTreeData: [
        {
   
   
          children: [
            {
   
   
              budgetVersionId: 1842182452449281,
              children: [
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011922,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "柱、暗柱VS柱",
                  new: false,
                  orderNo: 2,
                  parentId: 1849351790963362,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "柱,暗柱",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351790963361,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "柱帽VS柱帽",
                  new: false,
                  orderNo: 3,
                  parentId: 1849351790963362,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "柱帽",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011921,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "构造柱",
                  new: false,
                  orderNo: 4,
                  parentId: 1849351790963362,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "构造柱",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
              ],
              count: 0,
              createTime: 1682301810000,
              createdHostIp: "10.1.46.78,10.42.0.0",
              creator: "system",
              creatorId: -1,
              id: 1849351790963362,
              lastOperator: "韦民扬",
              lastOperatorId: 1658858666192897,
              name: "柱",
              new: false,
              orderNo: 1,
              projectId: 1774308439969793,
              reportType: 1,
              status: 1,
              type: 1,
              updateHostIp: "10.1.46.78,10.42.0.0",
              updateTime: 1682301810000,
            },
            {
   
   
              budgetVersionId: 1842182452449281,
              children: [
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011920,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "混凝土墙VS剪力墙",
                  new: false,
                  orderNo: 6,
                  parentId: 1849351790963360,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "剪力墙",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351790963391,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "砌体墙VS砌体墙",
                  new: false,
                  orderNo: 7,
                  parentId: 1849351790963360,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "砌体墙",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
              ],
              count: 0,
              createTime: 1682301810000,
              createdHostIp: "10.1.46.78,10.42.0.0",
              creator: "system",
              creatorId: -1,
              id: 1849351790963360,
              lastOperator: "韦民扬",
              lastOperatorId: 1658858666192897,
              name: "墙",
              new: false,
              orderNo: 5,
              projectId: 1774308439969793,
              reportType: 1,
              status: 1,
              type: 1,
              updateHostIp: "10.1.46.78,10.42.0.0",
              updateTime: 1682301810000,
            },
            {
   
   
              budgetVersionId: 1842182452449281,
              children: [
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351790963390,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "门VS门",
                  new: false,
                  orderNo: 9,
                  parentId: 1849351792011919,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "门",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011918,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "窗VS窗",
                  new: false,
                  orderNo: 10,
                  parentId: 1849351792011919,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "窗",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351790963389,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "门联窗",
                  new: false,
                  orderNo: 11,
                  parentId: 1849351792011919,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "门联窗",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011917,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "飘窗",
                  new: false,
                  orderNo: 12,
                  parentId: 1849351792011919,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "飘窗",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351790963388,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "墙洞VS墙洞",
                  new: false,
                  orderNo: 13,
                  parentId: 1849351792011919,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "墙洞",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011916,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "板洞",
                  new: false,
                  orderNo: 14,
                  parentId: 1849351792011919,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "板洞",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
              ],
              count: 0,
              createTime: 1682301810000,
              createdHostIp: "10.1.46.78,10.42.0.0",
              creator: "system",
              creatorId: -1,
              id: 1849351792011919,
              lastOperator: "韦民扬",
              lastOperatorId: 1658858666192897,
              name: "门窗洞",
              new: false,
              orderNo: 8,
              projectId: 1774308439969793,
              reportType: 1,
              status: 1,
              type: 1,
              updateHostIp: "10.1.46.78,10.42.0.0",
              updateTime: 1682301810000,
            },
            {
   
   
              budgetVersionId: 1842182452449281,
              children: [
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011915,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "梁、环梁VS梁",
                  new: false,
                  orderNo: 16,
                  parentId: 1849351790963387,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "梁",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351790963386,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "连梁",
                  new: false,
                  orderNo: 17,
                  parentId: 1849351790963387,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "连梁",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351792011914,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "圈梁",
                  new: false,
                  orderNo: 18,
                  parentId: 1849351790963387,
                  projectId: 1774308439969793,
                  reportType: 1,
                  status: 1,
                  thirdCommpent: "圈梁",
                  type: 2,
                  updateHostIp: "10.1.46.78,10.42.0.0",
                  updateTime: 1682301810000,
                },
                {
   
   
                  budgetVersionId: 1842182452449281,
                  count: 0,
                  createTime: 1682301810000,
                  createdHostIp: "10.1.46.78,10.42.0.0",
                  creator: "system",
                  creatorId: -1,
                  id: 1849351790963385,
                  lastOperator: "韦民扬",
                  lastOperatorId: 1658858666192897,
                  name: "过梁",
                  new: false,
                  orderNo: 19,
                  parentId: 1849351790963387,
                  projectId: 1774308439969793,
   
<think>嗯,用户问的是关于el-tree节点隐藏的问题。首先,我得回想一下Element UI中的el-tree组件,它常用于展示树形结构数据。用户可能在使用过程中遇到了需要隐藏某些节点的需求,比如根据条件动态隐藏,或者在特定情况下不显示某些节点。 首先,我需要确认el-tree是否直接支持节点隐藏的属性或方法。记得Element UI的文档中,el-tree节点配置是通过data数组里的对象,每个节点可以设置各种属性。可能有一个属性叫`visible`或者`show`,但不确定。或者可能需要通过其他方式实现,比如CSS或者条件渲染。 如果直接属性不存在,可能需要用v-if或者v-show来控制节点显示隐藏。不过el-tree的数据是响应式的,可能动态修改数据源来过滤节点会更有效。或者利用el-tree的过滤方法,比如使用`filter-node-method`属性,通过过滤函数来隐藏不符合条件的节点。 另外,用户可能希望隐藏节点但保留其数据,这样在后续操作中可能需要用到这些节点。这时候用CSS设置display:none可能更合适,或者通过过滤方法返回false来隐藏节点。需要确认filter-node-method的工作机制,是否只是隐藏节点而不是移除。 还可能要考虑性能问题,如果数据量很大,频繁过滤或修改数据源是否会导致渲染问题。这时候使用内置的过滤功能可能更高效,因为el-tree内部已经做了优化。 此外,用户可能需要动态切换节点显示状态,比如点击按钮隐藏某些节点。这时候可能需要维护一个状态变量,控制过滤条件,然后调用el-tree的filter方法触发重新渲染。 总结一下,可能的解决方案有几种:使用filter-node-method方法过滤节点,在数据源中动态排除不需要的节点,或者用CSS隐藏。需要详细说明每种方法的步骤,并提供示例代码,方便用户理解。 还要考虑用户可能对Vue和Element UI的熟悉程度,所以解释时要尽量清晰,分步骤说明,并给出代码示例。比如,如何设置filter方法,如何绑定过滤条件,如何触发过滤动作等。 可能还需要提到,如果用户希望某些节点初始不可见,可以在数据初始化时设置相应的属性,或者在mounted生命周期中调用过滤方法。同时提醒用户,隐藏节点和删除节点的区别,确保他们不会因为隐藏而导致数据丢失。 最后,检查是否有其他相关属性或方法,比如lazy load是否影响节点隐藏,或者是否有关联的API如updateKey等需要特别注意的地方。确保给出的信息准确且全面,避免遗漏重要细节。</think>在 Element UI 的 `el-tree` 组件中,隐藏节点可以通过以下几种方式实现: --- ### **1. 数据驱动隐藏** 通过动态修改树形结构的 `data` 数据源,过滤掉需要隐藏节点。这是最直接的方式,但会**真正移除节点数据**(非仅视觉隐藏)。 **示例代码:** ```javascript export default { data() { return { treeData: [ { label: '父节点', id: 1, children: [...] }, // ...其他节点 ] }; }, methods: { hideNode(nodeId) { this.treeData = this.treeData.filter(node => node.id !== nodeId); } } }; ``` --- ### **2. 使用 `filter-node-method` 过滤显示** 通过内置的过滤方法,实现节点的视觉隐藏节点数据仍保留,仅不显示)。 **步骤:** 1. 设置 `filter-node-method` 属性,定义过滤函数。 2. 绑定一个过滤关键词 `filterText`,动态触发过滤。 **示例代码:** ```html <template> <el-tree :data="treeData" :filter-node-method="filterNode" :filter="filterText" /> </template> <script> export default { data() { return { filterText: '', treeData: [/* 树数据 */] }; }, methods: { filterNode(value, data) { if (!value) return true; // 返回 true 表示显示,false 表示隐藏 return data.label.includes(value); } } }; </script> ``` --- ### **3. 通过 CSS 控制样式** 直接隐藏节点元素(视觉隐藏,但节点仍存在)。 **示例代码:** ```css /* 隐藏所有 class 包含 'is-hidden' 的节点 */ .el-tree-node.is-hidden { display: none; } ``` 在节点数据中动态添加 `class`: ```javascript // 在节点数据中设置 className { label: '隐藏节点', className: 'is-hidden', // ... } ``` --- ### **4. 动态控制 `disabled` 属性** 虽然不是直接隐藏,但可以通过禁用节点 (`disabled: true`) 达到类似效果: ```javascript { label: '禁用节点', disabled: true, // ... } ``` --- ### **选择建议** - 若需**完全移除节点**,用 **数据驱动**。 - 若需**保留数据但视觉隐藏**,用 `filter-node-method`。 - 若需快速实现简单隐藏,用 **CSS**。 根据你的场景选择合适的方式即可!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值