Appearance
树形结构 tree
多选用法
html
<template>
<view>
<uni-section class="mb-10" title="多选用法" type="line">
<PlUniTree
ref="PlTreeRef"
:data="roomTreeData"
:field="{
label: 'name',
key: 'id',
showArrow: 'haveChild',
checkedShow: 'haveChild'
}"
:default-expanded-keys="['1']"
:load-api="onLoadData"
checkbox-placement="right"
load-mode
:show-checkbox="showCheckbox"
:show-radio-icon="false"
:default-checked-keys="defaultCheckedKeysValue"
@change="change"
></PlUniTree>
</uni-section>
</view>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { getLazyQueryOrgTree } from '@/api/org'
// 初始化
const roomTreeData = ref<API.TreeType[]>([])
// 请求参数
const params = ref<API.LazyQueryOrgTreeType>({
isInit: true,
orgId: '',
keyword: '',
loadCurrentOrg: true,
orgType: 140
})
const showCheckbox = ref(true)
const defaultCheckedKeysValue = ref([])
const PlTreeRef = ref()
// 点击change事件
const change = (key) => {
// 多选的情况下,如果父级选中,子级也会选中,所以需要过滤掉子级
if (showCheckbox.value) {
const nodes = PlTreeRef.value.getCheckedNodes()
const valueSet = new Set(key)
const checkboxItem = nodes.filter((key) => {
return !valueSet.has(key.parentKey)
})
console.log('多选:', checkboxItem)
}
}
// 懒加载请求数据
const onLoadData = async (item) => {
Object.assign(params.value, {
isInit: false,
orgId: item.key
})
const [err, res] = await getLazyQueryOrgTree(params.value)
if (err) return
return res
}
// 首次加载树形数据
const _getLazyQueryOrgTree = async (obj: API.LazyQueryOrgTreeType) => {
const [err, res] = await getLazyQueryOrgTree(obj)
if (err) return
roomTreeData.value = res
}
onShow(() => {
_getLazyQueryOrgTree(params.value)
})
</script>
单选用法
html
<template>
<view>
<uni-section class="mb-10" title="单选用法" type="line">
<PlUniTree
ref="PlTreeRef"
:data="roomTreeData"
:field="{
label: 'name',
key: 'id',
showArrow: 'haveChild',
checkedShow: 'haveChild'
}"
:default-expanded-keys="['1']"
:load-api="onLoadData"
checkbox-placement="right"
load-mode
:show-checkbox="showCheckbox"
:show-radio-icon="true"
:default-checked-keys="defaultCheckedKeysValue"
@change="change"
></PlUniTree>
</uni-section>
</view>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { getLazyQueryOrgTree } from '@/api/org'
// 初始化
const roomTreeData = ref<API.TreeType[]>([])
// 请求参数
const params = ref<API.LazyQueryOrgTreeType>({
isInit: true,
orgId: '',
keyword: '',
loadCurrentOrg: true,
orgType: 140
})
const showCheckbox = ref(false)
const defaultCheckedKeysValue = ref([])
const PlTreeRef = ref()
// 点击change事件
const change = (key) => {
// 多选的情况下,如果父级选中,子级也会选中,所以需要过滤掉子级
console.log('单选:', key)
}
// 懒加载请求数据
const onLoadData = async (item) => {
Object.assign(params.value, {
isInit: false,
orgId: item.key
})
const [err, res] = await getLazyQueryOrgTree(params.value)
if (err) return
return res
}
// 首次加载树形数据
const _getLazyQueryOrgTree = async (obj: API.LazyQueryOrgTreeType) => {
const [err, res] = await getLazyQueryOrgTree(obj)
if (err) return
roomTreeData.value = res
}
onShow(() => {
_getLazyQueryOrgTree(params.value)
})
</script>
API
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
data | tree的数据 | Array | [] |
themeColor | 主题颜色 | String | |
defaultCheckedKeys | 默认选中的节点,注意单选时为单个key,多选时为key的数组 | Array | String | Number | -- |
checkboxPlacement | 选择框的位置,可选 left/right | String | left |
defaultExpandAll | 是否默认展开全部 | Boolean | false |
defaultExpandedKeys | 默认展开的节点 | Array | -- |
indent | 子项缩进距离,默认40,单位rpx | Number | 40 |
field | 字段对应内容名称,默认为{label: 'label',key: 'key', children: 'children', disabled: 'disabled', append: 'append', showArrow: 'showArrow', checkedShow: 'showArrow'}
| Object |
|
showCheckbox | 是否开启多选,默认单选 | Boolean | false |
showRadioIcon | 是否显示单选图标,默认显示t | Boolean | true |
onlyRadioLeaf | 单选时只允许选中末级,默认可随意选中 | Boolean | false |
checkStrictly | 多选时,是否执行父子不关联的任意勾选,默认父子关联 | Boolean | false |
loadMode | 为 true 时,空的 children 数组会显示展开图标 | Boolean | false |
loadApi | 异步加载接口 | Function | -- |
checkedDisabled | 是否渲染禁用值 | Boolean | false |
packDisabledkey | 是否返回已禁用的但已选中的key | Boolean | true |
expandChecked | 选择时是否展开当前已选的所有下级节点,默认不展开 | Boolean | false |
checkedShow | 开启根据判断是否展示单选或复选框icon | Boolean | true |
maxTagCount | 多选时候最大个数 | Number | -- |
事件
事件名称 | 说明 | 回调参数 |
---|---|---|
change | 选择完成后的回调 | (key, originItem, item) => void |
expand | 点击啊箭头展开收缩 | (expand, currentItem || originItem || null) => void |
方法 ref内部事件(使用通过ref获取内部事件)
事件名称 | 说明 | 回调参数 |
---|---|---|
getCheckedKeys | 返回已选的 key | -- |
setCheckedKeys | 根据key设置已选 | Function(keys, checked = true) |
getHalfCheckedKeys | 返回半选的 key | -- |
getUncheckedKeys | 返回未选的 key | -- |
getExpandedKeys | 返回已展开的 key | -- |
getUnexpandedKeys | 返回未展开的 key | -- |
setExpandedKeys | 根据key展开/收起 | Function(keys, expand = true) |
getCheckedNodes | 返回已选的节点 | -- |
getHalfCheckedNodes | 返回半选的节点 | -- |
getUncheckedNodes | 返回未选的节点 | -- |
getExpandedNodes | 返回已展开的节点 | -- |
getUnexpandedNodes | 返回未展开的节点 | -- |