naive-ui/demo/documentation/components/tree/zhCN/async.demo.md

76 lines
1.6 KiB
Markdown
Raw Normal View History

2020-02-25 18:48:29 +08:00
# 异步加载
2020-12-12 14:44:44 +08:00
2020-02-25 23:03:38 +08:00
设定 `remote` 后,使用 `on-load` 回调来加载数据。异步加载时,所有 `isLeaf``false` 并且 `children` 不为数组的节点会被视为未加载的节点。
2020-12-12 14:44:44 +08:00
2020-02-25 18:48:29 +08:00
```html
<n-tree
block-node
checkable
remote
:data="data"
:checked-keys="checkedKeys"
:on-load="handleLoad"
2020-10-01 14:46:08 +08:00
@update:checked-keys="handleCheckedKeysChange"
2020-02-25 18:48:29 +08:00
:expanded-keys="expandedKeys"
2020-10-01 14:46:08 +08:00
@update:expanded-keys="handleExpandedKeysChange"
2020-02-25 18:48:29 +08:00
/>
```
2020-12-12 14:44:44 +08:00
2020-02-25 18:48:29 +08:00
```js
2020-12-12 14:44:44 +08:00
function createData() {
2020-02-25 18:48:29 +08:00
return [
{
label: nextLabel(),
key: 1,
isLeaf: false
},
{
label: nextLabel(),
key: 2,
isLeaf: false
}
]
}
2020-12-12 14:44:44 +08:00
function nextLabel(currentLabel) {
2020-02-25 23:03:38 +08:00
if (!currentLabel) return '道生一'
if (currentLabel === '道生一') return '一生二'
2020-02-25 18:48:29 +08:00
if (currentLabel === '一生二') return '二生三'
if (currentLabel === '二生三') return '三生万物'
2020-02-25 23:03:38 +08:00
if (currentLabel === '三生万物') return '道生一'
2020-02-25 18:48:29 +08:00
}
export default {
2020-12-12 14:44:44 +08:00
data() {
2020-02-25 18:48:29 +08:00
return {
data: createData(),
expandedKeys: [],
checkedKeys: []
}
},
methods: {
2020-12-12 14:44:44 +08:00
handleExpandedKeysChange(expandedKeys) {
2020-02-25 18:48:29 +08:00
this.expandedKeys = expandedKeys
},
2020-12-12 14:44:44 +08:00
handleCheckedKeysChange(checkedKeys) {
2020-02-25 18:48:29 +08:00
this.checkedKeys = checkedKeys
},
2020-12-12 14:44:44 +08:00
handleLoad(node) {
2020-02-25 18:48:29 +08:00
return new Promise((resolve, reject) => {
setTimeout(() => {
node.children = [
{
label: nextLabel(node.label),
key: node.key + nextLabel(node.label),
isLeaf: false
}
]
this.data = Array.from(this.data)
resolve()
}, 1000)
})
}
}
}
2020-12-12 14:44:44 +08:00
```