|
| 1 | +<docs> |
| 2 | +--- |
| 3 | +order: 0 |
| 4 | +title: |
| 5 | + zh-CN: 手风琴模式 |
| 6 | + en-US: Accordion |
| 7 | +--- |
| 8 | + |
| 9 | +## zh-CN |
| 10 | + |
| 11 | +同一级的节点,每次只能展开一个 |
| 12 | + |
| 13 | +## en-US |
| 14 | + |
| 15 | +Nodes of the same level can only be expanded one |
| 16 | + |
| 17 | +</docs> |
| 18 | +<template> |
| 19 | + <a-tree |
| 20 | + v-model:selectedKeys="selectedKeys" |
| 21 | + :expanded-keys="expandedKeys" |
| 22 | + :tree-data="treeData" |
| 23 | + @expand="handleExpand" |
| 24 | + > |
| 25 | + <template #title="{ title, key }"> |
| 26 | + <span v-if="key === '0-0-1-0'" style="color: #1890ff">{{ title }}</span> |
| 27 | + <template v-else>{{ title }}</template> |
| 28 | + </template> |
| 29 | + </a-tree> |
| 30 | +</template> |
| 31 | +<script lang="ts"> |
| 32 | +import type { TreeProps } from 'ant-design-vue'; |
| 33 | +import _ from 'lodash'; |
| 34 | +import { defineComponent, ref, watch } from 'vue'; |
| 35 | +
|
| 36 | +const treeData: TreeProps['treeData'] = [ |
| 37 | + { |
| 38 | + title: 'parent 1', |
| 39 | + key: '0-0', |
| 40 | + children: [ |
| 41 | + { |
| 42 | + title: 'parent 1-0', |
| 43 | + key: '0-0-0', |
| 44 | + children: [ |
| 45 | + { title: 'leaf', key: '0-0-0-0' }, |
| 46 | + { title: 'leaf', key: '0-0-0-1' }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + { |
| 50 | + title: 'parent 1-1', |
| 51 | + key: '0-0-1', |
| 52 | + children: [{ key: '0-0-1-0', title: 'sss' }], |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + { |
| 57 | + title: 'parent 2', |
| 58 | + key: '1-0', |
| 59 | + children: [ |
| 60 | + { |
| 61 | + title: 'parent 2-0', |
| 62 | + key: '1-0-0', |
| 63 | + }, |
| 64 | + { |
| 65 | + title: 'parent 2-1', |
| 66 | + key: '2-0-1', |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | +]; |
| 71 | +
|
| 72 | +export default defineComponent({ |
| 73 | + setup() { |
| 74 | + const expandedKeys = ref<string[]>([]); |
| 75 | + const selectedKeys = ref<string[]>(['0-0-0', '0-0-1']); |
| 76 | + const checkedKeys = ref<string[]>(['0-0-0', '0-0-1']); |
| 77 | + watch(expandedKeys, () => { |
| 78 | + console.log('expandedKeys', expandedKeys); |
| 79 | + }); |
| 80 | + watch(selectedKeys, () => { |
| 81 | + console.log('selectedKeys', selectedKeys); |
| 82 | + }); |
| 83 | + watch(checkedKeys, () => { |
| 84 | + console.log('checkedKeys', checkedKeys); |
| 85 | + }); |
| 86 | + const handleExpand = (keys: string[], { expanded, node }) => { |
| 87 | + // node.parent add from 3.0.0-alpha.10 |
| 88 | + const tempKeys = ((node.parent ? node.parent.children : treeData) || []).map( |
| 89 | + ({ key }) => key, |
| 90 | + ); |
| 91 | + if (expanded) { |
| 92 | + expandedKeys.value = _.difference(keys, tempKeys).concat(node.key); |
| 93 | + } else { |
| 94 | + expandedKeys.value = keys; |
| 95 | + } |
| 96 | + }; |
| 97 | + return { |
| 98 | + treeData, |
| 99 | + expandedKeys, |
| 100 | + selectedKeys, |
| 101 | + checkedKeys, |
| 102 | + handleExpand, |
| 103 | + }; |
| 104 | + }, |
| 105 | +}); |
| 106 | +</script> |
0 commit comments