Tree (树)
功能强大,多级节点展开、折叠、选择、懒加载,支持插槽灵活布局,并具有丰富的样式定制和事件响应能力
支持平台
安卓 | ios | 鸿蒙 | web | 微信小程序 |
---|---|---|---|---|
√ | √ | √ | √ | √ |
示例代码
html
<template>
<t-page title='Tree-基础功能' main-class="p-30">
<t-card title="Tree 树" main-class="mb-30" sub-title="功能强大,多级节点展开、折叠、选择、懒加载,支持插槽灵活布局,并具有丰富的样式定制和事件响应能力"></t-card>
<t-card title="亮点" main-class="mb-30" type="e" effect="l" sub-title="支持折叠动画。功能齐全"></t-card>
<t-tree :list="list" type="p" :row-height="60" :selects="selects" :folds="['1','2']" :show-check-box="true"
@edit="edit" @add="add" :load="lazyload" @delete="trash" @change="change" :accordion="false"></t-tree>
</t-page>
</template>
<script setup>
import { TuiGuid } from '@/uni_modules/tui-plus'
const accordion = ref(false)
const showCheckbox = ref(true)
const selects = ref<string[]>(['11', '6'])
const list = ref<UTSJSONObject[]>([{
id: '1',
label: '一级 1',
children: [{
id: '4',
label: '二级 1-1',
children: [{
id: '9',
label: '三级 1-1-1',
disabled: true,
children: [{
id: '11',
label: '四级 1-1-1'
}]
}, {
id: '12',
label: '三级 1-1-1',
children: [{
id: '13',
label: '四级 1-1-1',
}, {
id: '14',
label: '四级 4-2-1',
}]
}, {
id: '10',
label: '三级 1-1-2'
}]
}, {
id: '16',
label: '二级 1-1',
children: [{
id: '17',
label: '三级 1-1-1',
disabled: true,
children: [{
id: '18',
label: '四级 1-1-1'
}]
}, {
id: '19',
label: '三级 1-1-1',
children: [{
id: '20',
label: '四级 1-1-1',
}]
}, {
id: '21',
label: '最后一级 1-1-2'
}]
}]
}, {
id: '2',
label: '一级 2',
children: [{
id: '5',
label: '二级 2-1',
disabled: true
}, {
id: '6',
label: '二级 2-2'
}]
}, {
id: '3',
label: '一级 3',
children: [{
id: '7',
label: '二级 3-1'
}, {
id: '8',
label: '二级 3-2',
}]
}, {
id: '89',
label: '懒加载',
lazy: true
}])
function change(e : string[]) {
console.log(e)
}
function add(callback : (e : UTSJSONObject[]) => void) {
callback([{
id: TuiGuid(10),
label: `新增${TuiGuid(10)}`
}])
}
function edit(e : UTSJSONObject) {
uni.showModal({
title: `label更新`,
editable: true,
success: (res) => {
if (res.confirm) {
e.set('label', res.content)
}
}
})
}
function trash(e : UTSJSONObject) {
uni.showModal({
title: '提示',
content: `确认删除${e.getString('item.label')}吗?`,
success: function (res) {
if (res.confirm) {
const callback = e.getAny('callback') as () => void
callback()
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
function lazyload(e : UTSJSONObject) : Promise<UTSJSONObject[]> {
return new Promise((
resolve : (res : UTSJSONObject[]) => void,
) => {
setTimeout(() => {
console.log(e) //选中的项
resolve([{
id: TuiGuid(10),
label: `异步数据${TuiGuid(10)}`
}, {
id: TuiGuid(10),
label: `异步数据${TuiGuid(10)}`
}])
}, 2000)
})
}
</script>
属性
名称 | 类型 | 默认值 | 说明 | 可选值 |
---|---|---|---|---|
size | String | mini | 组件尺寸 | large(大尺寸), medium(中尺寸), small(小尺寸), mini(迷你) |
type | String | "" | 组件类型 | info(信息), primary(正常), error(错误), warning(警告), success(成功) |
disabled | Boolean | false | 组件是否禁用 | false, true |
stop | Boolean | false | 是否阻止事件冒泡(Tui统一写法处理事件冒泡) | false, true |
hover | Boolean | true | 是否有点击效果 | false, true |
path | String | "" | 点击组件后跳转的页面路径,如果为空则响应点击事件,如果不为空则跳转页面不会响应单击事件。 | - |
mainClass | String | "" | 组件根节点的样式 | - |
nativeClass | String | "" | 组件根节点原生样式 | - |
effect | String | "normal" | 组件显示主题 | normal(正常), dark(深色), light(浅色), plain(镂空) |
selectIds | string[] | [] | 选中项ID集合 | |
foldIds | string[] | [] | 默认展开的ID集合设置 | |
list | any | `` | 数据源 | |
accordion | Boolean | false | 是否开启手风琴模式 | |
load | function | ()=>void | 懒加载函数 | |
showCheckbox | Boolean | true | 是否显示选择框 | |
lazy | Boolean | false | 是否开启懒加载 | |
showAddButton | Boolean | true | 是否显示增加按钮 | |
addIcon | String | plus-circle | 增加按钮图标 | |
addIconClass | String | `` | 增加按钮样式 | |
showEditButton | Boolean | true | 是否显示编辑按钮 | |
editIcon | String | edit-pen | 编辑按钮图标 | |
editIconClass | String | `` | 编辑按钮图标样式 | |
showDeleteButton | Boolean | true | 是否显示删除按钮 | |
deleteIcon | String | trash | 删除按钮图标 | |
deleteIconClass | String | `` | 删除按钮图标样式 | |
rowHeight | Number | 50 | 每行的高度单位px |
事件
名称 | 返回参数 | 说明 |
---|---|---|
change | (seledtIds: Map<string, TuiTreeData>) | 选中状态变化时触发 |
delete | (e:TuiTreeDeleteData) | 点击删除时触发 |
edit | (e:TuiTreeDeleteData) | 点击编辑时触发 |
plus | (e:TuiTreeDeleteData) | 点击新增、添加时触发 |
插槽
名称 | 返回值 | 说明 |
---|---|---|
default | - |