Tree树形控件·续2
<template> <div> <div style="margin-bottom: 16px"> showLine: <a-switch v-model:checked="showLine" /> <br /> <br /> showIcon: <a-switch v-model:checked="showIcon" /> </div> <a-tree :show-line="showLine" :show-icon="showIcon" :default-expanded-keys="['0-0-0', '0-0-1', '0-0-2']" @select="onSelect" > <template #icon><carry-out-outlined /></template> <a-tree-node key="0-0"> <template #icon><carry-out-outlined /></template> <template #title><span style="color: #1890ff">parent 1</span></template> <a-tree-node title="parent 1-0" key="0-0-0"> <template #icon><carry-out-outlined /></template> <a-tree-node title="leaf" key="0-0-0-0"> <template #icon><carry-out-outlined /></template> </a-tree-node> <a-tree-node title="leaf" key="0-0-0-1"> <template #icon><carry-out-outlined /></template> </a-tree-node> <a-tree-node title="leaf" key="0-0-0-2"> <template #icon><carry-out-outlined /></template> </a-tree-node> </a-tree-node> <a-tree-node title="parent 1-1" key="0-0-1"> <template #icon><carry-out-outlined /></template> <a-tree-node title="leaf" key="0-0-1-0"> <template #icon><carry-out-outlined /></template> </a-tree-node> </a-tree-node> <a-tree-node title="parent 1-2" key="0-0-2"> <template #icon><carry-out-outlined /></template> <a-tree-node title="leaf" key="0-0-2-0"> <template #icon><carry-out-outlined /></template> </a-tree-node> <a-tree-node title="leaf" key="0-0-2-1"> <template #icon><carry-out-outlined /></template> <template #switcherIcon><form-outlined /></template> </a-tree-node> </a-tree-node> </a-tree-node> </a-tree> </div></template><script lang="ts">import { CarryOutOutlined, FormOutlined } from '@ant-design/icons-vue';import { SelectEvent } from 'ant-design-vue/es/tree/Tree';import { defineComponent, ref } from 'vue';export default defineComponent({ components: { CarryOutOutlined, FormOutlined, }, setup() { const showLine = ref<boolean>(true); const showIcon = ref<boolean>(false); const onSelect = (selectedKeys: string[], info: SelectEvent) => { console.log('selected', selectedKeys, info); }; return { showLine, showIcon, onSelect, }; },});</script>
-
parent 1
-
parent 1-0
-
leaf
-
leaf
-
leaf
-
parent 1-1
-
parent 1-2
自定义展开/折叠图标
自定义展开/折叠图标。
<template> <a-tree showLine v-model:expandedKeys="expandedKeys" v-model:selectedKeys="selectedKeys"> <template #switcherIcon><down-outlined /></template> <a-tree-node title="parent 1" key="0-0"> <a-tree-node title="parent 1-0" key="0-0-0"> <a-tree-node title="leaf" key="0-0-0-0" /> <a-tree-node title="leaf" key="0-0-0-1" /> <a-tree-node title="leaf" key="0-0-0-2" /> </a-tree-node> <a-tree-node key="0-0-1" title="parent 1-1"> <a-tree-node key="0-0-1-0" title="leaf" /> </a-tree-node> <a-tree-node key="0-0-2" title="parent 1-2"> <a-tree-node key="0-0-2-0" title="leaf" /> <a-tree-node key="0-0-2-1" title="leaf" /> </a-tree-node> </a-tree-node> </a-tree></template><script lang="ts">import { DownOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';export default defineComponent({ components: { DownOutlined, }, setup() { const expandedKeys = ref<string[]>(['0-0-0']); const selectedKeys = ref<string[]>([]); return { expandedKeys, selectedKeys, }; },});</script>
API
Tree props
| blockNode |
是否节点占据一行 |
boolean |
false |
|
|
|
|
|
|
| treeData |
treeNodes 数据,如果设置则不需要手动构造 TreeNode 节点(key 在整个树范围内唯一) |
array<{key, title, children, [disabled, selectable]}> |
— |
|
|
|
|
|
|
| replaceFields |
替换 treeNode 中 title,key,children 字段为 treeData 中对应的字段 |
object |
{children:’children’, title:’title’, key:’key’ } |
|
|
|
|
|
|
| autoExpandParent |
是否自动展开父节点 |
boolean |
true |
|
|
|
|
|
|
| checkable |
节点前添加 Checkbox 复选框 |
boolean |
false |
|
|
|
|
|
|
| checkedKeys(v-model) | (受控)选中复选框的树节点(注意:父子节点有关联,如果传入父节点 key,则子节点自动选中;相应当子节点 key 都传入,父节点也自动选中。当设置checkable和checkStrictly,它是一个有checked和halfChecked属性的对象,并且父子节点的选中与否不再关联 | string[] | number[] | {checked: string[] | number[], halfChecked: string[] | number[]} | [] | |
|---|---|---|---|---|
| checkStrictly |
checkable 状态下节点选择完全受控(父子节点选中状态不再关联) |
boolean |
false |
|
|
|
|
|
|
| defaultExpandAll |
默认展开所有树节点, 如果是异步数据,需要在数据返回后再实例化,建议用 v-if=”data.length”;当有 expandedKeys 时,defaultExpandAll 将失效 |
boolean |
false |
|
|
|
|
|
|
| draggable |
设置节点可拖拽 |
boolean |
false |
|
|
|
|
|
|
| expandedKeys(v-model) | (受控)展开指定的树节点 | string[] | number[] | [] | |
|---|---|---|---|---|
| filterTreeNode |
按需筛选树节点(高亮),返回 true |
function(node) |
- |
|
|
|
|
|
|
| loadData |
异步加载数据 |
function(node) |
- |
|
|
|
|
|
|
| loadedKeys | (受控)已经加载的节点,需要配合 loadData 使用 | string[] | number[] | [] | |
|---|---|---|---|---|
| multiple |
支持点选多个节点(节点本身) |
boolean |
false |
|
|
|
|
|
|
| selectable |
是否可选中 |
boolean |
true |
|
|
|
|
|
|
| selectedKeys(v-model) | (受控)设置选中的树节点 | string[] | number[] | - | |
|---|---|---|---|---|
| showIcon |
是否展示 TreeNode title 前的图标,没有默认样式,如设置为 true,需要自行定义图标相关样式 |
boolean |
false |
|
|
|
|
|
|
| switcherIcon |
自定义树节点的展开/折叠图标 |
slot |
- |
|
|
|
|
|
|
| showLine |
是否展示连接线 |
boolean |
false |
|
|
|
|
|
|
事件
| check |
点击复选框触发 |
function(checkedKeys, e:{checked: bool, checkedNodes, node, event}) |
|
|
|
| dragend |
dragend 触发时调用 |
function({event, node}) |
|
|
|
| dragenter |
dragenter 触发时调用 |
function({event, node, expandedKeys}) |
|
|
|
| dragleave |
dragleave 触发时调用 |
function({event, node}) |
|
|
|
| dragover |
dragover 触发时调用 |
function({event, node}) |
|
|
|
| dragstart |
开始拖拽时调用 |
function({event, node}) |
|
|
|
| drop |
drop 触发时调用 |
function({event, node, dragNode, dragNodesKeys}) |
|
|
|
| expand |
展开/收起节点时触发 |
function(expandedKeys, {expanded: bool, node}) |
|
|
|
| load |
节点加载完毕时触发 |
function(loadedKeys, {event, node}) |
|
|
|
| rightClick |
响应右键点击 |
function({event, node}) |
|
|
|
| select |
点击树节点触发 |
function(selectedKeys, e:{selected: bool, selectedNodes, node, event}) |
|
|
|
TreeNode props
结点描述数据对象,是 treeNodes 中的一项,TreeNode 使用相同的 API。
| style | 节点的 style | string|object | - | |
|---|---|---|---|---|
| checkable |
当树为 checkable 时,设置独立节点是否展示 Checkbox |
boolean |
- |
|
|
|
|
|
|
| disableCheckbox |
禁掉 checkbox |
boolean |
false |
|
|
|
|
|
|
| disabled |
禁掉响应 |
boolean |
false |
|
|
|
|
|
|
| icon | 自定义图标。可接收组件,props 为当前节点 props | slot|slot-scope | - | |
|---|---|---|---|---|
| isLeaf |
设置为叶子节点(设置了loadData时有效) |
boolean |
false |
|
|
|
|
|
|
| key | 被树的 (default)ExpandedKeys / (default)CheckedKeys / (default)SelectedKeys 属性所用。注意:整个树范围内的所有节点的 key 值不能重复! | string | number | 内部计算出的节点位置 | |
|---|---|---|---|---|
| selectable |
设置节点是否可被选中 |
boolean |
true |
|
|
|
|
|
|
| title | 标题 | string|slot|slot-scope | ‘—-‘ | |
|---|---|---|---|---|
| slots |
使用 treeNodes 时,可以通过该属性配置支持 slot 的属性,如 slots: { title: ‘XXX’} |
object |
- |
|
|
|
|
|
|
DirectoryTree props