element-plus/docs/examples/tree-v2/default-state.vue
Xc 48a056b051
docs: modify layout style (#10514)
* docs: modify layout style

* docs: modify nav padding

* docs: update style

* feat: update

* docs: upadte

* docs: update

* docs: modify layout style

* docs: update style

* feat: update

* docs: upadte

* docs: update

* docs: update

* docs: update

* docs: remove empty script

* docs: update

---------

Co-authored-by: kooriookami <bingshuanglingluo@163.com>
Co-authored-by: kooriookami <38392315+kooriookami@users.noreply.github.com>
2024-02-19 20:10:44 +08:00

69 lines
1.4 KiB
Vue

<template>
<el-tree-v2
style="max-width: 600px"
:data="data"
:height="208"
:props="props"
show-checkbox
:default-checked-keys="defaultCheckedKeys"
:default-expanded-keys="defaultExpandedKeys"
/>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
interface Tree {
id: string
label: string
children?: Tree[]
}
const getKey = (prefix: string, id: number) => {
return `${prefix}-${id}`
}
const createData = (
maxDeep: number,
maxChildren: number,
minNodesNumber: number,
deep = 1,
key = 'node'
): Tree[] => {
let id = 0
return Array.from({ length: minNodesNumber })
.fill(deep)
.map(() => {
const childrenNumber =
deep === maxDeep ? 0 : Math.round(Math.random() * maxChildren)
const nodeKey = getKey(key, ++id)
return {
id: nodeKey,
label: nodeKey,
children: childrenNumber
? createData(maxDeep, maxChildren, childrenNumber, deep + 1, nodeKey)
: undefined,
}
})
}
const props = {
value: 'id',
label: 'label',
children: 'children',
}
const data = createData(4, 30, 40)
const checkedKeys: string[] = []
const expanedKeys: string[] = []
for (const datum of data) {
const children = datum.children
if (children) {
expanedKeys.push(datum.id)
checkedKeys.push(children[0].id)
break
}
}
const defaultCheckedKeys = ref(checkedKeys)
const defaultExpandedKeys = ref(expanedKeys)
</script>