docs(base-select): add lazy tree demo

This commit is contained in:
Kagol 2024-05-21 11:20:52 +08:00
parent 60523be4e1
commit b375bc34bc
No known key found for this signature in database
GPG Key ID: 48BBA0ECCB3EB997
2 changed files with 153 additions and 3 deletions

View File

@ -66,6 +66,51 @@
></tiny-tree> ></tiny-tree>
</template> </template>
</tiny-base-select> </tiny-base-select>
<div>场景4下拉树懒加载单选</div>
<tiny-base-select v-model="value4">
<template #panel="{ props: { state }, methods: { updateModelValue } }">
<tiny-tree
lazy
:load="load"
:expand-on-click-node="false"
:icon-trigger-click-node="false"
@node-click="
(data) => {
state.visible = false
updateModelValue(data.id)
nextTick(() => {
state.selectedLabel = data.label
})
}
"
></tiny-tree>
</template>
</tiny-base-select>
<div>场景5下拉树懒加载多选</div>
<tiny-base-select v-model="value5" multiple>
<template #panel="{ props: { state }, methods: { updateModelValue } }">
<tiny-tree
lazy
:load="load"
:expand-on-click-node="false"
:icon-trigger-click-node="false"
:show-checkbox="true"
@check="
(data, { checkedKeys, checkedNodes }) => {
updateModelValue(checkedNodes.map((node) => node.id))
nextTick(() => {
state.selected = state.selected.map((item) => {
return {
...item,
currentLabel: checkedNodes.find((node) => node.id === item.value).label
}
})
})
}
"
></tiny-tree>
</template>
</tiny-base-select>
</template> </template>
<script setup> <script setup>
@ -75,6 +120,8 @@ import { BaseSelect as TinyBaseSelect, Tree as TinyTree } from '@opentiny/vue'
const value = ref() const value = ref()
const value2 = ref([]) const value2 = ref([])
const value3 = ref() const value3 = ref()
const value4 = ref()
const value5 = ref([])
const treeRef = ref() const treeRef = ref()
@ -124,6 +171,34 @@ const filter = (value, data) => {
return data.label.includes(value) return data.label.includes(value)
} }
const load = (node, resolve) => {
if (node.level === 0) {
return resolve([
{
id: 3,
label: '一级 3'
}
])
}
if (node.level > 1) return resolve([])
setTimeout(() => {
const data = [
{
id: 1,
label: '一级 1'
},
{
id: 2,
label: '一级 2',
isLeaf: true
}
]
resolve(data)
}, 500)
}
</script> </script>
<style scoped> <style scoped>

View File

@ -11,7 +11,7 @@
(data) => { (data) => {
state.visible = false state.visible = false
updateModelValue(data.id) updateModelValue(data.id)
nextTick(() => { $nextTick(() => {
state.selectedLabel = data.label state.selectedLabel = data.label
}) })
} }
@ -31,7 +31,7 @@
@check=" @check="
(data, { checkedKeys, checkedNodes }) => { (data, { checkedKeys, checkedNodes }) => {
updateModelValue(checkedNodes.map((node) => node.id)) updateModelValue(checkedNodes.map((node) => node.id))
nextTick(() => { $nextTick(() => {
state.selected = state.selected.map((item) => { state.selected = state.selected.map((item) => {
return { return {
...item, ...item,
@ -58,7 +58,7 @@
(data) => { (data) => {
state.visible = false state.visible = false
updateModelValue(data.id) updateModelValue(data.id)
nextTick(() => { $nextTick(() => {
state.selectedLabel = data.label state.selectedLabel = data.label
}) })
} }
@ -66,6 +66,51 @@
></tiny-tree> ></tiny-tree>
</template> </template>
</tiny-base-select> </tiny-base-select>
<div>场景4下拉树懒加载单选</div>
<tiny-base-select v-model="value4">
<template #panel="{ props: { state }, methods: { updateModelValue } }">
<tiny-tree
lazy
:load="load"
:expand-on-click-node="false"
:icon-trigger-click-node="false"
@node-click="
(data) => {
state.visible = false
updateModelValue(data.id)
$nextTick(() => {
state.selectedLabel = data.label
})
}
"
></tiny-tree>
</template>
</tiny-base-select>
<div>场景5下拉树懒加载多选</div>
<tiny-base-select v-model="value5" multiple>
<template #panel="{ props: { state }, methods: { updateModelValue } }">
<tiny-tree
lazy
:load="load"
:expand-on-click-node="false"
:icon-trigger-click-node="false"
:show-checkbox="true"
@check="
(data, { checkedKeys, checkedNodes }) => {
updateModelValue(checkedNodes.map((node) => node.id))
$nextTick(() => {
state.selected = state.selected.map((item) => {
return {
...item,
currentLabel: checkedNodes.find((node) => node.id === item.value).label
}
})
})
}
"
></tiny-tree>
</template>
</tiny-base-select>
</template> </template>
<script> <script>
@ -80,6 +125,9 @@ export default {
return { return {
value: '', value: '',
value2: [], value2: [],
value3: '',
value4: '',
value5: [],
treeData: [ treeData: [
{ {
id: 1, id: 1,
@ -126,6 +174,33 @@ export default {
if (!value) return true if (!value) return true
return data.label.includes(value) return data.label.includes(value)
},
load(node, resolve) {
if (node.level === 0) {
return resolve([
{
id: 3,
label: '一级 3'
}
])
}
if (node.level > 1) return resolve([])
setTimeout(() => {
const data = [
{
id: 1,
label: '一级 1'
},
{
id: 2,
label: '一级 2',
isLeaf: true
}
]
resolve(data)
}, 500)
} }
} }
} }