Tree
组件类型:UxTreeComponentPublicInstance
支持无限嵌套,支持异步加载
平台兼容性
UniApp X
Android | iOS | web | 鸿蒙 Next | 小程序 |
---|---|---|---|---|
√ | √ | √ | x | √ |
UniApp Vue Nvue
Android | iOS | web | 鸿蒙 Next | 小程序 |
---|---|---|---|---|
x | x | √ | x | x |
Props
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
value | Array | 默认值 | |
modelValue | Array | 双向绑定 | |
idKey | String | id | idkey |
labelKey | String | label | labelkey |
nodes | Array | 节点数据 | |
includeChild | Boolean | false | 允许选择父级同时选择其所有子级 |
onlyChild | Boolean | false | 只允许选择子级 |
offset | Number | 15 | 子级距父级水平偏移量 |
theme | String | 主题颜色 | |
color | String | 文本颜色 | |
darkColor | String | 深色none-不显示,auto-自动适配深色模式,其他-颜色 | |
activeColor | String | 选中时颜色优先级高于主题 | |
activeDarkColor | String | 深色none-不显示,auto-自动适配深色模式,其他-颜色 | |
size | Any | $ux.Conf.fontSize | 字体大小 |
sizeType | String | small | 字体大小类型 |
bold | Boolean | false | 字体加粗 |
openIcon | String | 打开图标 | |
closeIcon | String | 关闭图标 | |
iconSize | Any | $ux.Conf.fontSize | 图标字体大小 |
customFont | String | 网络字体路径或自定义字体路径 | |
customFamily | String | 字体family | |
disabled | Boolean | false | 是否禁用 |
theme
值 | 说明 |
---|---|
primary | 主色 |
warning | 警告 |
success | 成功 |
error | 错误 |
info | 文本 |
darkColor
值 | 说明 |
---|---|
none不显示 | |
auto自动适配深色模式 | |
color其他颜色 |
activeDarkColor
值 | 说明 |
---|---|
none不显示 | |
auto自动适配深色模式 | |
color其他颜色 |
sizeType
值 | 说明 |
---|---|
normal正常 | |
small较小 | |
big较大 |
bold
值 | 说明 |
---|---|
true加粗 | |
false正常 |
Events
事件名 | 说明 | 参数 |
---|---|---|
openedChange | 打开时触发 | |
change | 选择时触发 | |
click | 点击时触发 |
示例代码
html
<template>
<ux-page :stack="showDoc">
<ux-navbar :title="title" :bold="true">
<template v-slot:right>
<!-- #ifndef MP -->
<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
<!-- #endif -->
</template>
</ux-navbar>
<ux-scroll>
<ux-card direction="column" icon="flag-filled" title="树节点" :bold="true">
<ux-text text="支持无限嵌套,支持异步加载,支持默认打开和默认选择节点,支持只选择子节点和包含所有子节点"></ux-text>
<ux-col style="margin: 15px;">
<ux-radio value="1" v-model="includeChild" text="允许选择父级同时选择其所有子级" mode="button"></ux-radio>
<ux-radio value="1" v-model="onlyChild" style="margin-top: 10px;" text="只允许选择子级" mode="button"></ux-radio>
</ux-col>
</ux-card>
<ux-card direction="column" icon="arrowright" title="树" :bold="true">
<ux-tree
v-model="values"
:opened-value="opendedValues"
:nodes="nodes"
theme="primary"
:bold="false"
:include-child="includeChild == '1'"
:only-child="onlyChild == '1'"
@click="click"
@opened-change="openedChange"
@change="change">
</ux-tree>
</ux-card>
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
</ux-row>
</ux-placeholder>
</ux-scroll>
</ux-page>
</template>
<template>
<ux-page :stack="showDoc">
<ux-navbar :title="title" :bold="true">
<template v-slot:right>
<!-- #ifndef MP -->
<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
<!-- #endif -->
</template>
</ux-navbar>
<ux-scroll>
<ux-card direction="column" icon="flag-filled" title="树节点" :bold="true">
<ux-text text="支持无限嵌套,支持异步加载,支持默认打开和默认选择节点,支持只选择子节点和包含所有子节点"></ux-text>
<ux-col style="margin: 15px;">
<ux-radio value="1" v-model="includeChild" text="允许选择父级同时选择其所有子级" mode="button"></ux-radio>
<ux-radio value="1" v-model="onlyChild" style="margin-top: 10px;" text="只允许选择子级" mode="button"></ux-radio>
</ux-col>
</ux-card>
<ux-card direction="column" icon="arrowright" title="树" :bold="true">
<ux-tree
v-model="values"
:opened-value="opendedValues"
:nodes="nodes"
theme="primary"
:bold="false"
:include-child="includeChild == '1'"
:only-child="onlyChild == '1'"
@click="click"
@opened-change="openedChange"
@change="change">
</ux-tree>
</ux-card>
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
</ux-row>
</ux-placeholder>
</ux-scroll>
</ux-page>
</template>
ts
<script setup>
import { $ux } from '@/uni_modules/ux-frame'
import * as plus from '@/uni_modules/ux-plus'
const title = ref('')
const showDoc = ref(false)
const values = ref<string[]>(['1-2-2'])
const opendedValues = ref<string[]>(['1', '1-2'])
const includeChild = ref('')
const onlyChild = ref('')
let callback = async (id: string): Promise<UTSJSONObject[]> => {
return Promise.resolve([] as UTSJSONObject[])
}
callback = async (id: string): Promise<UTSJSONObject[]> => {
uni.showLoading({
mask: true,
title: 'Loading'
})
await $ux.Util.sleep(1000)
uni.hideLoading()
return [
{
id: id + "-1",
label: "节点" + id + "-1",
disabled: true
},
{
id: id + "-2",
label: "节点" + id + "-2",
},
{
id: id + "-3",
label: "节点" + id + "-3 (异步加载)",
callback: callback
}
] as UTSJSONObject[]
}
const nodes = ref<UTSJSONObject[]>([
{
id: "1",
label: "节点1",
children: [
{
id: "1-1",
label: "节点1-1",
children: [
{
id: "1-1-1",
label: "节点1-1-1"
},
{
id: "1-1-2",
label: "节点1-1-2"
},
{
id: "1-1-3",
label: "节点1-1-3",
children: [
{
id: "1-1-3-1",
label: "节点1-1-3-1"
},
{
id: "1-1-3-2",
label: "节点1-1-3-2"
}
]
}
]
},
{
id: "1-2",
label: "节点1-2",
disabled: false,
children: [
{
id: "1-2-1",
label: "节点1-2-1"
},
{
id: "1-2-2",
label: "节点1-2-2"
},
{
id: "1-2-3",
label: "节点1-2-3"
}
]
},
{
id: "1-3",
label: "节点1-3",
children: [
{
id: "1-3-1",
label: "节点1-3-1"
},
{
id: "1-3-2",
label: "节点1-3-2"
},
{
id: "1-3-3",
label: "节点1-3-3"
}
]
}
]
},
{
id: "2",
label: "节点2",
children: [
{
id: "2-1",
label: "节点2-1",
disabled: true,
children: [
{
id: "2-1-1",
label: "节点2-1-1"
},
{
id: "2-1-2",
label: "节点2-1-2"
}
]
},
{
id: "2-2",
label: "节点2-2",
children: [
{
id: "2-2-1",
label: "节点2-2-1",
disabled: true
},
{
id: "2-2-2",
label: "节点2-2-2"
},
{
id: "2-2-3",
label: "节点2-2-3"
}
]
}
]
},
{
id: "3",
label: "节点3 (异步加载)",
// 异步加载函数
callback: callback,
}
])
function click(id: string) {
console.log('click', id);
}
function openedChange(ids: string[]) {
console.log('opened', ids);
}
function change(ids: string[]) {
console.log('selected', ids);
}
function onDoc() {
plus.openWeb({
title: '在线文档',
url: 'https://www.uxframe.cn/component/tree.html',
// blur: 1,
success: () => {
showDoc.value = true
},
complete: () => {
showDoc.value = false
}
})
}
onLoad((e : OnLoadOptions) => {
title.value = e['title'] ?? ''
})
</script>
<script setup>
import { $ux } from '@/uni_modules/ux-frame'
import * as plus from '@/uni_modules/ux-plus'
const title = ref('')
const showDoc = ref(false)
const values = ref<string[]>(['1-2-2'])
const opendedValues = ref<string[]>(['1', '1-2'])
const includeChild = ref('')
const onlyChild = ref('')
let callback = async (id: string): Promise<UTSJSONObject[]> => {
return Promise.resolve([] as UTSJSONObject[])
}
callback = async (id: string): Promise<UTSJSONObject[]> => {
uni.showLoading({
mask: true,
title: 'Loading'
})
await $ux.Util.sleep(1000)
uni.hideLoading()
return [
{
id: id + "-1",
label: "节点" + id + "-1",
disabled: true
},
{
id: id + "-2",
label: "节点" + id + "-2",
},
{
id: id + "-3",
label: "节点" + id + "-3 (异步加载)",
callback: callback
}
] as UTSJSONObject[]
}
const nodes = ref<UTSJSONObject[]>([
{
id: "1",
label: "节点1",
children: [
{
id: "1-1",
label: "节点1-1",
children: [
{
id: "1-1-1",
label: "节点1-1-1"
},
{
id: "1-1-2",
label: "节点1-1-2"
},
{
id: "1-1-3",
label: "节点1-1-3",
children: [
{
id: "1-1-3-1",
label: "节点1-1-3-1"
},
{
id: "1-1-3-2",
label: "节点1-1-3-2"
}
]
}
]
},
{
id: "1-2",
label: "节点1-2",
disabled: false,
children: [
{
id: "1-2-1",
label: "节点1-2-1"
},
{
id: "1-2-2",
label: "节点1-2-2"
},
{
id: "1-2-3",
label: "节点1-2-3"
}
]
},
{
id: "1-3",
label: "节点1-3",
children: [
{
id: "1-3-1",
label: "节点1-3-1"
},
{
id: "1-3-2",
label: "节点1-3-2"
},
{
id: "1-3-3",
label: "节点1-3-3"
}
]
}
]
},
{
id: "2",
label: "节点2",
children: [
{
id: "2-1",
label: "节点2-1",
disabled: true,
children: [
{
id: "2-1-1",
label: "节点2-1-1"
},
{
id: "2-1-2",
label: "节点2-1-2"
}
]
},
{
id: "2-2",
label: "节点2-2",
children: [
{
id: "2-2-1",
label: "节点2-2-1",
disabled: true
},
{
id: "2-2-2",
label: "节点2-2-2"
},
{
id: "2-2-3",
label: "节点2-2-3"
}
]
}
]
},
{
id: "3",
label: "节点3 (异步加载)",
// 异步加载函数
callback: callback,
}
])
function click(id: string) {
console.log('click', id);
}
function openedChange(ids: string[]) {
console.log('opened', ids);
}
function change(ids: string[]) {
console.log('selected', ids);
}
function onDoc() {
plus.openWeb({
title: '在线文档',
url: 'https://www.uxframe.cn/component/tree.html',
// blur: 1,
success: () => {
showDoc.value = true
},
complete: () => {
showDoc.value = false
}
})
}
onLoad((e : OnLoadOptions) => {
title.value = e['title'] ?? ''
})
</script>
css
<style lang="scss">
</style>
<style lang="scss">
</style>