更新记录
1.4.4(2026-05-14) 下载此版本
调整图片
1.4.2(2026-05-14) 下载此版本
- 节点右侧可选小图:
treeProps.rightImage映射数据字段,有非空路径则显示,否则不占位;right-image-size控制尺寸;right-image-click事件
平台兼容性
uni-app(4.01)
| Vue2 | Vue2插件版本 | Vue3 | Chrome | Chrome插件版本 | Safari | app-vue | app-vue插件版本 | app-nvue | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| √ | 1.4.2 | - | √ | 1.4.2 | - | √ | 1.4.2 | - | √ | 1.4.2 | √ | 1.4.2 | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | - | - | - | - | - | - |
uni-app x(4.01)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| - | - | - | - | - | - |
lv-tree
Vue2 uni-app 通用树形组件:左侧展开/折叠仅图片;支持复选框(含父子联动),可发布至 DCloud 插件市场。
目录说明
uni_modules/lv-tree/
├── static/
│ └── lv-tree-chevron.png # 默认展开图标(由组件内 require 引用,勿改文件名或需同步改代码)
├── components/lv-tree/
│ ├── lv-tree.vue 根组件(请使用此组件)
│ └── lv-tree-node.vue 内部节点(无需单独引用)
├── package.json
├── readme.md
└── changelog.md
安装
将 uni_modules/lv-tree 目录拷贝到目标工程的 uni_modules 下,或在 HBuilderX 中通过插件市场导入。
用法
<template>
<view class="page">
<lv-tree
ref="tree"
:data="treeData"
:tree-props="treeProps"
node-key="id"
:show-checkbox="true"
:default-expand-all="true"
@node-click="onNodeClick"
@leaf-click="onLeafClick"
@check-change="onCheckChange"
/>
</view>
</template>
<script>
export default {
data() {
return {
treeProps: {
children: 'children',
label: 'name',
disabled: 'disabled',
checkable: 'checkable',
rightImage: 'rightImage'
},
treeData: [
{
id: '1',
name: '一级',
children: [
{ id: '1-1', name: '二级 A' },
{ id: '1-2', name: '二级 B' }
]
}
]
}
},
methods: {
onNodeClick({ node, key, expanded }) {
console.log('click', key, expanded)
},
onLeafClick({ node, key }) {
console.log('叶子', key, node)
},
onCheckChange({ checkedNodes, checkedKeys }) {
console.log('keys', checkedKeys, 'nodes', checkedNodes)
},
readChecked() {
const nodes = this.$refs.tree.getCheckedNodes()
console.log(nodes)
}
}
}
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| data | Array | [] |
树数据,根级节点数组 |
| treeProps | Object | 见下 | 字段映射 |
| nodeKey | String | id |
节点唯一标识字段名(需在数据中可读且尽量唯一) |
| showCheckbox | Boolean | false |
整体开关:为 false 时不展示复选框列,不处理勾选 |
| checkStrictly | Boolean | false |
为 true 时父子勾选互不关联 |
| defaultExpandAll | Boolean | false |
是否默认展开全部 |
| defaultExpandedKeys | Array | [] |
默认展开的节点 key 列表 |
| defaultCheckedKeys | Array | [] |
默认勾选的节点 key 列表 |
| indent | Number | 24 |
每级缩进(单位 rpx) |
| expandIconSrc | String | 内置图 | 收起态图地址;默认 require 内置 static/lv-tree-chevron.png。传 '' 仍使用内置图(与默认等价) |
| collapseIconSrc | String | '' |
展开态图;不设且为自定义单图时,展开态对图旋转 90°。使用默认内置图时收起态旋转 -90° 为向右 |
| expandIconSize | Number | 48 |
展开外框/点击热区宽高(rpx);若 expand-icon-image-size 更大,外框会随之撑大 |
| expandIconImagePercent | Number | 70 |
图标相对外框的宽高百分比(1–100)。若 expand-icon-image-size > 0 则忽略 |
| expandIconImageSize | Number | 0 |
图标固定宽高(rpx);> 0 时优先生效,外框为 max(expandIconSize, expandIconImageSize) |
| rightImageSize | Number | 40 |
节点右侧小图宽高(rpx);仅当该节点配置了右侧图字段且非空时渲染 |
展开/折叠(仅 <image>,无文本字符)
<!-- 内置 chevron:收起向右、展开向下 -->
<lv-tree />
<!-- 双图 -->
<lv-tree
expand-icon-src="/static/tree-right.png"
collapse-icon-src="/static/tree-down.png"
:expand-icon-size="40"
/>
<!-- 自定义单图(右箭头):展开时旋转 90° -->
<lv-tree expand-icon-src="/static/arrow-right.png" />
<!-- 图标占满外框 / 固定 rpx -->
<lv-tree :expand-icon-image-percent="100" />
<lv-tree :expand-icon-image-size="32" :expand-icon-size="48" />
treeProps 默认值
{ children: 'children', label: 'label', disabled: 'disabled', checkable: 'checkable', rightImage: 'rightImage' }
节点上若 checkable === false(字段名可通过 treeProps.checkable 修改),则该行不显示勾选框,且该节点不参与勾选与 getCheckedKeys;父级勾选时仅作用于可勾选的后代。
右侧小图:在 treeProps 中配置 rightImage 为节点对象上的字段名(默认 'rightImage')。该字段为非空字符串(图片路径)时在行末展示;未配置、字段不存在或为空字符串时不展示(不占位)。可用 right-image-size(rpx)控制尺寸。点击右侧图会触发 right-image-click。
Events
| 事件名 | 参数 | 说明 |
|---|---|---|
| node-click | 见下 | 点击节点行;含 node(完整节点对象)、key、expanded;开启复选框时附带当前 checkedKeys、checkedNodes、halfCheckedKeys、halfCheckedNodes |
| leaf-click | 见下 | 叶子行点击;含 node、key;开启复选框时同上附带勾选快照 |
| check-change | 见下 | 含 node、key、checked、halfChecked、checkedKeys、checkedNodes、halfCheckedKeys、halfCheckedNodes |
| expand-change | { key, expanded, node } |
node 为当前节点完整对象(无则 null) |
| right-image-click | { node, key, src } |
点击节点右侧小图(不冒泡到行点击) |
事件参数补充
check-change:checkedNodes/halfCheckedNodes为与checkedKeys/halfCheckedKeys一一对应的原始节点对象(树里data中的引用)。node-click/leaf-click:在show-checkbox为true时额外携带checkedKeys、checkedNodes、halfCheckedKeys、halfCheckedNodes(点击瞬间的快照)。
方法(ref)
| 方法 | 说明 |
|---|---|
| getCheckedKeys(onlyLeaf) | 获取已勾选节点的 key 列表 |
| getCheckedNodes(onlyLeaf) | 获取已勾选节点的 完整对象数组(与 keys 顺序一致) |
| getHalfCheckedKeys() | 半选节点的 key 列表 |
| getHalfCheckedNodes() | 半选节点的完整对象数组 |
| setCheckedKeys(keys) | 按 key 设置勾选,会触发 check-change |
注意事项
nodeKey对应字段在整棵树中应唯一,否则勾选与展开状态可能异常。- 若节点无
children或children为空数组,视为叶子节点。 - 左侧展开/折叠仅使用图片(
<image>),已移除expand-icon-text、collapse-icon-text、expand-icon-class、expand-icon-color、expand-icon-font-size等文本相关配置。 - 本插件为纯 Vue 组件,未使用 UTS;若仅需 App 原生能力,可自行扩展
utssdk目录(非必填)。
License
MIT

收藏人数:
下载插件并导入HBuilderX
下载示例项目ZIP
赞赏(0)
下载 1
赞赏 0
下载 11912408
赞赏 1914
赞赏
京公网安备:11010802035340号