Skip to content
本页导读

Tree

组件类型:UxTreeComponentPublicInstance

支持无限嵌套,支持异步加载

平台兼容性

UniApp X

AndroidiOSweb鸿蒙 Next小程序
x

UniApp Vue Nvue

AndroidiOSweb鸿蒙 Next小程序
xxxx

Props

属性名类型默认值说明
valueArray默认值
modelValueArray双向绑定
idKeyStringididkey
labelKeyStringlabellabelkey
nodesArray节点数据
includeChildBooleanfalse允许选择父级同时选择其所有子级
onlyChildBooleanfalse只允许选择子级
offsetNumber15子级距父级水平偏移量
themeString主题颜色
colorString文本颜色
darkColorString深色none-不显示,auto-自动适配深色模式,其他-颜色
activeColorString选中时颜色优先级高于主题
activeDarkColorString深色none-不显示,auto-自动适配深色模式,其他-颜色
sizeAny$ux.Conf.fontSize字体大小
sizeTypeStringsmall字体大小类型
boldBooleanfalse字体加粗
openIconString打开图标
closeIconString关闭图标
iconSizeAny$ux.Conf.fontSize图标字体大小
customFontString网络字体路径或自定义字体路径
customFamilyString字体family
disabledBooleanfalse是否禁用

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>