树下拉
基础下拉
使用 VTreeDrop
组件可实现树下拉
查看代码
vue
<template>
<VTreeDrop :data="data" checkable />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { VTreeDrop } from '@wsfe/vue-tree'
const data = ref([
{
title: 'node-1',
id: 'node-1',
children: [
{
title: 'node-1-1',
id: 'node-1-1',
children: [
{
title: 'node-1-1-1',
id: 'node-1-1-1',
},
{
title: 'node-1-1-2',
id: 'node-1-1-2',
},
{
title: 'node-1-1-3',
id: 'node-1-1-3',
},
],
},
{
title: 'node-1-2',
id: 'node-1-2',
children: [
{
title: 'node-1-2-1',
id: 'node-1-2-1',
},
{
title: 'node-1-2-2',
id: 'node-1-2-2',
},
],
},
{
title: 'node-1-3',
id: 'node-1-3',
children: [
{
title: 'node-1-3-1',
id: 'node-1-3-1',
},
{
title: 'node-1-3-2',
id: 'node-1-3-2',
},
],
},
],
},
{
title: 'node-2',
id: 'node-2',
children: [
{
title: 'node-2-1',
id: 'node-2-1',
children: [
{
title: 'node-2-1-1',
id: 'node-2-1-1',
},
{
title: 'node-2-1-2',
id: 'node-2-1-2',
},
],
},
],
},
])
</script>
自定义输入框展示
如果想自定义展示输入框选中的值,可使用 display
插槽
查看代码
vue
<template>
<VTreeDrop :data="data" checkable>
<template #display="scope">
<div
:style="{
width: '170px',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
}"
>
{{ scope.checkedNodes.map((node) => node.title).join(',') }}
</div>
</template>
</VTreeDrop>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { VTreeDrop } from '@wsfe/vue-tree'
const data = ref([
{
title: 'node-1',
id: 'node-1',
children: [
{
title: 'node-1-1',
id: 'node-1-1',
children: [
{
title: 'node-1-1-1',
id: 'node-1-1-1',
},
{
title: 'node-1-1-2',
id: 'node-1-1-2',
},
{
title: 'node-1-1-3',
id: 'node-1-1-3',
},
],
},
{
title: 'node-1-2',
id: 'node-1-2',
children: [
{
title: 'node-1-2-1',
id: 'node-1-2-1',
},
{
title: 'node-1-2-2',
id: 'node-1-2-2',
},
],
},
{
title: 'node-1-3',
id: 'node-1-3',
children: [
{
title: 'node-1-3-1',
id: 'node-1-3-1',
},
{
title: 'node-1-3-2',
id: 'node-1-3-2',
},
],
},
],
},
{
title: 'node-2',
id: 'node-2',
children: [
{
title: 'node-2-1',
id: 'node-2-1',
children: [
{
title: 'node-2-1-1',
id: 'node-2-1-1',
},
{
title: 'node-2-1-2',
id: 'node-2-1-2',
},
],
},
],
},
])
</script>
自定义整个输入框
使用默认插槽可把整个输入框替换掉
查看代码
vue
<template>
<VTreeDrop :data="data" checkable>
<template #default>
<div
:style="{
width: '170px',
cursor: 'pointer',
}"
>Click to show drop</div>
</template>
</VTreeDrop>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { VTreeDrop } from '@wsfe/vue-tree'
const data = ref([
{
title: 'node-1',
id: 'node-1',
children: [
{
title: 'node-1-1',
id: 'node-1-1',
children: [
{
title: 'node-1-1-1',
id: 'node-1-1-1',
},
{
title: 'node-1-1-2',
id: 'node-1-1-2',
},
{
title: 'node-1-1-3',
id: 'node-1-1-3',
},
],
},
{
title: 'node-1-2',
id: 'node-1-2',
children: [
{
title: 'node-1-2-1',
id: 'node-1-2-1',
},
{
title: 'node-1-2-2',
id: 'node-1-2-2',
},
],
},
{
title: 'node-1-3',
id: 'node-1-3',
children: [
{
title: 'node-1-3-1',
id: 'node-1-3-1',
},
{
title: 'node-1-3-2',
id: 'node-1-3-2',
},
],
},
],
},
{
title: 'node-2',
id: 'node-2',
children: [
{
title: 'node-2-1',
id: 'node-2-1',
children: [
{
title: 'node-2-1-1',
id: 'node-2-1-1',
},
{
title: 'node-2-1-2',
id: 'node-2-1-2',
},
],
},
],
},
])
</script>