el-tree 点击父节点无效,只能选中子节点

背景:

el-tree树形控件点击的时候,我们不希望点击父节点,只希望点击子节点触发效果。

实际使用示例:摄像头树状图,根据不同市级区域划分,我们只希望点击子节点【摄像头】有效,点击具体的摄像头调取网络接口,点击市级区域不调取接口。

官网示例:

官网示例代码:

javascript 复制代码
<template>
  <el-tree
    style="max-width: 600px"
    :data="data"
    :props="defaultProps"
    default-expand-all
    @node-click="handleNodeClick"
  />
</template>

<script lang="ts" setup>
interface Tree {
  label: string
  children?: Tree[]
}

const handleNodeClick = (data: Tree) => {
  console.log(data)
}

const data: Tree[] = [
  {
    label: 'Level one 1',
    children: [
      {
        label: 'Level two 1-1',
        children: [
          {
            label: 'Level three 1-1-1',
          },
        ],
      },
    ],
  },
  {
    label: 'Level one 2',
    children: [
      {
        label: 'Level two 2-1',
        children: [
          {
            label: 'Level three 2-1-1',
          },
        ],
      },
      {
        label: 'Level two 2-2',
        children: [
          {
            label: 'Level three 2-2-1',
          },
        ],
      },
    ],
  },
  {
    label: 'Level one 3',
    children: [
      {
        label: 'Level two 3-1',
        children: [
          {
            label: 'Level three 3-1-1',
          },
        ],
      },
      {
        label: 'Level two 3-2',
        children: [
          {
            label: 'Level three 3-2-1',
          },
        ],
      },
    ],
  },
]

const defaultProps = {
  children: 'children',
  label: 'label',
}
</script>

备注:从上面这个示例我们可以知道,点击父节点或者点击子节点都会触发节点点击事件,即触发节点点击事件@node-click="handleNodeClick"

要想仅仅点击子节点触发事件@node-click="handleNodeClick",需要在handleNodeClick事件内部判断点击的是否是子节点或者父节点,满足条件在执行,否则不执行。

实际效果图:

备注:在这个实际代码中,我们就可以判断type=== 100是否满足条件,满足的执行,不满足的不执行。

相关推荐
远方的小草6 小时前
Cursor(vscode) debug for Chrome
前端
喵了几个咪6 小时前
Headless 架构优势:内容与展示解耦,一套 API 打通全端生态
vue.js·架构·golang·cms·react·taro·headless
蒙面价肥猫6 小时前
el-popconfirm 弹窗不显示问题总结
vue.js·elementui
远方的小草6 小时前
线上调试代码,试试SourceMap?
前端
whuhewei6 小时前
React Fiber架构
前端·react.js·架构
陆枫Larry6 小时前
微信小程序:如何优雅地修改富文本(u-parse/rich-text)内部样式?
前端
远方的小草6 小时前
Nginx 反向代理
前端
英俊潇洒美少年6 小时前
通用构建优化(编译阶段)+ Vue 专属运行时优化 + React 专属运行时优化
前端·vue.js·react.js
慕斯fuafua6 小时前
CSS——样式
前端·css
一 乐7 小时前
非遗文化传承网站|基于springboot + vue非遗文化传承网站系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·非遗文化传承网站