树形视图
v-treeview
组件对于显示大量嵌套数据很有用。
用例
一个基本示例
template script
<template>
<v-treeview :items="items"></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<div>
<v-row justify="space-around">
<v-switch v-model="dense" label="Dense"></v-switch>
<v-switch v-model="selectable" label="Selectable"></v-switch>
<v-switch v-model="activatable" label="Activatable"></v-switch>
<v-switch v-model="hoverable" label="Hoverable"></v-switch>
<v-switch v-model="shaped" label="Shaped"></v-switch>
<v-switch v-model="rounded" label="Rounded"></v-switch>
<v-switch v-model="openOnClick" label="Open on any item click"></v-switch>
<v-col cols="12">
<v-select v-model="selectedColor" :items="selectedColors" :disabled="!selectable" label="Selected checkbox color"></v-select>
</v-col>
<v-col cols="12">
<v-select v-model="color" :items="selectedColors" :disabled="!activatable" label="Active node color"></v-select>
</v-col>
</v-row>
<v-treeview
:items="items"
:dense="dense"
:selectable="selectable"
:activatable="activatable"
:hoverable="hoverable"
:open-on-click="openOnClick"
:selected-color="selectedColor"
:color="color"
:shaped="shaped"
:rounded="rounded"
></v-treeview>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
dense: false,
selectable: false,
activatable: false,
hoverable: false,
openOnClick: false,
shaped: false,
rounded: false,
color: 'primary',
selectedColor: 'accent',
selectedColors: [
'accent',
'teal',
'red',
'success',
'warning lighten-2',
],
}),
}
</script>
示例
下面是一些简单到复杂的例子。
密度模式
密集模式提供了更紧凑的布局,同时降低了项目的高度。
template script
<template>
<v-treeview
dense
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
复选框颜色
您可以控制所选节点复选框的颜色。
template script
<template>
<v-treeview
selectable
selected-color="red"
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
颜色
您可以控制活动的树形视图节点的文本和背景颜色。
template script
<template>
<v-treeview
activatable
color="warning"
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
形状
形状的树形视图在节点的一侧具有圆形边界。
template script
<template>
<v-treeview
shaped
hoverable
activatable
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
圆角
您可以使树视图节点变成圆角。
template script
<template>
<v-treeview
rounded
hoverable
activatable
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
停用节点
设置 item-disabled
属性可当设置为 true
时,以控制哪个节点的属性会禁用该节点。
template script
<template>
<v-treeview
selectable
item-disabled="locked"
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
locked: true,
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
locked: true,
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf', locked: true },
{ id: 17, name: 'November : pdf', locked: true },
{ id: 18, name: 'Tutorial : html', locked: true },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
选择类型
树形视图现在支持两种不同的选择类型。 默认类型是 ‘leaf’,它仅在 v-model 数组中包括叶节点,但会将父节点呈现为部分或完全选择状态。 另一种模式是 ‘independent’,它允许选择一个父节点,但是每个节点都独立于其父节点和子节点。
template script
<template>
<v-container>
<v-select v-model="selectionType" :items="['leaf', 'independent']" label="Selection type"></v-select>
<v-row>
<v-col>
<v-treeview
v-model="selection"
:items="items"
:selection-type="selectionType"
selectable
return-object
open-all
></v-treeview>
</v-col>
<v-divider vertical></v-divider>
<v-col class="pa-6" cols="6">
<template v-if="!selection.length">
No nodes selected.
</template>
<template v-else>
<div v-for="node in selection" :key="node.id">
{{ node.name }}
</div>
</template>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data: () => ({
selectionType: 'leaf',
selection: [],
items: [
{
id: 1,
name: 'Root',
children: [
{ id: 2, name: 'Child #1' },
{ id: 3, name: 'Child #2' },
{
id: 4,
name: 'Child #3',
children: [
{ id: 5, name: 'Grandchild #1' },
{ id: 6, name: 'Grandchild #2' },
],
},
],
},
],
}),
}
</script>
可选择
您可以轻松选择树形视图节点和子节点。
template script
<template>
<v-treeview
selectable
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
Consulting Support
Utilize John’s in-demand services for guidance and advice on advanced implementations and best practices or in-depth training for your project’s developers.
ads by Vuetify
]($e3ec2117d5271cd0.md)
可激活
树形视图节点可以将其激活。
template script
<template>
<v-treeview
activatable
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
可悬停的
树形视图节点可以具有悬停效果。
template script
<template>
<v-treeview
hoverable
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
打开所有
树形视图节点可以在页面加载时预先打开。
template script
<template>
<v-treeview
open-all
:items="items"
></v-treeview>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Applications :',
children: [
{ id: 2, name: 'Calendar : app' },
{ id: 3, name: 'Chrome : app' },
{ id: 4, name: 'Webstorm : app' },
],
},
{
id: 5,
name: 'Documents :',
children: [
{
id: 6,
name: 'vuetify :',
children: [
{
id: 7,
name: 'src :',
children: [
{ id: 8, name: 'index : ts' },
{ id: 9, name: 'bootstrap : ts' },
],
},
],
},
{
id: 10,
name: 'material2 :',
children: [
{
id: 11,
name: 'src :',
children: [
{ id: 12, name: 'v-btn : ts' },
{ id: 13, name: 'v-card : ts' },
{ id: 14, name: 'v-window : ts' },
],
},
],
},
],
},
{
id: 15,
name: 'Downloads :',
children: [
{ id: 16, name: 'October : pdf' },
{ id: 17, name: 'November : pdf' },
{ id: 18, name: 'Tutorial : html' },
],
},
{
id: 19,
name: 'Videos :',
children: [
{
id: 20,
name: 'Tutorials :',
children: [
{ id: 21, name: 'Basic layouts : mp4' },
{ id: 22, name: 'Advanced techniques : mp4' },
{ id: 23, name: 'All about app : dir' },
],
},
{ id: 24, name: 'Intro : mov' },
{ id: 25, name: 'Conference introduction : avi' },
],
},
],
}),
}
</script>
插槽
使用插槽,我们可以创建一个直观的文件浏览器。 除了 prepend
插槽外,还有一个用于 label
和 append
插槽。
template script
<template>
<v-treeview
v-model="tree"
:open="open"
:items="items"
activatable
item-key="name"
open-on-click
>
<template v-slot:prepend="{ item, open }">
<v-icon v-if="!item.file">
{{ open ? 'mdi-folder-open' : 'mdi-folder' }}
</v-icon>
<v-icon v-else>
{{ files[item.file] }}
</v-icon>
</template>
</v-treeview>
</template>
<script>
export default {
data: () => ({
open: ['public'],
files: {
html: 'mdi-language-html5',
js: 'mdi-nodejs',
json: 'mdi-json',
md: 'mdi-markdown',
pdf: 'mdi-file-pdf',
png: 'mdi-file-image',
txt: 'mdi-file-document-outline',
xls: 'mdi-file-excel',
},
tree: [],
items: [
{
name: '.git',
},
{
name: 'node_modules',
},
{
name: 'public',
children: [
{
name: 'static',
children: [{
name: 'logo.png',
file: 'png',
}],
},
{
name: 'favicon.ico',
file: 'png',
},
{
name: 'index.html',
file: 'html',
},
],
},
{
name: '.gitignore',
file: 'txt',
},
{
name: 'babel.config.js',
file: 'js',
},
{
name: 'package.json',
file: 'json',
},
{
name: 'README.md',
file: 'md',
},
{
name: 'vue.config.js',
file: 'js',
},
{
name: 'yarn.lock',
file: 'txt',
},
],
}),
}
</script>
搜索目录
使用 search 属性轻松过滤您的树形视图。 如果需要区分大小写或模糊过滤,可以通过设置 filter 属性轻松地应用自定义过滤功能。 这类似于 v-autocomplete 组件。
template script
<template>
<v-card
class="mx-auto"
max-width="500"
>
<v-sheet class="pa-4 primary lighten-2">
<v-text-field
v-model="search"
label="Search Company Directory"
dark
flat
solo-inverted
hide-details
clearable
clear-icon="mdi-close-circle-outline"
></v-text-field>
<v-checkbox
v-model="caseSensitive"
dark
hide-details
label="Case sensitive search"
></v-checkbox>
</v-sheet>
<v-card-text>
<v-treeview
:items="items"
:search="search"
:filter="filter"
:open.sync="open"
>
<template v-slot:prepend="{ item }">
<v-icon
v-if="item.children"
v-text="`mdi-${item.id === 1 ? 'home-variant' : 'folder-network'}`"
></v-icon>
</template>
</v-treeview>
</v-card-text>
</v-card>
</template>
<script>
export default {
data: () => ({
items: [
{
id: 1,
name: 'Vuetify Human Resources',
children: [
{
id: 2,
name: 'Core team',
children: [
{
id: 201,
name: 'John',
},
{
id: 202,
name: 'Kael',
},
{
id: 203,
name: 'Nekosaur',
},
{
id: 204,
name: 'Jacek',
},
{
id: 205,
name: 'Andrew',
},
],
},
{
id: 3,
name: 'Administrators',
children: [
{
id: 301,
name: 'Ranee',
},
{
id: 302,
name: 'Rachel',
},
],
},
{
id: 4,
name: 'Contributors',
children: [
{
id: 401,
name: 'Phlow',
},
{
id: 402,
name: 'Brandon',
},
{
id: 403,
name: 'Sean',
},
],
},
],
},
],
open: [1, 2],
search: null,
caseSensitive: false,
}),
computed: {
filter () {
return this.caseSensitive
? (item, search, textKey) => item[textKey].indexOf(search) > -1
: undefined
},
},
}
</script>
同步项目
您可以通过向 load-children
属性提供 Promise 回调来动态加载子数据。 用户首次尝试扩展具有子属性(为空数组)的项目时,将执行此回调。
template script
<template>
<v-card>
<v-card-title class="indigo white--text headline">
User Directory
</v-card-title>
<v-row
class="pa-4"
justify="space-between"
>
<v-col cols="5">
<v-treeview
:active.sync="active"
:items="items"
:load-children="fetchUsers"
:open.sync="open"
activatable
color="warning"
open-on-click
transition
>
<template v-slot:prepend="{ item, active }">
<v-icon v-if="!item.children">mdi-account</v-icon>
</template>
</v-treeview>
</v-col>
<v-divider vertical></v-divider>
<v-col
class="d-flex text-center"
>
<v-scroll-y-transition mode="out-in">
<div
v-if="!selected"
class="title grey--text text--lighten-1 font-weight-light"
style="align-self: center;"
>
Select a User
</div>
<v-card
v-else
:key="selected.id"
class="pt-6 mx-auto"
flat
max-width="400"
>
<v-card-text>
<v-avatar
v-if="avatar"
size="88"
>
<v-img
:src="`https://avataaars.io/${avatar}`"
class="mb-6"
></v-img>
</v-avatar>
<h3 class="headline mb-2">
{{ selected.name }}
</h3>
<div class="blue--text mb-2">{{ selected.email }}</div>
<div class="blue--text subheading font-weight-bold">{{ selected.username }}</div>
</v-card-text>
<v-divider></v-divider>
<v-row
class="text-left"
tag="v-card-text"
>
<v-col class="text-right mr-4 mb-2" tag="strong" cols="5">Company:</v-col>
<v-col>{{ selected.company.name }}</v-col>
<v-col class="text-right mr-4 mb-2" tag="strong" cols="5">Website:</v-col>
<v-col>
<a :href="`//${selected.website}`" target="_blank">{{ selected.website }}</a>
</v-col>
<v-col class="text-right mr-4 mb-2" tag="strong" cols="5">Phone:</v-col>
<v-col>{{ selected.phone }}</v-col>
</v-row>
</v-card>
</v-scroll-y-transition>
</v-col>
</v-row>
</v-card>
</template>
<script>
const avatars = [
'?accessoriesType=Blank&avatarStyle=Circle&clotheColor=PastelGreen&clotheType=ShirtScoopNeck&eyeType=Wink&eyebrowType=UnibrowNatural&facialHairColor=Black&facialHairType=MoustacheMagnum&hairColor=Platinum&mouthType=Concerned&skinColor=Tanned&topType=Turban',
'?accessoriesType=Sunglasses&avatarStyle=Circle&clotheColor=Gray02&clotheType=ShirtScoopNeck&eyeType=EyeRoll&eyebrowType=RaisedExcited&facialHairColor=Red&facialHairType=BeardMagestic&hairColor=Red&hatColor=White&mouthType=Twinkle&skinColor=DarkBrown&topType=LongHairBun',
'?accessoriesType=Prescription02&avatarStyle=Circle&clotheColor=Black&clotheType=ShirtVNeck&eyeType=Surprised&eyebrowType=Angry&facialHairColor=Blonde&facialHairType=Blank&hairColor=Blonde&hatColor=PastelOrange&mouthType=Smile&skinColor=Black&topType=LongHairNotTooLong',
'?accessoriesType=Round&avatarStyle=Circle&clotheColor=PastelOrange&clotheType=Overall&eyeType=Close&eyebrowType=AngryNatural&facialHairColor=Blonde&facialHairType=Blank&graphicType=Pizza&hairColor=Black&hatColor=PastelBlue&mouthType=Serious&skinColor=Light&topType=LongHairBigHair',
'?accessoriesType=Kurt&avatarStyle=Circle&clotheColor=Gray01&clotheType=BlazerShirt&eyeType=Surprised&eyebrowType=Default&facialHairColor=Red&facialHairType=Blank&graphicType=Selena&hairColor=Red&hatColor=Blue02&mouthType=Twinkle&skinColor=Pale&topType=LongHairCurly',
]
const pause = ms => new Promise(resolve => setTimeout(resolve, ms))
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items () {
return [
{
name: 'Users',
children: this.users,
},
]
},
selected () {
if (!this.active.length) return undefined
const id = this.active[0]
return this.users.find(user => user.id === id)
},
},
watch: {
selected: 'randomAvatar',
},
methods: {
async fetchUsers (item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500)
return fetch('https://jsonplaceholder.typicode.com/users')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err))
},
randomAvatar () {
this.avatar = avatars[Math.floor(Math.random() * avatars.length)]
},
},
}
</script>
自定义可选择图标
为您的可选树形视图自定义 on, off 和 indeterminate 图标。 与其他高级功能(如 API 加载项)结合使用。
template script
<template>
<v-card>
<v-toolbar
color="primary"
dark
flat
>
<v-icon>mdi-silverware</v-icon>
<v-toolbar-title>Local hotspots</v-toolbar-title>
</v-toolbar>
<v-row>
<v-col>
<v-card-text>
<v-treeview
v-model="tree"
:load-children="fetch"
:items="items"
selected-color="indigo"
open-on-click
selectable
return-object
expand-icon="mdi-chevron-down"
on-icon="mdi-bookmark"
off-icon="mdi-bookmark-outline"
indeterminate-icon="mdi-bookmark-minus"
>
</v-treeview>
</v-card-text>
</v-col>
<v-divider vertical></v-divider>
<v-col
cols="12"
md="6"
>
<v-card-text>
<div
v-if="tree.length === 0"
key="title"
class="title font-weight-light grey--text pa-4 text-center"
>
Select your favorite breweries
</div>
<v-scroll-x-transition
group
hide-on-leave
>
<v-chip
v-for="(selection, i) in tree"
:key="i"
color="grey"
dark
small
class="ma-1"
>
<v-icon left small>mdi-beer</v-icon>
{{ selection.name }}
</v-chip>
</v-scroll-x-transition>
</v-card-text>
</v-col>
</v-row>
<v-divider></v-divider>
<v-card-actions>
<v-btn
text
@click="tree = []"
>
Reset
</v-btn>
<v-spacer></v-spacer>
<v-btn
class="white--text"
color="green darken-1"
depressed
>
Save
<v-icon right>mdi-content-save</v-icon>
</v-btn>
</v-card-actions>
</v-card>
</template>
<script>
export default {
data: () => ({
breweries: [],
isLoading: false,
tree: [],
types: [],
}),
computed: {
items () {
const children = this.types.map(type => ({
id: type,
name: this.getName(type),
children: this.getChildren(type),
}))
return [{
id: 1,
name: 'All Breweries',
children,
}]
},
shouldShowTree () {
return this.breweries.length > 0 && !this.isLoading
},
},
watch: {
breweries (val) {
this.types = val.reduce((acc, cur) => {
const type = cur.brewery_type
if (!acc.includes(type)) acc.push(type)
return acc
}, []).sort()
},
},
methods: {
fetch () {
if (this.breweries.length) return
return fetch('https://api.openbrewerydb.org/breweries')
.then(res => res.json())
.then(data => (this.breweries = data))
.catch(err => console.log(err))
},
getChildren (type) {
const breweries = []
for (const brewery of this.breweries) {
if (brewery.brewery_type !== type) continue
breweries.push({
...brewery,
name: this.getName(brewery.name),
})
}
return breweries.sort((a, b) => {
return a.name > b.name ? 1 : -1
})
},
getName (name) {
return `${name.charAt(0).toUpperCase()}${name.slice(1)}`
},
},
}
</script>