更新记录
1.0.2(2024-07-11)
增加vue2适配
1.0.1(2024-07-08)
文档更新
1.0.0(2024-07-08)
新建
查看更多
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.1.0 app-vue |
√ |
√ |
√ |
√ |
√ |
√ |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
√ |
√ |
√ |
√ |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
ev-tree
功能说明:
- 理论上支持大数据量
- 支持子节点按需渲染配置
- 支持单选多选配置
- 支持父节点是否可选配置
- 支持手风琴模式配置(每次只展开一个同级树节点)
- 支持父子节点是否相互关联配置
- 支持样式自定义,提供插槽
- 支持异步加载子节点
- 支持配置主题色
- 支持vue2+3
- 提供方法设置目前选中的节点(支持配置是否自动打开父节点)
- 提供方法设置节点是否被选中(支持配置是否自动打开父节点)
- 提供方法展开或关闭指定节点
- 提供方法展开或关闭所有节点
- 提供节点点击事件监听(包含打开/关闭,选中/取消选中
调用示例
<view class="content">
<view class="operate-button" style="overflow-y: scroll">
<view class="" style="padding: 20rpx 0; text-align: center">
<text>操作列表</text>
</view>
<view>
<checkbox :checked="accordion" @click="accordion = !accordion" />
<text>手风琴模式</text>
</view>
<view>
<checkbox :checked="multipe" @click="multipe = !multipe" />
<text>多选</text>
</view>
<view>
<checkbox :checked="showCheckBox" @click="showCheckBox = !showCheckBox" />
<text>展示多选框</text>
</view>
<view>
<checkbox :checked="selectParent" @click="selectParent = !selectParent" />
<text>父节点可选</text>
</view>
<view>
<checkbox :checked="lazy" @click="lazy = !lazy" :disabled="true" />
<text>懒加载子节点</text>
</view>
<view>
<checkbox :checked="checkStrictly" @click="checkStrictly = !checkStrictly" />
<text>父子节点是否不相关联</text>
</view>
<button @click="openAllNodes" type="warn">打开所有</button>
<button @click="openNodes([2, 3002])">打开3002、2</button>
<button @click="closeAllNodes" type="warn">关闭所有</button>
<button @click="closeNodes([3002, 2])">关闭3002、2</button>
<button @click="setCurrentKeys([3002, 2001])" type="primary">仅选中3002、2001</button>
<button @click="setCurrentKeys(1001)" type="primary">仅选中1001</button>
<button @click="setChecked(3004, true)" type="primary">选中3004</button>
<button @click="setChecked(3004, false)" type="primary">取消选中3004</button>
<view class="mt-20">
<text style="color: red">注意事项:\n</text>
<text>1. lazy为true时,无法打开未渲染的节点)</text>
</view>
<view class="mt-20">
<text style="word-break: break-all">已选中的节点:{{ JSON.stringify(actives) }}</text>
</view>
<view>
<text style="word-break: break-all">半选中的节点:{{ JSON.stringify(halfActives) }}</text>
</view>
</view>
<view style="overflow-y: scroll; height: 100%; flex: 1">
<ev-tree
ref="treeRef"
:list="list"
:props="{ children: 'children', label: 'name', key: 'id' }"
@nodeClick="clickCate"
mainColor="#ffaa00"
:accordion="accordion"
:multipe="multipe"
:showCheckBox="showCheckBox"
:selectParent="selectParent"
:lazy="lazy"
:checkStrictly="checkStrictly"
>
<!-- 插槽示例-->
<!-- <template v-slot="{ node, list }">
<view>{{ node?.name }}-{{ node?.id }}</view>
<view @click.stop= "deleteNode(node)" style="color:#ffaa00">删除</view>
</template> -->
</ev-tree>
</view>
</view>
</template>
<script>
import { data } from './data.js';
const lazy = true;
export default {
data() {
return {
title: 'Hello',
list: [],
accordion: false, //手风琴模式
multipe: false, // 多选
showCheckBox: false, // 展示多选框
selectParent: false, // 父节点可选
checkStrictly: false, // 父子节点不关联
lazy: false, // 懒加载
actives: [],
halfActives: []
};
},
onLoad() {
this.getList();
},
methods: {
async getList() {
// 模拟调用接口
uni.showLoading({ title: '模拟接口调用...' });
await this.sleep(300);
uni.hideLoading();
this.list = data;
},
clickCate(node, type) {
if (type === 'open') {
console.log('打开节点', node);
} else if (type === 'close') {
console.log('关闭节点', node);
} else if (type === 'checked') {
console.log('选中节点', node);
} else if (type === 'unchecked') {
console.log('取消选中节点', node);
}
this.getCheckedKeys();
this.getHalfCheckedKeys();
},
setCurrentKeys(arg) {
this.$refs.treeRef.setCurrentKeys(arg);
},
openNodes(arg) {
this.$refs.treeRef.openCurrentKeys(arg);
},
closeNodes(arg) {
this.$refs.treeRef.closeCurrentKeys(arg);
},
openAllNodes() {
this.$refs.treeRef.openAllKeys();
},
closeAllNodes() {
this.$refs.treeRef.closeAllKeys();
},
getCheckedKeys() {
this.actives = this.$refs.treeRef.getCheckedKeys();
},
getHalfCheckedKeys() {
this.halfActives = this.$refs.treeRef.getHalfCheckedKeys();
},
setChecked(arg, status) {
this.$refs.treeRef.setChecked(arg, status);
},
sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
},
deleteNode(node) {
console.log('删除事件', node);
}
}
};
</script>
可选参数
ev-tree参数
属性名 |
类型 |
默认值 |
说明 |
list |
Array |
[] |
树列表 |
props |
Object |
|
详见下方props说明 |
accordion |
Boolean |
false |
手风琴模式 |
mainColor |
String |
#007aff |
主题色 |
multipe |
Boolean |
false |
是否多选 |
showCheckBox |
Boolean |
false |
展示多选框 |
selectParent |
Boolean |
false |
父节点可选中 |
checkStrictly |
Boolean |
false |
在显示复选框的情况下,是否严格的遵循父子不互相关联的做法 |
lazy |
Boolean |
false |
懒加载 |
props参数:
属性名 |
类型 |
默认值 |
说明 |
children |
String |
children |
子树字段 |
label |
String |
label |
显示字段 |
key |
String |
key |
唯一标识字段 |
方法:
方法名 |
说明 |
参数 |
setCurrentKeys |
设置当前选中的节点 |
(keys, shouldAutoExpandParent);keys为节点key的数组(非数组则自动转为数组),shouldAutoExpandParent标识是否自动打开父节点 |
openCurrentKeys |
打开指定节点 |
keys;keys为节点key的数组(非数组则自动转为数组) |
closeCurrentKeys |
关闭指定节点 |
keys;keys为节点key的数组(非数组则自动转为数组) |
openAllKeys |
打开全部节点 |
无 |
closeAllKeys |
关闭全部节点 |
无 |
getCheckedKeys |
获取当前选中的节点 |
无。返回一个节点key数组 |
getHalfCheckedKeys |
获取当前半选中的节点 |
无。返回一个节点key数组 |
setChecked |
设置节点是否被选中 |
(key, status, shouldAutoExpandParent = true);key为节点key,status为是否选中,shouldAutoExpandParent标识是否自动打开父节点 |
事件:
事件名 |
说明 |
回调参数 |
nodeClick |
节点点击事件 |
(node, type);node为节点对应的对象,type为事件类型(open/close/checked/unchecked) |
Scoped Slot插槽说明
name |
说明 |
— |
自定义树节点的内容,参数为 { node, data } |